下面的代码是否有效?
struct A
{
struct nested;
};
struct B : public A {};
struct B::nested {};
gcc 接受它,但 clang 拒绝它并出现以下错误:
test.cpp:8:14: error: no struct named 'nested' in 'B'
class B::nested {};
~~~^
最佳答案
是的,它是无效的。这是引述。
第 9 节第 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.
在您的示例中,class-head-name 是标记 B::nested
,它包含 nested-name-specifier B::
和类名 嵌套
。 class-specifier 是整个 struct B::nested {
... }
。
关于c++ - 基类嵌套类的外联定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18818315/