ArticleslgStudy

science

Argument-dependent name lookup

Argument-dependent name lookup is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Argument-dependent name lookup rather than just read about it. In short: In the C++ programming language, 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 also known as Koenig lookup, as it is often attributed to Andrew Koenig, though he is not its inventor.

Key takeaways

  • Argument-dependent name lookup belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Argument-dependent name lookup to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Argument-dependent name lookup from memory before moving on to harder problems.

Reference excerpt

In the C++ programming language, 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 also known as Koenig lookup, as it is often attributed to Andrew Koenig, though he is not its inventor. 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.

Example An example of ADL looks like this:

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:

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):

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

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:

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

As Koenig points out in a personal note, 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

may or may not be the same as the behavior of

(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

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

Worked examples

Example 1 — a first encounter with Argument-dependent name lookup

Start with the simplest possible case. Write down what Argument-dependent name lookup claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Argument-dependent name lookup before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Argument-dependent name lookup ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Argument-dependent name lookup

In research
Argument-dependent name lookup appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Argument-dependent name lookup in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Argument-dependent name lookup is common in secondary-school and first-year university syllabi. It links to neighbouring topics C++, so understanding it makes those chapters shorter.
In everyday life
Look for Argument-dependent name lookup outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Argument-dependent name lookup” →

How to study Argument-dependent name lookup in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Argument-dependent name lookup means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Argument-dependent name lookup out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Argument-dependent name lookup in simple terms?

In the C++ programming language, 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 also known as Koenig lookup, as it is often attributed to A…

Why does Argument-dependent name lookup matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Argument-dependent name lookup?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Argument-dependent name lookup.

Tags

  • C++

Keep exploring