gpt4 book ai didi

c++ - 什么时候应用 ADL?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:49:08 25 4
gpt4 key购买 nike

有3个例子:

我.

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
}

二.

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
}

三.

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 查找的实际条件是什么?我需要引用标准来描述它。

最佳答案

This Standard paragraph 澄清,甚至有一个非常像你的第一个例子的例子。

3.4.1/3:

The lookup for an unqualified name used as the postfix-expression of a function call is described in 3.4.2 [basic.lookup.argdep]. [Note: For purposes of determining (during parsing) whether an expression is a postfix-expression for a function call, the usual name lookup rules apply. The rules in 3.4.2 have no effect on the syntactic interpretation of an expression. For example,

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)
}
};
}

Because the expression is not a function call, the argument-dependent name lookup (3.4.2) does not apply and the friend function f is not found. -end note]

关于c++ - 什么时候应用 ADL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32394226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com