fork(40) download
  1. #include <iostream>
  2. #include <type_traits>
  3. using namespace std;
  4.  
  5. class MyClass
  6. {
  7. public:
  8. int a = 1;
  9. };
  10.  
  11. class Derived : public MyClass
  12. {
  13. public:
  14. int b = 2;
  15. };
  16.  
  17. class NotDerived
  18. {
  19. public:
  20. int b = 3;
  21. };
  22.  
  23. template<typename T, typename std::enable_if<std::is_base_of<MyClass, T>::value>::type* = nullptr>
  24. T Foo(T bar)
  25. {
  26. return T();
  27. }
  28.  
  29. int main() {
  30. Derived d;
  31. NotDerived nd;
  32. std::cout << Foo(d).b << std::endl;; // works
  33. //std::cout << (Foo(nd)).b << std::endl;; //compiler error
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
2