Argument-dependent Name Lookup - Example

Example

An example of ADL looks like this:

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

A common pattern in the C++ Standard Library is to declare overloaded operators that will be found in this manner. For example, this simple Hello World program would not compile if it weren't for ADL:

#include int main { std::cout << "Hello World, where did operator<< come from?" << std::endl; return 0; }

Using << is equivalent to calling operator<<, which however lacks the std qualifier. In this case, function std::ostream& std::operator<<(std::ostream&, const char*) is found through ADL.

Note that std::endl is a function but it needs full qualification, since it is used as an argument to operator<< (std::endl is a function pointer, not a function call).

Read more about this topic:  Argument-dependent Name Lookup

Famous quotes containing the word example:

    Our intellect is not the most subtle, the most powerful, the most appropriate, instrument for revealing the truth. It is life that, little by little, example by example, permits us to see that what is most important to our heart, or to our mind, is learned not by reasoning but through other agencies. Then it is that the intellect, observing their superiority, abdicates its control to them upon reasoned grounds and agrees to become their collaborator and lackey.
    Marcel Proust (1871–1922)