【发布时间】:2015-04-26 21:48:15
【问题描述】:
不允许将命名空间和同名的类放在一个声明区域中,即
namespace A {}
class A{};
is ill-formed(见 §3.3.1/4)。但是,可以通过 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++thinks so,还有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 accepts it。
谁是对的?
【问题讨论】:
-
GCC 选择
N::A::i。想知道为什么。 -
@Barry 我也很困惑,因为我希望它更喜欢更肤浅的声明,即“更接近”查找点。
标签: c++ language-lawyer name-lookup qualified-name