c++ 依赖参数的函数查找

来源:互联网 发布:linux 压缩文件夹 gz 编辑:程序博客网 时间:2024/06/16 02:30

依赖参数的函数查找

依赖参数的函数查找(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    }}
0 0
原创粉丝点击