The following works in VC10 using static_assert. I've just seen this used and haven't really dug around much into what static_assert actually does - perhaps someone else can answer that.
#include <type_traits> class Base { }; class Derived : public Base { }; class SomeRandomClass { }; template<typename T> class A { static_assert(std::tr1::is_base_of<Base, T>::value, "T not derived from Base"); }; int _tmain(int argc, _TCHAR* argv[]) { argc; argv; // // This will compile A<Derived> a; // // This will throw a compilation error A<SomeRandomClass> b; return 0; }
The compiler output being:
1>d:\temp\aaa\aaa\aaa.cpp(25): error C2338: T not derived from Base 1> d:\temp\aaa\aaa\aaa.cpp(41) : see reference to class template instantiation 'A<T>' being compiled 1> with 1> [ 1> T=SomeRandomClass 1> ]