fork download
  1. #include <type_traits>
  2.  
  3. // base
  4. template<typename T> class Base {};
  5. // public inheritance
  6. class IntDeriv : public Base<int> {};
  7. // public inheritance with private constructor
  8. class IntDerivCPr : public Base<int>
  9. {
  10. private:
  11. IntDerivCPr();
  12. };
  13. // private inheritance
  14. class IntDerivPr : private Base<int> {};
  15. // no inheritance
  16. class Foo {};
  17.  
  18. template< class TestClass >
  19. struct is_derived_from_Base
  20. {
  21. template<typename T>
  22. static std::true_type inherited(Base<T>*);
  23. static std::false_type inherited(void*);
  24.  
  25. static const bool value = decltype(inherited(new TestClass()))::value;
  26. };
  27.  
  28.  
  29. #include <iostream>
  30.  
  31. int main()
  32. {
  33. std::cout << std::boolalpha;
  34. std::cout << is_derived_from_Base<Foo>::value << "\n"; // should print false
  35. std::cout << is_derived_from_Base<IntDeriv>::value << "\n"; // should print true
  36. //std::cout << is_derived_from_Base<IntDerivCPr>::value << "\n"; // does not compile
  37. //std::cout << is_derived_from_Base<IntDerivPr>::value << "\n"; // should print false? true? (does not compile)
  38. }
  39.  
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
false
true