fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<typename ...>
  5. struct void_type
  6. {
  7. using type = void;
  8. };
  9.  
  10. template<typename ...T>
  11. using void_t = typename void_type<T...>::type;
  12.  
  13. #define HAS_TYPE(NAME) \
  14. template<typename, typename = void> \
  15. struct has_type_##NAME: std::false_type \
  16. {}; \
  17. template<typename T> \
  18. struct has_type_##NAME<T, void_t<typename T::NAME>>: std::true_type \
  19. {}
  20.  
  21. HAS_TYPE(MyType);
  22.  
  23. template<typename T, bool = has_type_MyType<T>::value>
  24. struct MyTypeOrTupple_impl;
  25.  
  26. template<typename T>
  27. struct MyTypeOrTupple_impl<T, true>
  28. { using type = typename T::MyType; };
  29.  
  30. template<typename T>
  31. struct MyTypeOrTupple_impl<T, false>
  32. { using type = std::tuple<>; };
  33.  
  34. template<typename T> using MyTypeOrTupple = typename MyTypeOrTupple_impl<T>::type;
  35.  
  36. struct A
  37. {
  38. using MyType = int;
  39. };
  40.  
  41. struct B {};
  42.  
  43. int main()
  44. {
  45. static_assert(std::is_same<MyTypeOrTupple<A>, A::MyType>{}, "!");
  46. static_assert(std::is_same<MyTypeOrTupple<B>, std::tuple<>>{}, "!");
  47. }
  48.  
  49.  
  50.  
Success #stdin #stdout 0s 3408KB
stdin
Standard input is empty
stdout
Standard output is empty