Argument-dependent name lookupIn 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. ExampleAn example of ADL looks like this: namespace NS {
class A {};
void f(A& a, int i) {}
} // namespace NS
int main() {
NS::A a;
f(a, 0); // Calls NS::f.
}
Even though the main function is not in namespace NS, nor is namespace NS in scope, the function NS::f(A&, 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: #include <iostream>
#include <string>
int main() {
std::string str = "hello world";
std::cout << str;
}
Using The following code would work without ADL (which is applied to it anyway): #include <iostream>
int main() {
std::cout << 5;
}
It works because the output operator for integers is a member function of the std::cout.operator<<(5);
which it can resolve during normal lookup. However, consider that e.g. the /* 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 /*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 InterfacesFunctions found by ADL are considered part of a class's interface. In the C++ Standard Library, several algorithms use unqualified calls to CriticismWhile 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 namespace N {
struct A {};
} // 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 In general, over-dependence on ADL can lead to semantic problems. If one library, References
External links |
Portal di Ensiklopedia Dunia