Saturday, May 8, 2010

Argument Dependent Lookup (ADL)

The following code compiles:
#include <iostream> 
int main() {endl(std::cout);}
Note that there is no 'std::' in front of endl and endl is being called like a function. Since the parameter to endl specifies the namespace, ADL (Argument Dependent Lookup aka Koenig Look) looks first for the function in the namespace of the parameter, which in this case is std. It is nicer to specify the 'std::' though.

Reference: The C++ Standard Library: A Tutorial and Reference by Nicolai M. Josuttis. Addison-Wesley, 1999, p. 613.

No comments:

Post a Comment