为什么 Koening 查找在这里不起作用?
Why is Koening lookup not working here?
参数依赖查找说:
For arguments of class type (including union), the set consists of...
a) The class itself b)...
那为什么printX找不到X呢?
#include<iostream>
using namespace std;
class A {
public:
static const int X = 1;
};
class B {
public:
static void printX(A a)
{
cout << "X is " << X << endl;
}
};
int main(int argc, char** argv)
{
A a;
B::printX(a);
return 0;
}
这不是 ADL 应该做的。 ADL 适用于 unqualified 函数调用,该函数首先在参数类型( s) is/are 已定义。
示例,
namespace N
{
struct A {};
void f(A const &) {}
}
N::A a;
N::f(a); //qualified function call. ADL is not needed.
f(a); //unqualified function call. ADL works here
在上面的示例中,f(a)
从命名空间 N
调用 f()
。由于 f(a)
是不合格的函数调用,ADL 使编译器能够 首先 在命名空间 N
中搜索函数 f
作为参数的类型 a
在 N
.
中定义
希望对您有所帮助。
我想扩展@Nawaz 的回答,它阐明了 "the set consists of... a) The class itself".
的含义
示例程序:
#include <iostream>
void bar(int ) { std::cout << "Came to ::bar(int)\n"; }
namespace foo
{
struct A2;
struct A1
{
friend void bar(A1 const&) { std::cout << "Came to foo::bar(A1 const&)\n"; }
friend void bar(A2 const&) { std::cout << "Came to foo::bar(A2 const&)\n"; }
};
struct A2
{
};
}
struct B
{
friend void bar(B) { std::cout << "Came to ::bar(B)\n"; }
};
int main()
{
foo::A1 a1;
foo::A2 a2;
B b;
int i = 0;
bar(i); // This resolves to the only bar() in the global namespace.
bar(a1); // This resolves to the appropriate overloaded friend function
// defined in foo::A1 because the scopes for looking up the
// function includes class foo::A1
bar(a2); // Error. The search for bar include foo::A2 but not foo::A1.
bar(b); // This resolves to the friend function defined in B because
// the scopes for looking up the function includes class B
}
参数依赖查找说:
For arguments of class type (including union), the set consists of... a) The class itself b)...
那为什么printX找不到X呢?
#include<iostream>
using namespace std;
class A {
public:
static const int X = 1;
};
class B {
public:
static void printX(A a)
{
cout << "X is " << X << endl;
}
};
int main(int argc, char** argv)
{
A a;
B::printX(a);
return 0;
}
这不是 ADL 应该做的。 ADL 适用于 unqualified 函数调用,该函数首先在参数类型( s) is/are 已定义。
示例,
namespace N
{
struct A {};
void f(A const &) {}
}
N::A a;
N::f(a); //qualified function call. ADL is not needed.
f(a); //unqualified function call. ADL works here
在上面的示例中,f(a)
从命名空间 N
调用 f()
。由于 f(a)
是不合格的函数调用,ADL 使编译器能够 首先 在命名空间 N
中搜索函数 f
作为参数的类型 a
在 N
.
希望对您有所帮助。
我想扩展@Nawaz 的回答,它阐明了 "the set consists of... a) The class itself".
的含义示例程序:
#include <iostream>
void bar(int ) { std::cout << "Came to ::bar(int)\n"; }
namespace foo
{
struct A2;
struct A1
{
friend void bar(A1 const&) { std::cout << "Came to foo::bar(A1 const&)\n"; }
friend void bar(A2 const&) { std::cout << "Came to foo::bar(A2 const&)\n"; }
};
struct A2
{
};
}
struct B
{
friend void bar(B) { std::cout << "Came to ::bar(B)\n"; }
};
int main()
{
foo::A1 a1;
foo::A2 a2;
B b;
int i = 0;
bar(i); // This resolves to the only bar() in the global namespace.
bar(a1); // This resolves to the appropriate overloaded friend function
// defined in foo::A1 because the scopes for looking up the
// function includes class foo::A1
bar(a2); // Error. The search for bar include foo::A2 but not foo::A1.
bar(b); // This resolves to the friend function defined in B because
// the scopes for looking up the function includes class B
}