In the
C++
C++ (, pronounced "C plus plus" and sometimes abbreviated as CPP or CXX) is a high-level, general-purpose programming language created by Danish computer scientist Bjarne Stroustrup. First released in 1985 as an extension of the C programmin ...
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
, argument-dependent lookup (ADL), or argument-dependent name lookup,
applies to the
lookup
In computer science, a lookup table (LUT) is an array that replaces runtime computation of a mathematical function with a simpler array indexing operation, in a process termed as ''direct addressing''. The savings in processing time can be sig ...
of an unqualified
function
Function or functionality may refer to:
Computing
* Function key, a type of key on computer keyboards
* Function model, a structured representation of processes in a system
* Function object or functor or functionoid, a concept of object-orie ...
name depending on the
types
Type may refer to:
Science and technology Computing
* Typing, producing text via a keyboard, typewriter, etc.
* Data type, collection of values used for computations.
* File type
* TYPE (DOS command), a command to display contents of a file.
* Ty ...
of the
argument
An argument is a series of sentences, statements, or propositions some of which are called premises and one is the conclusion. The purpose of an argument is to give reasons for one's conclusion via justification, explanation, and/or persu ...
s given to the
function call
In computer programming, a function (also procedure, method, subroutine, routine, or subprogram) is a callable unit of software logic that has a well-defined interface and behavior and can be invoked multiple times.
Callable units provide a p ...
. This behavior is also known as Koenig lookup, as it is often attributed to
Andrew Koenig
Joshua Andrew Koenig (; August 17, 1968 – February 16, 2010) was an American character actor, film director, editor, writer, and human rights activist. He was known for his role as Richard "Boner" Stabone in ''Growing Pains''.
Early li ...
, though he is not its inventor.
During argument-dependent lookup, other
namespaces
In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified.
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:
namespace NS // namespace NS
int main()
Even though the function is not in namespace NS, nor is namespace NS in scope, the function is found because of the declared types of the actual arguments in the function call statement.
A common pattern in the
C++ Standard Library
The C standard library, sometimes referred to as libc, is the standard library for the C programming language, as specified in the ISO C standard.ISO/ IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C §7'' Starting from the origina ...
is to declare overloaded operators that will be found in this manner. For example, this simple
Hello World
Hello World may refer to:
* "Hello, World!" program, a computer program that outputs or displays the message "Hello, World!"
Music
* "Hello World!" (composition), song by the Iamus computer
* "Hello World" (Tremeloes song), 1969
* "Hello World" ...
program would not compile if it weren't for ADL:
#include
#include
int main()
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):
#include
int main()
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,
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 N // namespace N
A a;
A 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
N::A
) because if
N::swap(N::A&, N::A&)
exists, the second of the above examples will 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
Semantics is the study of linguistic Meaning (philosophy), meaning. It examines what meaning is, how words get their meaning, and how the meaning of a complex expression depends on its parts. Part of this process involves the distinction betwee ...
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 (i.e.
L1::foo(x)
as opposed to
using L1::foo; foo(x);
) lest ADL get in the way.
References
{{Reflist
External links
''Argument-dependent lookup''by
Herb Sutter
Herb Sutter is a prominent C++ expert. He is also an author of several books on C++ and was a columnist for Dr. Dobb's Journal.
Education and career
Sutter was born and raised in Oakville, Ontario, and studied computer science at Canada's Uni ...
''Namespaces and the Interface Principle''by
Herb Sutter
Herb Sutter is a prominent C++ expert. He is also an author of several books on C++ and was a columnist for Dr. Dobb's Journal.
Education and career
Sutter was born and raised in Oakville, Ontario, and studied computer science at Canada's Uni ...
Why I Hate Namespacesby Danny Kalev
"A Modest Proposal: Fixing ADL (revision 2)"by
Herb Sutter
Herb Sutter is a prominent C++ expert. He is also an author of several books on C++ and was a columnist for Dr. Dobb's Journal.
Education and career
Sutter was born and raised in Oakville, Ontario, and studied computer science at Canada's Uni ...
C++
Articles with example C++ code