c++ - 基类嵌套类的外联定义

标签 c++ inheritance gcc clang nested-class

下面的代码是否有效?

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/

相关文章:

c++ - Visual C++ 如何为 Delphi 导出 x64 DLL 函数

c++ - 为什么通过同一个 COM 对象的不同接口(interface)检索到的 IUnknown* 指针具有相同的值?

java - 如何从子类调用重写的父类方法?

c++ - 排序函数,它采用指向接口(interface)类的指针 vector

linux - 我怎样才能更改 gcc 在链接/bin/ld 时使用的库?

c# - 从头开始——我应该使用 (MS Visual Studio) C# 还是 (MS Visual Studio) C++?

c++ - 是否允许 std::vector::size() 要求非平凡的计算?什么时候有意义?

language-agnostic - 更喜欢组合而不是继承?

docker - Alpine 编译失败中的 Gdal 安装 - "error: command ' gcc' 失败,退出状态为 1"

c - 为什么gcc对函数中的局部变量重新排序?