gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 22:14:59 26 4
gpt4 key购买 nike

不知道在哪里问(如果这是一个不恰当的问题,请随时关闭它)但我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com