ADL 何时应用?

2023-12-06

有3个例子:

I.

typedef int foo;

namespace B
{
    struct S
    {
        operator int(){ return 24; }
    };
        int foo(B::S s){ return 0; }
}

int main()
{
    int t=foo(B::S()); //24, ADL does not apply
}

II.

namespace B
{
    struct S
    {
        operator int(){ return 24; }
    };
        int foo(B::S s){ return 0; }
}

int main()
{
    int t=foo(B::S()); //0, ADL applies
}

III.

namespace B
{
    struct S
    {
        operator int(){ return 24; }
    };
        int foo(B::S s){ return 0; }
}
int foo(B::S s){ return 12; }

int main()
{
    int t=foo(B::S()); //error: call of overloaded ‘foo(B::S)’ is ambiguous
                       //ADL applies
}

我不清楚 ADL 查找适用的实际条件是什么?我需要参考标准来描述它。


这个标准段落澄清了,甚至有一个非常像你的第一个例子的例子。

3.4.1/3:

查找用作后缀表达式函数调用的过程在 3.4.2 [basic.lookup.argdep] 中描述。 [Note:为了确定(在解析期间)表达式是否是后缀表达式对于函数调用,应用通常的名称查找规则。 3.4.2 中的规则对表达式的语法解释没有影响。例如,

typedef int f;
namespace N {
  struct A {
    friend void f(A &);
    operator int();
    void g(A a) {
      int i = f(a);    // f is the typedef, not the friend
                       // function: equivalent to int(a)
    }
  };
}

因为表达式不是函数调用,所以依赖于参数的名称查找 (3.4.2) 不适用,并且友元函数f没有找到。 -end note]

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ADL 何时应用? 的相关文章

随机推荐