peterSweter
peterSweter

Reputation: 653

Lookup weird behaviour

c++ ADL lookup is running when normal lookup fails, in this example i get error about ambiguous g call. I think that normal lookup should only find B::g. Why i am wrong?

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:

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);  }

Upvotes: 0

Views: 54

Answers (1)

user743382
user743382

Reputation:

c++ ADL lookup is running when normal lookup fails

No, argument-dependent lookup always happens alongside normal lookup. That's kind of the point.

Suppose you have

struct S { /* ... */ };
void swap(S &, S &) { /* ... */ }

You wouldn't want standard library functions (defined in the std namespace) to be unable to find this user-defined swap function, would you? They'll always be able to see std::swap, because they themselves are part of the std namespace, yet they may still prefer your ::swap found by ADL.

Upvotes: 4

Related Questions