Qqwy
Qqwy

Reputation: 5649

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?

Upvotes: 2

Views: 66

Answers (2)

Laura Maftei
Laura Maftei

Reputation: 1863

According to the section 3.4.2 of the standard

For each argument type T in the function call, there is a set of zero or more associated namespaces

So both namespaces Foo and Bar will be in the set of associated namespace. As function test is found in both, it will be ambiguous.

Upvotes: 2

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

Upvotes: 7

Related Questions