c++ - C++17 会允许嵌套类的前向声明吗?

标签 c++ c++17

不知道在哪里问(如果这是一个不恰当的问题,请随时关闭它)但我在 C++17 提案中没有找到任何关于此的内容,this 也没有。或 this在处理 C++ 的嵌套命名空间添加时提到它。

所以目前这是唯一的选择:

class A 
{
public: 
    class B; //forward-declared INSIDE class/namespace
};

class A::B //defined outside
{
};

这在 C++17 中是否可行?

class A::B; //forward declared NESTED outside of parent class/namespace

class C
{
    A::B *b;
};

然后是这个(1)(似乎是嵌套命名空间定义的提议)

class A::B //definition of A::B without defining A
{

};

或者这个(2)

class A
{
public:
    class A::B
    {

    };
};

或者这个[3]

class A
{
public:
    class B;
};

class A::B
{
};

我怀疑 A::B 的定义而不首先定义 A 可能不起作用(尽管提案似乎允许这样做)。

最佳答案

关于嵌套类的转发声明 P0289R0 的问题有一个提案。 .但是,正如您从最后一个 Trip Report: C++ Standards Meeting in Jacksonville, February 2016 中看到的那样,该提案取决于鼓励进一步工作的提案。我引用委员会的裁决(Emphasis Mine):

This would allow things like X::A* to appear in a header without requiring a definition for X to also appear in the header (forward-declarations of X and X::A will be sufficient). EWG found the use case compelling, because currently a lot of class definitions to appear in headers only because interfaces defined in the header use pointers or references to nested classes of the type. Several details still need to be worked out. (For example, what happens if a definition of X does not appear in any other translation unit (TU)? What happens if a definition of X appears in another TU, but does not define a nested class A? What happens if it does define a nested class A, but it’s private? The answer to some or all of these may have to be “ill-formed, no diagnostic required”, because diagnosing errors of this sort would require significant linker support.)

关于c++ - C++17 会允许嵌套类的前向声明吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36735842/

相关文章:

c++ - 声明在早期友元定义中定义的友元函数

c++ - 采用 OpenCV Mat<double> 并转换为 12 位值的数组。

c++ - 链表 - 新节点指针与新节点变量

c++ - 在命名管道和套接字之间进行简单切换的 IPC

c++ - 返回 `std::string_view` 不包括第一个字符

c++ - 在编译时触发 void constexpr?

C++ 将参数传递给 lambda 函数

c++ - 使用 C++ MFC 进行递归文件搜索?

c++ - chrono 库中的 Clock 和 TrivialClock 概念

c++ - 如何通过多个子构造函数正确地将字符串传递给父级。 C++