fork download
  1. #include <stdio.h>
  2. #include <type_traits>
  3. #include <typeinfo>
  4.  
  5. struct Base {};
  6. struct OfBase: public Base {};
  7. struct NotBase {};
  8. template <typename T1, typename T2, typename = void>
  9. struct select_base
  10. {
  11. typedef T1 type;
  12. };
  13. template <typename T1, typename T2>
  14. struct select_base<T1, T2, typename std::enable_if<
  15. std::is_base_of<Base, T2>::value, T2>::type>
  16. {
  17. typedef T2 type;
  18. };
  19. int main()
  20. {
  21. printf("Expected OfBase: %s\n", typeid(typename select_base<NotBase, OfBase>::type).name());
  22. printf("Expected OfBase: %s\n", typeid(typename select_base<OfBase, NotBase>::type).name());
  23. return 0;
  24. }
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
Expected OfBase: 7NotBase
Expected OfBase: 6OfBase