How to Forward Declare an Inner Class

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.

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

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

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.

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.

Forward declare other nested struct in C++

When you do

virtual void accept(struct visitor& v);

you forward declare visitor in the
smallest namespace or block scope that contains the declaration. That means visitor is scoped to the global namespace in this case.specials`'s

void accept(visitor& v)

on the other hand is grabbing super::visitor. Since those are different types the compiler is correct.

What you need to do is move the forward declaration of visitor into super's scope like

struct super
struct visitor;
struct base
virtual void accept(visitor& v);
virtual ~base() {}

struct visitor
virtual void visit(base& b);
virtual ~visitor() {}

struct special : public base
void accept(visitor& v) override { v.visit(*this); }

int main() {}

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*);

Related Topics

Leave a reply