C++ 奇怪的行为

C++ 奇怪的行为,c++,c++11,C++,C++11,当正常查找失败时,c++ADL查找正在运行,在本例中,我得到了关于不明确g调用的错误。我认为普通的查找应该只找到B::g。为什么我错了 namespace A { struct X{ }; struct Y{ }; void g(X){ } void h(){ } } namespace B { void g(A

当正常查找失败时,c++ADL查找正在运行,在本例中,我得到了关于不明确g调用的错误。我认为普通的查找应该只找到B::g。为什么我错了

namespace A
    {
        struct X{
        };

        struct Y{
        };

        void g(X){
        }
        void h(){

        }

    }

    namespace B
    {
        void g(A::X x);
        void g(A::X x) { g(x);}

    }
错误:

error: call to 'g' is ambiguous
    void g(A::X x) { g(x);  }
                     ^
c0202.cpp:9:10: note: candidate function
    void g(X){
         ^
c0202.cpp:20:10: note: candidate function
    void g(A::X x) { g(x);  }
正常查找失败时,c++ADL查找正在运行

否,参数相关的查找总是与普通查找一起进行。这就是重点

假设你有

struct S { /* ... */ };
void swap(S &, S &) { /* ... */ }
您不希望标准库函数(在
std
命名空间中定义)无法找到此用户定义的
swap
函数,是吗?他们总是能够看到
std::swap
,因为他们自己是
std
命名空间的一部分,但他们可能仍然更喜欢ADL找到的
::swap

正常查找失败时,c++ADL查找正在运行

否,参数相关的查找总是与普通查找一起进行。这就是重点

假设你有

struct S { /* ... */ };
void swap(S &, S &) { /* ... */ }
您不希望标准库函数(在
std
命名空间中定义)无法找到此用户定义的
swap
函数,是吗?他们总是能够看到
std::swap
,因为他们自己是
std
命名空间的一部分,但他们可能仍然更喜欢ADL找到的
::swap