c++language-lawyername-lookupqualified-name

Ambiguous name lookup with using-directive


It's not allowed to put a namespace and a class with the same name into one declarative region, i.e.

namespace A {}
class A{};

is ill-formed (see §3.3.1/4). However, one can introduce the name of either one via a using-directive:

namespace N { namespace A {int i;} }

struct A {static int i;};

using namespace N;

int i = A::i; // The global struct, or namespace N::A?

Is this code ill-formed? VC++ thinks so, as well as Clang:

main.cpp:7:9: error: reference to 'A' is ambiguous
int i = A::i;
        ^
main.cpp:3:8: note: candidate found by name lookup is 'A'
struct A {static int i;};
       ^
main.cpp:1:25: note: candidate found by name lookup is 'N::A'
namespace N { namespace A {int i;} }
                        ^

However, GCC accepts it.

Who is right?


Solution

  • The code is ill-formed. When looking up A, §7.3.4/6 steps in:

    If name lookup finds a declaration for a name in two different namespaces, and the declarations do not declare the same entity and do not declare functions, the use of the name is ill-formed.

    Here, the namespaces are the global namespace and N, and the entities are the namespace N::A and the class ::A.