Argument-dependent name lookup

Short description: Behavior in the C++ programming language

In the C++ programming language, argument-dependent lookup (ADL), or argument-dependent name lookup,[1] applies to the lookup of an unqualified function name depending on the types of the arguments given to the function call. This behavior is also known as Koenig lookup, as it is often attributed to Andrew Koenig, though he is not its inventor.[2]

During argument-dependent lookup, other namespaces not considered during normal lookup may be searched where the set of namespaces to be searched depends on the types of the function arguments. Specifically, the set of declarations discovered during the ADL process, and considered for resolution of the function name, is the union of the declarations found by normal lookup with the declarations found by looking in the set of namespaces associated with the types of the function arguments.

## Contents

* 1 Example
* 2 Interfaces
* 3 Criticism
* 4 References
* 5 External links

## Example

An example of ADL looks like this:

namespace foo {
class Bar {
// ...

void baz(Bar& a, int i) {
// ...

int main() {
foo::Bar a;
baz(a, 0);  // Calls foo::baz.

Even though the main function is not in namespace `foo`, nor is namespace `foo` in scope, the function foo::baz(Bar&, int) is found because of the declared types of the actual arguments in the function call statement.

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:

import std;

int main() {
std::string str = "hello world";
std::cout << str;

Using `<<` is equivalent to calling `operator<<` without the `std::` qualifier. However, in this case, the overload of operator<< that works for `string` is in the `std` namespace, so ADL is required for it to be used.

The following code would work without ADL (which is applied to it anyway):

import std;

int main() {
std::cout << 5;

It works because the output operator for integers is a member function of the `std::ostream` class, which is the type of `cout`. Thus, the compiler interprets this statement as

std::cout.operator<<(5);

which it can resolve during normal lookup. However, consider that e.g. the `const char*` overloaded `operator<<` is a non-member function in the `std` namespace and, thus, requires ADL for a correct lookup:

// will print the provided char string as expected
// using ADL derived from the argument type std::cout
operator<<(std::cout, "Hi there")

// calls a ostream member function of the operator<<
// taking a void const*, which will print the address of
// the provided char string instead of the content of the char string
std::cout.operator<<("Hi there")

The `std` namespace overloaded non-member `operator<<` function to handle strings is another example:

// equivalent to
// operator<<(std::cout, str)
// The compiler searches the std namespace using ADL
// due to the type std::string of the str parameter and std::cout
std::cout << str;

As Koenig points out in a personal note,[2] without ADL the compiler would indicate an error stating it could not find `operator<<` as the statement doesn't explicitly specify that it is found in the `std` namespace.

## Interfaces

Functions found by ADL are considered part of a class's interface. In the C++ Standard Library, several algorithms use unqualified calls to `swap` from within the `std` namespace. As a result, the generic `std::swap` 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 overload of `swap` will be used.

## Criticism

While ADL makes it practical for functions defined outside of a class to behave as if they were part of the interface of that 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 `std::swap` to swap two values. The idea is that then one can define an own version of `swap` in one's own namespace and it will be used within the standard library algorithms. In other words, the behavior of

namespace foo {
class Bar {
// ...

Bar a;
Bar b;

std::swap(a, b);

may or may not be the same as the behavior of

using std::swap;
swap(a, b);

(where `a` and `b` are of type `foo::Bar`) because if `foo::swap(foo::Bar&, foo::Bar&)` exists, the second of the above examples will call it while the first will not. Furthermore, if for some reason both `foo::swap(foo::Bar&, foo::Bar&)` and `std::swap(foo::Bar&, foo::Bar&)` are defined, then the first example will call `std::swap(foo::Bar&, foo::Bar&)` 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, `lib1`, expects unqualified calls to `foo(T)` to have one meaning and another library, `lib2` expects it to have another, then namespaces lose their utility. If, however, `lib1` expects `lib1::foo(T)` to have one meaning and `lib2` does likewise, then there is no conflict, but calls to `foo(T)` would have to be fully qualified (i.e. `lib1::foo(x)` as opposed to `using lib1::foo; foo(x);`) lest ADL get in the way.

## References

1. ↑ "Working Draft, Standard for Programming Language C++". JTC1/SC22/WG21. 19 October 2005. Chapter 3.4.2 – Argument-dependent name lookup – p. 2. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf. Retrieved 13 March 2012.
2. ↑ 2.0 2.1 "A Personal Note About Argument-Dependent Lookup". 2012-05-03. http://www.drdobbs.com/cpp/a-personal-note-about-argument-dependent/232901443.

## External links

* Argument-dependent lookup
* What's In a Class? – The Interface Principle by Herb Sutter
* Namespaces and the Interface Principle by Herb Sutter
* Why I Hate Namespaces by Danny Kalev
* "A Modest Proposal: Fixing ADL (revision 2)" by Herb Sutter

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Argument-dependent name lookup. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Argument-dependent_name_lookup&oldid=4298757"