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


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.

Updated on: 12-Feb-2020

174 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements