Why does this CRTP-based clone pattern correctly return a Derived pointer without each subclass rewriting clone()?
struct Base { virtual Base* clone() const = 0; virtual ~Base() = default; };
template <class D>
struct Cloneable : Base {
Base* clone() const override {
return new D(static_cast<const D&>(*this));
}
};
struct Foo : Cloneable<Foo> { int x; };