Re: covariant return types with CRTP
Unfortunately, during the instantiation of clonable<D,B> the class D
is still an incomplete type, so the the compiler can't tell that the
return types are covariant.
There have been numerous discussions about this, just try to seach
clc++m. The best solution seems to be
I'm sorry for not searching first.
I found your reply from the search result. (http://tinyurl.com/pyvms)
In that reply, you wrote, "It may seem that the compiler _could_
already know this,"
Is this not knowing problem caused from Standard or
compiler-implemetation thing? I could not find this case in 14.7.1/1.
template <typename Derived,typename Base>
class Clonable : Base{
public:
Derived* clone() const{
return static_cast<Derived*>(this->do_clone());
}
private:
virtual Clonable* do_clone() const{
return new Derived(static_cast<const Derived&>(*this));
}
};
class ClonableBase{
public:
ClonableBase* clone() const{
return do_clone();
}
private:
virtual ClonableBase* do_clone() const=0;
};
You get pretty much everything you need including
pseudo-covariant returns. This is, however, as good as it gets.
This is good enough to me. I couldn't think about using name-hiding.
;-)
Thanks,
iwongu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]