fork download
  1. #include <iostream>
  2. #include <type_traits>
  3. using namespace std;
  4.  
  5. class Base
  6. {};
  7.  
  8. class Derived : public Base
  9. {};
  10.  
  11. template <class T>
  12. class Foo
  13. {
  14. public:
  15. Foo()
  16. {
  17. static_assert (std::is_base_of<Base,T>::value, "Error: Type T not derived from Base");
  18. }
  19. };
  20.  
  21. class Lol
  22. {
  23. };
  24.  
  25. int main() {
  26.  
  27. Foo<Derived> foo1;
  28. Foo<Lol> foo2;
  29.  
  30. return 0;
  31.  
  32. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
1
2
10
42
11
compilation info
prog.cpp: In constructor 'Foo<T>::Foo() [with T = Lol]':
prog.cpp:28:11:   instantiated from here
prog.cpp:17:3: error: static assertion failed: "Error: Type T not derived from Base"
stdout
Standard output is empty