我剛剛閱讀這篇文章:Fun with C++ namespaces 作者表示編譯器在遇到第一個時會停止查找重載,這裏使用命名空間。爲什麼編譯器在重載時停止名稱查找?
namespace A
{
void f(int x); // like our std::sqrt(double)
}
namespace B
{
struct S {}; // user-defined type with associated namespace B
void f(S);
void f(int, int);
void test1()
{
using namespace A; // using DIRECTIVE
f(1); // ERROR namespace A is not considered because
// B contains two overloads for 'f'
f(1,2); // OK B::f(int,int)
f(B::S()); // OK B::f(S)
}
void test2()
{
using A::f; // using DECLARATION
f(1); // OK A::f(int)
f(1,2); // ERROR A::f hides B::f(int,int)
f(B::S()); // OK B::f(S) due to ADL!
}
}
namespace C
{
void test3()
{
using namespace A; // using DIRECTIVE
f(1); // OK A::f(int)
f(B::S()); // OK B::f(S) due to ADL!
}
void test4()
{
using A::f; // using DECLARATION
f(1); // OK A::f(int)
f(B::S()); // OK B::f(S) due to ADL!
}
}
爲什麼編譯器應該停止?編輯#1:這個問題確實是一個問題:標準爲什麼這麼說?
感謝您的所有答案!
因爲標準是這樣說的嗎? – PlasmaHH