What is "Argument-Dependent Lookup" ("Koenig Lookup") in C++?

C++Server Side ProgrammingProgramming

Argument-dependent lookup(ADL) is a protocol for looking up unqualified function names in function-call expressions.

These function call expressions include implicit function calls to overloaded operators.

The function names are looked up in the namespaces of their arguments in addition to the scopes and namespaces considered by the usual unqualified name lookup. Argument-dependent lookup makes it possible to use operators defined in a different namespace. 

Example

namespace MyNamespace{
   class A {};
   void f( A &a, int i) {}
}
int main() {
   MyNamespace::A a;
   f( a, 0 );    //calls MyNamespace::f
}

The lookup of a function call to f was dependent on the argument a. The same case is applied to arguments like << and >> that are looked up in std namespace when we use things like cout, cin, endl, etc.

raja
Published on 28-Feb-2018 16:19:46
Advertisements