wiki:cpp-adl
C++ ADL
ADL (Argument-Dependent Lookup) is the rule that when calling a function with unqualified name, the compiler searches for candidates not just in local and global scopes but also in the namespaces of the function's arguments. This allows writing std::swap(a, b) and having the compiler find custom swap overloads in the same namespace as the arguments.
Use ADL to find overloaded functions for generic algorithms; qualify with std:: when you want only the standard library version.
Example
This example shows ADL finding custom overloads based on argument namespaces.
// compile: g++ -o adl adl.cpp // run: ./adl // description: ADL finds overloads in argument namespaces #include <iostream> #include <utility> namespace MyLib { class Widget { public: int value = 42; }; void process(const Widget& w) { std::cout << "MyLib::process(Widget): " << w.value << "\n"; } } void process(int x) { std::cout << "global process(int): " << x << "\n"; } int main() { MyLib::Widget w; process(w); // ADL finds MyLib::process because w is in MyLib process(42); // global process(int) return 0; }
wiki/cpp-adl.md · Last modified: by 127.0.0.1
