A form of name lookup in C++ which allows function names to be found in namespaces associated with the arguments used in the function call.
An unqualified function call such as func(a, b, c)
will lookup the name func
in the namespaces that are associated with the types of the arguments a
, b
and c
. For example, if a
has type ns::A
and a function ns::func
exists then it can be found by argument dependent lookup and will be added to the overload set used to resolve the call.
The reason for this feature is to allow overloaded operators declared in namespaces to be found, because operators cannot be qualified by a namespace e.g.
namespace ns
{
struct A { };
A operator+(const A&, const A&);
}
void f(ns::A a1, ns::A a2)
{
ns::A sum = a1 + a2; // must find ns::operator+
}
In the example above the +
operator must find ns::operator+
, but without ADL it would not.
ADL allows the natural a1 + a2
syntax to work as expected, instead of having to write something like a2 ns::+ a2
, which isn't valid syntax, or ns::operator+(a1, a2)
.
ADL is sometimes known as "Koenig Lookup" after Andrew Koenig, who suggested the feature.