fork download
  1. #include <type_traits>
  2.  
  3. class A {};
  4.  
  5. class B: public A {};
  6.  
  7. class C: public B {};
  8.  
  9. class X {};
  10.  
  11. template<typename T, typename Base,
  12. typename = typename std::enable_if<
  13. std::is_base_of<std::decay_t<Base>, std::decay_t<T>>::value>::type>
  14. void foo(T& tp, const Base* bp);
  15.  
  16. int main()
  17. {
  18. A a; B b; C c; X x;
  19. foo(c, &a);
  20. foo(b, &a);
  21. foo(a, &a);
  22.  
  23. foo(c, &b);
  24.  
  25. foo(c, &c);
  26.  
  27. foo(x, &a); // invalid and produces compiler error
  28. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:27:11: error: no matching function for call to 'foo(X&, A*)'
  foo(x, &a); // invalid and produces compiler error
           ^
prog.cpp:27:11: note: candidate is:
prog.cpp:14:6: note: template<class T, class Base, class> void foo(T&, const Base*)
 void foo(T& tp, const Base* bp);
      ^
prog.cpp:14:6: note:   template argument deduction/substitution failed:
prog.cpp:12:5: error: no type named 'type' in 'struct std::enable_if<false, void>'
     typename = typename std::enable_if<
     ^
stdout
Standard output is empty