fork(5) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. template<template<class> class T, class U>
  5. struct isDerivedFrom
  6. {
  7. static constexpr bool value = decltype(isDerivedFrom::test(std::declval<U>()))::value;
  8. private:
  9. template<class V>
  10. static decltype(static_cast<T<V>>(std::declval<U>()), std::true_type{}) test(const T<V>&);
  11. static std::false_type test(...);
  12. };
  13.  
  14. template<class T>
  15. struct Base {};
  16. struct Base_D1 : Base<int> {};
  17. struct Base_D2 : Base<Base_D2> {};
  18. struct Base_D1_D1 : Base_D1 {};
  19. struct NotDerived {};
  20.  
  21. template <typename T>
  22. struct Base2 {protected: Base2(){}};
  23. struct Derived2 : private Base2<int> {};
  24.  
  25. int main()
  26. {
  27. std::cout << std::boolalpha
  28. << "is Base_D1 derived from or a template instantiation of Base: "
  29. << isDerivedFrom<Base, Base_D1>::value << "\n"
  30. << "is Base_D2 derived from or a template instantiation of Base: "
  31. << isDerivedFrom<Base, Base_D2>::value << "\n"
  32. << "is Base_D1_D1 derived from or a template instantiation of Base: "
  33. << isDerivedFrom<Base, Base_D1_D1>::value << "\n"
  34. << "is Base<double> derived from or a template instantiation of Base: "
  35. << isDerivedFrom<Base, Base<double>>::value << "\n"
  36. << "is NotDerived derived from or a template instantiation of Base: "
  37. << isDerivedFrom<Base, NotDerived>::value << "\n"
  38.  
  39. << "is Derived2 derived from or a template instantiation of Base2: "
  40. << isDerivedFrom<Base2, Derived2>::value << "\n";
  41. return 0;
  42. }
  43.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
is Base_D1 derived from or a template instantiation of Base: true
is Base_D2 derived from or a template instantiation of Base: true
is Base_D1_D1 derived from or a template instantiation of Base: true
is Base<double> derived from or a template instantiation of Base: true
is NotDerived derived from or a template instantiation of Base: false
is Derived2 derived from or a template instantiation of Base2: false