covariant return types with CRTP
Hi,
I tried to make 'clonable' base class for automatic clone() method
implementation. The first trial is the following.
template <class T>
struct clonable
{
T* clone() const {
T const* d = static_cast<T const*>(this);
return new T(*d);
}
};
struct B
{
virtual ~B() {}
virtual B* clone() const = 0;
};
struct D : B, clonable<D>
{
};
And it gave me an error that the D can't be new-ed because its 'clone'
is abstract yet. After some thought, I agreed with my compiler.
So I changed the code like the following.
template <class T, class U>
struct clonable : U
{
T* clone() const { // (8)
T const* d = static_cast<T const*>(this);
return new T(*d);
}
};
struct B
{
virtual ~B() {}
virtual B* clone() const = 0; // (17)
};
struct D : clonable<D, B>
{ // (21)
};
But this change does not help. The compile error message is,
: In instantiation of `clonable<D, B>':
:21: instantiated from here
:8: conflicting return type specified for `T* clonable<T, U>::clone()
const [with T = D, U = B]'
:17: overriding `virtual B* B::clone() const'
I think that this should be treated as a covariance return type case
according to the 10.3.5 of the C++98.
If I change the line (8) from "T* clone() const {" to "U* clone() const
{", it compiles cleanly. But I can't get the D* from it with this.
Am I missing some Standard-thing?
Thanks,
iwongu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]