fork download
  1. #include <iostream>
  2. #include <type_traits>
  3. using namespace std;
  4.  
  5. class A{};
  6. class B:public A {};
  7.  
  8. template <bool Inherited>
  9. class Checker; // error, not implemented if not inherited
  10.  
  11. template<>
  12. class Checker<true> { /* empty*/ };
  13.  
  14.  
  15. template <typename T>
  16. class Test
  17. {
  18. private:
  19. //Checker< std::is_base_of<A,T>::value > check;
  20. // or use static_assert
  21. static_assert(std::is_base_of<A,T>::value,"Type T must inherit from A");
  22. };
  23.  
  24. int main()
  25. {
  26.  
  27. Test<B> test1;
  28. Test<int> test2;
  29. return 0;
  30. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of 'class Test<int>':
prog.cpp:28:12:   required from here
prog.cpp:21:3: error: static assertion failed: Type T must inherit from A
   static_assert(std::is_base_of<A,T>::value,"Type T must inherit from A");
   ^
stdout
Standard output is empty