Forward declaration of nested types/classes in C++
You can't do it, it's a hole in the C++ language. You'll have to un-nest at least one of the nested classes.
How do I forward declare an inner class?
This is simply not possible. You cannot forward declare a nested structure outside the container. You can only forward declare it within the container.
You'll need to do one of the following
- Make the class non-nested
- Change your declaration order so that the nested class is fully defined first
- Create a common base class that can be both used in the function and implemented by the nested class.
Will C++17 allow forward declaration of nested classes?
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.)
Forward declaration within a class (not a nested class)
Now this might be an elaborated type specifier, but I'm not sure about that.
It is. In fact, class A;
is a declaration to uses an elaborate class specifier.
but I'm afraid it may cause scoping issues that I'm currently unaware of
The only scoping related point you should be aware of is that if the elaborate type specifier (that is not a declaration by itself) is the first time class is referenced, the behavior is the same as if you introduced a forward declaration to the nearest namespace or block scope. So for instance
namespace N {
class foo {
class bar {
void func(class baz*);
};
};
};
... is the same as
namespace N {
class baz;
class foo {
class bar {
void func(baz*);
};
};
};
C++ nested class/forward declaration issue
You can't forward-declare a nested class like that.
Depending on what you're trying to do, maybe you can use a namespace rather than a class on the outer layer. You can forward-declare such a class no problem:
namespace Outer {
struct Inner;
};
Outer::Inner* sweets; // Outer::Inner is incomplete so
// I can only make a pointer to it
If your Outer absolutely must be a class, and you can't shoe-horn it into a namespace, then you'll need for Outer to be a complete type in the context where you forward declare Inner.
class Outer
{
class Inner; // Inner forward-declared
}; // Outer is fully-defined now
Outer yes; // Outer is complete, you can make instances of it
Outer::Inner* fun; // Inner is incomplete, you can only make
// pointers/references to it
class Outer::Inner
{
}; // now Inner is fully-defined too
Outer::Inner win; // Now I can make instances of Inner too
C++ Nested classes forward declaration error
Define the constructor for A AFTER the definition of struct B.
Forward Declaration necessary for nested friend classes
A class declared to be friend
is looked up in the context of the friend
declaration. If one is found, this class is made a friend
. If none is found the class is declared at the closest namespace level and this class is made a friend
. The friend
declaration will not introduce a nested class name. It would make a found nested class a friend
, though.
Forward declaration of template class nested inside template class
Try the following
template<typename T>
template <int N>
class Outer<T>::Inner {};
According to the C++ Standard (14.5.2 Member templates)
1 A template can be declared within a class or class template; such a
template is called a member template. A member template can be defined
within or outside its class definition or class template definition. A
member template of a class template that is defined outside of its
class template definition shall be specified with the
template-parameters of the class template followed by the
template-parameters of the member template.
Related Topics
Convert Python Program to C/C++ Code
How to Properly Delete Nodes of Linked List in C++
Can Modern X86 Hardware Not Store a Single Byte to Memory
Differences Between Std::Make_Unique and Std::Unique_Ptr With New
How to Sort a Std::Vector by the Values of a Different Std::Vector
What Are the New Features in C++17
Why Is My Program Slow When Looping Over Exactly 8192 Elements
How to Remove Unused C/C++ Symbols With Gcc and Ld
Difference of Keywords 'Typename' and 'Class' in Templates
Is (4 ≫ Y ≫ 1) a Valid Statement in C++? How to Evaluate It If So
What Are All the Member-Functions Created by Compiler For a Class? Does That Happen All the Time
Unmangling the Result of Std::Type_Info::Name
Why Does C++ Disallow Anonymous Structs