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.
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.
Secondly is there a way to achieve what I want, which is to let X know the
definition of a type defined in any class derived from X, by passing the
derived type as a template parameter to X (aka the curiously recurring
template pattern).
Thanks,
john
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.
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.
Secondly is there a way to achieve what I want, which is to let X know the
definition of a type defined in any class derived from X, by passing the
derived type as a template parameter to X (aka the curiously recurring
template pattern).
Thanks,
john
Comment