c++ - What is "Argument-Dependent Lookup" (aka ADL, or "Koenig Lookup")? -
what explanations on argument dependent lookup is? many people call koenig lookup well.
preferably i'd know:
- why thing?
- why bad thing?
- how work?
(note: meant entry stack overflow's c++ faq.)
koenig lookup commonly known argument dependent lookup in c++ , of standard c++ compilers support it.
the c++11 standard § 3.4.2/1 states:
when postfix-expression in function call (5.2.2) unqualified-id, other namespaces not considered during usual unqualified lookup (3.4.1) may searched, , in namespaces, namespace-scope friend function declarations (11.3) not otherwise visible may found. these modifications search depend on types of arguments (and template template arguments, namespace of template argument).
in simpler terms nicolai josuttis states1:
you don’t have qualify namespace functions if 1 or more argument types defined in namespace of function.
a simple code example:
namespace mynamespace { class myclass {}; void dosomething(myclass); } mynamespace::myclass obj; // global object int main() { dosomething(obj); // works fine - mynamespace::dosomething() called. }
in above example there neither using-declaration
nor using-directive
still compiler correctly identifies unqualified name dosomething()
function declared in namespace mynamespace
applying koenig algorithm.
how work?
algorithm tells compiler not @ local scope, namespaces contain argument's type. thus, in above code, compiler finds object obj
, argument of function dosomething()
, belongs namespace mynamespace
. so, looks @ namespace locate declaration of dosomething()
.
what advantage of koenig lookup
?
simple code example above demonstrates above koenig algorithm provides convenience , ease of usage programmer. without koenig algorithm there overhead on programmer, repeatedly specify qualified names, or instead, use numerous using-declarations.
why criticism of koenig algorithm
?
on dependence on koenig algorithm can lead semantic problems,and catch programmer off guard sometimes.
consider example of std::swap, standard library algorithm swap 2 values. koenig algorithm 1 have cautious while using algorithm because:
std::swap(obj1,obj2);
may not show same behavior as:
using std::swap; swap(obj1, obj2);
with adl, version of swap
function gets called depend on namespace of arguments passed it.
if there exists namespace a
, if a::obj1
, a::obj2
& a::swap()
exist second example result in call a::swap()
might not user wanted.
further, if reason both:
a::swap(a::myclass&, a::myclass&)
, std::swap(a::myclass&, a::myclass&)
defined, first example call std::swap(a::myclass&, a::myclass&)
second not compile because swap(obj1, obj2)
ambiguous.
trivia:
why called koenig lookup
?
because devised former at&t , bell labs researcher , programmer,andrew koenig.
good reads:
herb sutter's name lookup on gotw
standard c++03/11 [basic.lookup.argdep]: 3.4.2 argument-dependent name lookup.
1 definition of koenig algorithm defined in josuttis's book, the c++ standard library: tutorial , reference.
Comments
Post a Comment