If your class contains code that says:
T* pT = 0; Base *pB = pT;
Then there will be a compiler error if T not compatible with the Base assignment.
This type of validation is formalized in C ++ 11, so you do not need to write it manually and can receive useful error messages:
#include <type_traits> template<typename T> class Base { public: void do_something() { static_assert( std::is_base_of<Base, T>::value, "T must be derived from Base"); } }; class B : public Base<B> { }; int main() { B b; b.do_something(); }
As for the fact that the Base type parameter is exactly the class that derives from it, this seems conceptually erroneous. A class acting as a base class cannot "talk" about which type inherits it. It can be inherited more than once through multiple inheritance or is completely absent.
source share