class本身的ADL
ADL of class itself
根据标准参数相关查找添加到搜索集 class 如果我们有 class 类型作为函数参数:
If T
is a class type (including unions), its associated classes are: the class itself; the class of which it is a member, if any; and its direct and indirect base classes.
如果是这样,为什么在此上下文中找不到 foo
:
class X{
public:
void foo(const X& ref){std::cout<<"Inner class method\n";}
};
int main(){
X x;
foo(x);
}
不应该将 adp 添加到搜索集 X
class 并在其中查找 foo
?
foo
不是自由函数,它是一个 class 方法,因此您需要从 class
的实例中调用它
X a;
X b;
a.foo(b);
请注意,这里使用了 ADL,因此您不必写出以下内容,它也可以编译,但由于 ADL
不必要地冗长
a.X::foo(b);
不是,因为foo
是成员函数,不是可以通过ADL找到的自由函数。
也许你的意思是:
static void foo(const X& ref){std::cout<<"Inner class method\n";}
这也不会通过ADL找到;您需要像 X::foo(b)
.
这样限定通话
有关关联 classes 的子句适用于在 class 中声明的友元函数。例如:
class X{
public:
friend void foo(const X& ref){std::cout<<"Inner class method\n";}
};
foo
是一个non-member函数,但是只能通过ADL找到
根据标准参数相关查找添加到搜索集 class 如果我们有 class 类型作为函数参数:
If
T
is a class type (including unions), its associated classes are: the class itself; the class of which it is a member, if any; and its direct and indirect base classes.
如果是这样,为什么在此上下文中找不到 foo
:
class X{
public:
void foo(const X& ref){std::cout<<"Inner class method\n";}
};
int main(){
X x;
foo(x);
}
不应该将 adp 添加到搜索集 X
class 并在其中查找 foo
?
foo
不是自由函数,它是一个 class 方法,因此您需要从 class
X a;
X b;
a.foo(b);
请注意,这里使用了 ADL,因此您不必写出以下内容,它也可以编译,但由于 ADL
不必要地冗长a.X::foo(b);
不是,因为foo
是成员函数,不是可以通过ADL找到的自由函数。
也许你的意思是:
static void foo(const X& ref){std::cout<<"Inner class method\n";}
这也不会通过ADL找到;您需要像 X::foo(b)
.
有关关联 classes 的子句适用于在 class 中声明的友元函数。例如:
class X{
public:
friend void foo(const X& ref){std::cout<<"Inner class method\n";}
};
foo
是一个non-member函数,但是只能通过ADL找到