Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 947
Columbo Avatar asked Apr 26 '15 21:04

Columbo


1 Answers

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.

like image 136
Columbo Avatar answered Nov 13 '22 01:11

Columbo