Argument dependent name lookup

Argument dependent name lookup

In the C++ programming language, Koenig lookup, also known as argument dependent lookup (ADL), or argument dependent name lookup, applies to the lookup of an unqualified function name depending on the types of the arguments given to the function call. This behavior is named after Andrew Koenig.

In Koenig lookup, other namespaces not considered during normal lookup may be searched. The set of namespaces to be searched depends on the types of the function arguments.

For a class A, there is a set of associated classes which make up its direct and indirect base classes. The set of associated namespaces for A is the namespace that contains A together with the namespaces containing A's associated classes. It is this set of associated namespaces that is searched for a function with an argument of type A.

If the normal lookup of the unqualified name finds a class member function, then Koenig lookup does not occur. Otherwise, the set of declarations found by lookup is the union of the declarations found by normal lookup with the declarations found by looking in the set of associated namespaces.

The canonical example of Koenig lookup looks like this:namespace NS { class A {}; void f( A ) {int main() { NS::A a; f( a ); //calls NS::f}A common pattern in the Standard Template 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 Koenig lookup:
#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 Koenig Lookup.

Note that is a function but it needs full qualification, since it is usedas an argument to operator<< ( is a function pointer, not a function call).

Interfaces

Within C++, functions found by Koenig lookup are considered part of a class's interface. Within the Standard Template Library, several algorithms make use of unqualified calls to swap from within the std namespace. As a result, the generic function is used if nothing else is found, but if these algorithms are used with a third-party class, Foo, found in another namespace that also contains swap(Foo&, Foo&), that specialization of swap will be used.

Criticism

While ADL makes it practical for free functions to be part of the interface of a class, it makes namespaces less strict and so can require the use of fully-qualified names when they would not otherwise be needed. For example, the C++ standard library makes extensive use of unqualified calls to to swap two values. The idea being that then one can define a specialization of std::swap in their own namespace and it will be used within the STL algorithms. In other words, the behavior ofstd::swap(a, b);may or may not be the same as the behavior ofusing std::swap;swap(a, b);(where a and b are of type N::A) because if N::swap(N::A&, N::A&) exists, the second of the above examples call it while the first will not. Furthermore, if for some reason both N::swap(N::A&, N::A&) and std::swap(N::A&, N::A&) are defined, then the first example will call std::swap(N::A&, N::A&) but the second will not compile because swap(a, b) would be ambiguous.

In general, over-dependence on ADL can lead to semantic problems. If one library, L1, expects unqualified calls to foo(T) to have one meaning and another library, L2 expects it to have another, then namespaces lose their utility. If, however, L1 expects L1::foo(T) to have one meaning and L2 does likewise, then there is no conflict, but calls to foo(T) would have to be fully qualified (as opposed to using L1::foo; foo(x);) lest ADL get in the way.

External links

* [http://www.gotw.ca/publications/mill02.htm "What's In a Class? - The Interface Principle"] by Herb Sutter
* [http://www.gotw.ca/publications/mill08.htm "Namespaces and the Interface Principle"] by Herb Sutter
* [http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=286 Why I Hate Namespaces] by Danny Kalev
* [http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2103.pdf "A Modest Proposal: Fixing ADL (revision 2)"] by Herb Sutter


Wikimedia Foundation. 2010.

Игры ⚽ Нужно сделать НИР?

Look at other dictionaries:

  • Argument dependent name lookup — (häufig kurz Argument dependent lookup oder ADL, zu deutsch argumentabhängige Namensauflösung) ist eine Technik, um in Programmiersprachen mit Unterstützung sowohl von Namensräumen als auch freien Funktionen unter bestimmten Umständen Symbole aus …   Deutsch Wikipedia

  • Andrew Koenig (programmer) — Andrew Koenig is a former AT T researcher and programmer known for his work with C++. He is the author of C Traps and Pitfalls , co author (with Barbara Moo) of Accelerated C++ Ruminations on C++ , and his name is associated with Argument… …   Wikipedia

  • ADL — is a three letter abbreviation that may refer to:*Action description language, a formal language for automatic planning systems *Activities of daily living *Adelaide Airport, from its IATA airport code *Amalgamated Dairies Limited, The largest… …   Wikipedia

  • Initialisierungsliste — Die Initialisierungsliste ist eine Spezialität der Programmiersprache C++, sie kommt ausschließlich in Konstruktoren vor und hat die Aufgabe, die Konstruktion von Vorfahren Klassen, eingebetteten Elementen und Referenzen zu regeln. Hintergrund Im …   Deutsch Wikipedia

  • ADL — Cette page d’homonymie répertorie les différents sujets et articles partageant un même nom.   Sigles d’une seule lettre   Sigles de deux lettres > Sigles de trois lettres   Sigles de quatre lettres …   Wikipédia en Français

  • Barton-Nackman trick — is a term coined by the C++ standardization committee (ISO/IEC JTC1 SC22 WG21) to refer to an idiom introduced by John Barton and Lee Nackman as Restricted Template Expansion [cite book | last=Barton | first=John J. | coauthors=Lee R. Nackman |… …   Wikipedia

  • Duck typing — Type systems Type safety Inferred vs. Manifest Dynamic vs. Static Strong vs. Weak Nominal vs. Structural Dependent typing Duck typing Latent typing Linear typing Uniqueness typing …   Wikipedia

  • Alcibiades — Infobox Military Person name= Alcibiades Ἀλκιβιάδης Alkibiádēs caption= Alcibiades allegiance= Athens (415–412 BC Sparta) rank= general (strategos) commands= nickname= lived= 450–404 BC placeofbirth= Athens placeofdeath=… …   Wikipedia

  • Agriculture — General …   Wikipedia

  • HSL and HSV — Fig. 1. HSL (a–d) and HSV (e–h). Above (a, e): cut away 3D models of each. Below: two dimensional plots showing two of a model’s three parameters at once, holding the other constant: cylindrical shells (b, f) of constant saturation, in this case… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”