Reason why forward declarations of a nested class are not allowed?

Example:

// can't forward declare with class Foo::Bar // actual class class Foo { public: class Bar // or enum Bar { } }; 

I agree that this is prohibited by current C ++ standards, but I couldnโ€™t come up with a good reason not to allow this, especially with C ++ 0x, now we can forward declare enumerations. I would suggest that the argument against this would be that if we declare a nested class that turns out to be private, it will not be allowed. But that would not be too different to say that you need to declare a class in the namespace, and then declare it a nested class of the outer class. The compiler would simply give an error (perhaps the error message along the line of the previous declaration does not match this declaration).

So why is this really not allowed?

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

** Given that the C ++ standards committee recognized the advantage of using forward declarations to reduce dependencies and compilation time by introducing direct declaration of enumerations in C ++ 0x, this is certainly part of the same, isnโ€™t it?

+7
source share
3 answers

The reason this is not in the language is simply because no one came up with a good suggestion on how to incorporate it into the language. The functions do not include themselves, someone should write this and explain the advantages and that there are no (or very minor) disadvantages.

The argument for the non-transmitting declaration enums ( enum x; ) was simply that the compiler cannot select the correct size for the enumeration variables until it sees how many values โ€‹โ€‹exist. This problem has been resolved by allowing you for the compiler ( enum x : int; ). It has also been implemented and shown to work correctly before introducing the standard.

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

+9
source

Nested classes can certainly be declared forward:

 class Foo { public: class Bar; }; class Foo::Bar { }; 
+3
source

How can you redirect a declaration of a nested class? To do this, you must declare the class in which it is nested. After that:

 class ClassName { 

You started the announcement of the full class. You cannot partially declare a class; it is either all here or none of it here. So, what kind of goodness will the inner class declare if the outer class has to be fully declared?

The only reason the listing has not been announced so far is because their size was determined by their counters. This is why you must give them an explicit size in order to post your ad.

+2
source

All Articles