依赖参数的函数查找
依赖参数的函数查找(Argument dependent lookup),(ADL)是用于查找非限制函数名查找的一系列规则。这些函数名在参数的命名空间,再加上没有限制的名字查找的作用域和命名空间,查找。
这一特性的用处
可以将非类成员函数和非类成员操作符定义在与类在同一个命名空间的地方,作为该类的共有接口。例如:
using std::swap;
swap(obj1,obj2);
Example
namespace A {
struct X;
struct Y;
void f(int);
void g(X);
}
namespace B {
void f(int i) {
f(i); // calls B::f (endless recursion)
}
void g(A::X x) {
g(x); // Error: ambiguous between B::g (ordinary lookup)
// and A::g (argument-dependent lookup)
}
void h(A::Y y) {
h(y); // calls B::h (endless recursion): ADL examines the A namespace
// but finds no A::h, so only B::h from ordinary lookup is used
}
}