Ioannis said:
May you provide an example where this would be useful? I think you can't forward declare
the contained class for valid reasons.
Given the existing convention that iterators are sub-classes of the
container over which they iterate, then
class Container
{
//...
class iterator
{
//...
}
//...
}
is not an unlikely scenario.
Under certain circumstances, it can be necessary for the iterator to
need to be a friend of the class contained in the container (don't ask,
it just is), and there is no way to order the declarations and stuff to
enable avoid forward declaring either the iterator class (which you
can't) or a proxy class which just wraps the iterator (which is a real
waste of time (and extra code to maintain, etc)). Changing the order of
declaration isn't applicable for various reasons.
How the compiler could check whether the
declaration is correct, before reaching the class definition?
In answer to your 2nd question - presumably the compiler determines
that the forward declaration is valid in exactly the same way it
determines that a forward declaration of class is valid. i.e. it
doesn't, it assumes that it is, until it finds another declaration of
Container::iterator which isn't a class. For instance, this is going to
be considered valid:
class Wibble;
std:
stream &std:
perator<<(str:
stream &, Wibble const &w);
until the compiler finds a declaration of Wibble which isn't a class. I
can't see why a subclass in here would confuse the compiler any more
(or less).
class Wibble;
class Wibble::Sub;
std:
stream &std:
perator<<(str:
stream &, Wibble::Sub const
&w);
I thought perhaps if Wibble::Sub was private, but even then the
implementation is going to need to get hold of the real definition of
Wibble::Sub and the compiler will error at that point (as indeed it
will if Wibble isn't a class, etc, etc). I'd really like to see the
reason why this isn't permitted.