C++17 是否允许嵌套 类 的前向声明?

Will C++17 allow forward declaration of nested classes?

不知道在哪里问(如果这是一个不合适的问题,请随意关闭它)但是我在 C++17 提案中没有找到任何关于这个的内容,this or 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
{
};

我怀疑没有首先定义 AA::B 的定义可能行不通(尽管提案似乎允许这样做)。

有一个关于标题为 Forward declarations of nested classes P0289R0. However as you can see from the last Trip Report: C++ Standards Meeting in Jacksonville, February 2016 的提案,该提案与鼓励进一步工作的提案悬而未决。我引用了委员会的判决(强调我的观点):

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.)

恕我直言,缺乏对 classes 进行前向减速的能力是 C++ 语言定义中的一个主要漏洞,这导致人们使用 void*,而前向引用会更安全。

这是一个使用命名空间的解决方法:

  1. 展平您需要 pre-declare
  2. 的 class 结构
  3. 使用命名空间分隔代码
  4. 使用命名空间进行向前减速
namespace ns1 {
namespace ns2 {  

typedef class C * cptr_t; // declare both class and a pointer type
}}

ns1::ns2::C *cp;      // use forward deceleration of the class
ns1::ns2::cptr_t cp;  // use the typedef

此解决方法无法正确解决问题,但在某些情况下可能有所帮助。