subclass - Nested subclasses in C++ -
i'm trying make nested class subclass of parent:
struct x { struct y : public x {}; };   unfortunately, doesn't seem allowed in c++, g++ produces error
error: invalid use of incomplete type 'struct x'
however, actual code has x templated class:
template<typename t> struct x { struct y : public x {}; };   i same message, time it's warning:
warning: invalid use of incomplete type 'struct x< t >'
my question is: why former case illegal, while templated case gives warning?  templated version works expect (i can create instances of x<t>::y, cast them x<t>, , on), warning mean shouldn't use it?  problems can expect run if ignore warning?
technically, far compiler concerned, layout of base (x) not need known until template (x) instanciated. , template (x) may not istantiated before defined. @ point, it's layout known.
simplest way error template try istantiate y inside x:
template<typename t> struct x {     struct y : public x {};     y y; };   in earlier versions of complier, there no warning in case show, added @ point. here discussion gcc bugtracker whether warning spurious. there uncertainty whether allowed standard conclusion not allowed.
so, neither case allowed standard, gcc keeps working latter, because can.
yam marcovic shows how x::y can defined in standard compliant way. analoguously identical example shown in gcc bugtracker.
Comments
Post a Comment