Not sure where to ask (feel free to close this if it is an inappropriate question) but I have not found anything on this specifically in C++17 proposals, neither this or this mentions it when dealing with the nested namespace addition to C++.
So currently this is the only option:
class A { public: class B; //forward-declared INSIDE class/namespace }; class A::B //defined outside { };
Will this be possible in C++17?
class A::B; //forward declared NESTED outside of parent class/namespace class C { A::B *b; };
and then either this (1) (as seems to be the proposal of nested namepsace definitions)
class A::B //definition of A::B without defining A { };
or this (2)
class A { public: class A::B { }; };
or this [3]
class A { public: class B; }; class A::B { };
I suspect the definition of A::B
without defining A
first might not work though (although the proposal seems to allow it).
You cannot forward declare a nested structure outside the container. You can only forward declare it within the container.
Classes. In some object-oriented languages like C++ and Objective-C, it is sometimes necessary to forward-declare classes. This is done in situations when it is necessary to know that the name of the class is a type, but where it is unnecessary to know the structure.
Nested Classes in C++The nested class is also a member variable of the enclosing class and has the same access rights as the other members. However, the member functions of the enclosing class have no special access to the members of a nested class. A program that demonstrates nested classes in C++ is as follows.
A base class MUST be declared (not forward declared) when being declared as a based class of another class. An object member MUST be declared (not forward declared) when being declared by another class, or as parameter, or as a return value.
There's a proposal on the issue titled Forward declarations of nested classes
P0289R0. However as you can see from the last Trip Report: C++ Standards Meeting in Jacksonville, February 2016, this proposal was pendent to proposals for which further work is encouraged. I'm quoting the verdict of the committee (Emphasis Mine):
This would allow things like
X::A*
to appear in a header without requiring a definition forX
to also appear in the header (forward-declarations ofX
andX::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 ofX
does not appear in any other translation unit (TU)? What happens if a definition ofX
appears in another TU, but does not define a nestedclass 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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With