Qqwy Qqwy - 1 year ago 79
C++ Question

C++ Koenig(Argument-Dependent) Lookup: What if two namespaces functions in different namespaces have the same argument types?

What happens if there is a

Foo::test(Foo::A &a, Bar::B &b, C &c);
and a

Bar::test(Foo::A &a, Bar::B &b, C &c);

Are the namespaces of the arguments considered in-order by the compiler (the first argument taking precedence for the argument-dependent-lookup), or is this considered to be ambiguous?

Answer Source

It will be ambiguous. The overload set contains two equally valid overloads:

namespace Bar
    struct B;

namespace Foo
    struct A{};
    void test(A& , Bar::B&, int){}

namespace Bar
    struct B{};
    void test(Foo::A& , B&, int){}

int main() {
    Foo::A a; Bar::B b;
    test (a, b, 0);

    return 0;

results on gcc in:

prog.cpp: In function 'int main()':
prog.cpp:21:15: error: call of overloaded 'test(Foo::A&, Bar::B&, int)' is ambiguous test (a, b, 0);
^ prog.cpp:10:7: note: candidate: void Foo::test(Foo::A&, Bar::B&, int) void test(A& , Bar::B&, int){} ^ prog.cpp:16:7: note: candidate: void Bar::test(Foo::A&, Bar::B&, int) void test(Foo::A& , B&, int){}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download