C++ class 声明中的限定名称
qualified names in C++ class declaration
根据this page,class 名称可以是"optionally qualified"。因此,我希望编译以下代码:
struct ::globalSt {};
在 MSVC 2013u4 中,出现错误:
Error 1 error C2039: 'globalSt' : is not a member of '`global namespace''
我是不是误解了引用,或者这是一个 MSVC 错误?
要使其正常工作,class 必须已经声明。如果您在代码中的某处放置 struct globalst;
定义之前,它将正常工作。
例如,如果你这样做
struct MyStruct;
然后做
struct ::MyStruct {};
它应该编译。
已使用 MSVC 2013 进行测试。
如果您使用限定名称定义 class,则该名称必须先前已声明。 [class]/11
If a class-head-name contains a nested-name-specifier, the class-specifier shall refer to a class that was
previously declared directly in the class or namespace to which the nested-name-specifier refers, or in an
element of the inline namespace set (7.3.1) of that namespace (i.e., not merely inherited or introduced by
a using-declaration), and the class-specifier shall appear in a namespace enclosing the previous declaration.
In such cases, the nested-name-specifier of the class-head-name of the definition shall not begin with a
decltype-specifier.
根据this page,class 名称可以是"optionally qualified"。因此,我希望编译以下代码:
struct ::globalSt {};
在 MSVC 2013u4 中,出现错误:
Error 1 error C2039: 'globalSt' : is not a member of '`global namespace''
我是不是误解了引用,或者这是一个 MSVC 错误?
要使其正常工作,class 必须已经声明。如果您在代码中的某处放置 struct globalst;
定义之前,它将正常工作。
例如,如果你这样做
struct MyStruct;
然后做
struct ::MyStruct {};
它应该编译。
已使用 MSVC 2013 进行测试。
如果您使用限定名称定义 class,则该名称必须先前已声明。 [class]/11
If a class-head-name contains a nested-name-specifier, the class-specifier shall refer to a class that was previously declared directly in the class or namespace to which the nested-name-specifier refers, or in an element of the inline namespace set (7.3.1) of that namespace (i.e., not merely inherited or introduced by a using-declaration), and the class-specifier shall appear in a namespace enclosing the previous declaration. In such cases, the nested-name-specifier of the class-head-name of the definition shall not begin with a decltype-specifier.