John Harrison said:
The following code does not compile
template <class Derived>
struct X
{
typedef typename Derived::type type;
};
struct Y : public X<Y>
{
typedef int type;
};
int main()
{
}
gcc and VC++ 7 give unhelpful error messages but Comeau C++ says 'incomplete
type is not allowed' pointing at the typedef in X.
Hi John,
Interesting thing - to me it seems that this is a problem regarding the
order of how and when the template parameters are substituted and when
typedefs are resolved. However, I´m not an expert on compiler techniques so
I´d also be interested if somebody could shed some more light onto this,
please.
Can someone explain to my why this should be a problem? I'm presuming that
this kind of construct is difficult or impossible to compile for some
reason.
[SNIP]
There is a way to circumvent this (IMHO probably correct behavior) by using
templates only instead of typedefs.
template <class Derived, typename DerivedType>
struct X
{
X() {};
~X() {};
void test() { cout << typeid( type ).name() << endl; };
typedef typename DerivedType type;
};
template<typename InternalType>
struct Y : public X<Y, InternalType>
{
Y() {};
~Y() {};
};
int main()
{
Y<int> MyObj;
MyObj.test();
return 1;
}
HTH
Chris