以下代碼位於兩個源文件中。爲什麼找到f1但f2不是?
第一:
namespace A {
// two friends; neither is declared apart from a friend declaration
// these functions implicitly are members of namespace A
class C {
friend void f2(); // won’t be found, unless otherwise declared
friend void f1(const C&); // found by argument-dependent lookup
};
}
int main()
{
A::C obj;
f1(obj); // ok: find A::f through the friend declaration in A::C
A::f2(); // no member named f2 in namespace A
}
和第二:
#include <iostream>
namespace A {
class C;
void f1(const C&) {
std::cout << 1;
}
void f2() {
std::cout << 2;
}
}
代碼的第一片選自C複製++引物,唯一的區別是C++底漆呼叫F2()無前綴命名空間。第二件是我的補充。我想知道現在F1和F2隱含的命名空間的成員,爲什麼A :: F2()仍然是錯誤的,而F1(OBJ)可以通過ADL找到?
我第一次遇到ADL ...我的反應大概會是「OHMYGODWHY」。 – Cubic
@Cubic這樣'std :: cout <<'x';'起作用。沒有ADL你會寫是'使用std ::運算符<<'或'的std ::運算符<<(標準::法院, 'X');'等 –