使用指令的歧义名称查找 [英] 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{};
格式错误(请参见第3.3.1/4节).但是,可以通过using-directive引入任何一个的名称:
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?
此代码格式错误吗? VC ++ 如此 以及
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;} }
^
但是, GCC接受 .
谁是对的?
推荐答案
代码格式错误.查找A
时,第7.3.4/6节进入:
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.
这里,名称空间是全局名称空间和N
,而实体是名称空间N::A
和类::A
.
Here, the namespaces are the global namespace and N
, and the entities are the namespace N::A
and the class ::A
.
这篇关于使用指令的歧义名称查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!