Make Base
's destructor private, and then make Derived
a friend of Base<Derived>
:
template <class Derived>
class Base {
private: ~Base() = default;
friend Derived;
};
class Derived : public Base<Derived> {
};
This does not actually make doing
class AnotherDerived : public Base<Derived> {
};
illegal, but any attempt to actually construct an AnotherDerived
will fail.