2015-07-18 63 views
16

请考虑以下程序。根据C++标准是它良好的成立(该标准的相关部分引用所需的):尝试通过使用声明来定义名称空间成员

namespace X { extern int i; } 

namespace N { using X::i; } 

int N::i = 1; 

int main() {} 

我得到不同的编译器不同的结果。我试图找出什么编译器我应该立案的bug报告:

  • 锵:提供了以下编译器错误:没有名为“我”在命名空间“N”

  • GCC成员和Visual C++编译它没有错误。

为便于比较,下面给出编译错误,与所有三个编译器:

namespace X { void f(); } 

namespace N { using X::f; } 

void N::f() {}; 

int main() {} 
+1

有趣,VS2013编译,但IntelliSence说''错误:命名空间“N”没有真正的成员“我”# – AlexD

+0

我想答案在这里:[链接](http://stackoverflow.com/questions/6175705/scope-of-using-declaration-within-a-namespace) – Jorj

+0

@Supremum你可以从https://isocpp.org/std/the-standard获得自己的C++标准副本。 – Coder

回答

11

当前工作草案N4527,[8.3p1]:

[...] When the declarator-id is qualified, the declaration shall refer to a previously declared member of the class or namespace to which the qualifier refers (or, in the case of a namespace, of an element of the inline namespace set of that namespace (7.3.1)) or to a specialization thereof; the member shall not merely have been introduced by a using-declaration in the scope of the class or namespace nominated by the nested-name-specifier of the declarator-id. [...]

所以,绝对病态的; GCC和MSVC是错误的。

相关问题