Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reason for not allowing forward declarations of nested class?

Tags:

c++

standards

Example:

// can't forward declare with class Foo::Bar

// actual class
class Foo
{
public:
    class Bar // or enum Bar
    {
    }
};

I accept that this is not allowed under the current C++ standards, but I couldn't come up with a good reason of not allowing it though, especially with C++0x, we're now able to forward declare enums. I would imagine an argument against it would be if we foward declare nested class which turns out to be private, it wouldn't be allowed. But this wouldn't be too different to say forward declaring a class in a namespace, and then declaring it a nested class of an outer class. The compiler would simply give an error (perhaps an error message along the line of previous declaration doesn't match this declaration).

So why is it really not allowed then?

In other words (by James McNellis), "why is class Foo::Bar; without providing a definition for Foo or Bar not allowed?"

** Given that the C++ standards committee recognised the benefit of using forward declarations to reduce dependencies and compile time by introducing forward declaration of enums in C++0x, surely this is part of the same thing isn't it?

like image 360
Zach Saw Avatar asked Jul 13 '11 05:07

Zach Saw


People also ask

Can you forward declare a nested class?

You cannot forward declare a nested structure outside the container. You can only forward declare it within the container. Create a common base class that can be both used in the function and implemented by the nested class.

When the forward declaration is required while declaring function?

Forward declaration is used in languages that require declaration before use; it is necessary for mutual recursion in such languages, as it is impossible to define such functions (or data structures) without a forward reference in one definition: one of the functions (respectively, data structures) must be defined ...

Why should I use forward declarations?

A forward declaration allows us to tell the compiler about the existence of an identifier before actually defining the identifier. In the case of functions, this allows us to tell the compiler about the existence of a function before we define the function's body.

Does C support forward declaration?

In C/C++, Visual Assist can add a forward declaration for a referenced symbol, e.g. a pointer to a class, to make an unknown symbol known. The forward declaration, if sufficient, typically reduces compile time relative to a comparable include directive.


1 Answers

The reason that it is not in the language is simply that nobody has come up with a good proposal on how to include it in the language. Features don't include themselves, someone has to write it up and explain the advantages and that there are no (or very minor) disadvantages.

The argument for not forward declaring enums (enum x;) was simply that the compiler cannot select the proper size for enum variables until it has seen how many values there are. This problem was solved by allowing you to decide for the compiler (enum x : int;). This has also been implemented and shown to work properly, before entering the standard.

See http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2764.pdf

like image 89
Bo Persson Avatar answered Sep 20 '22 14:09

Bo Persson