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:
不规范的(见§3.3.1/4)。但是,可以通过使用指令来介绍其中任何一个的名称:
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:
这是不规范的代码吗?VC++ +也这么认为,还有叮当声:
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.
然而,GCC接受它。
Who is right?
谁是正确的?
1 个解决方案
#1
6
The code is ill-formed. When looking up A
, §7.3.4/6 steps in:
不规范的代码。当查找,§7.3.4/6步骤:
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
.
在这里,名称空间是全局名称空间和N,实体是名称空间N::A和类:::A。
#1
6
The code is ill-formed. When looking up A
, §7.3.4/6 steps in:
不规范的代码。当查找,§7.3.4/6步骤:
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
.
在这里,名称空间是全局名称空间和N,实体是名称空间N::A和类:::A。