fork(3) download
  1. #include <type_traits>
  2. #include <iostream>
  3.  
  4. template <typename... Ts>
  5. struct TypeList;
  6.  
  7. template <>
  8. struct TypeList<>
  9. {
  10. static const int size = 0;
  11. static std::integral_constant<int, -1> indexOf(...);
  12. };
  13.  
  14. template <typename Head, typename... Tail>
  15. struct TypeList<Head, Tail...> : TypeList<Tail...>
  16. {
  17. static const int size = sizeof...(Tail) + 1;
  18. static std::integral_constant<int, sizeof...(Tail)> indexOf(Head&&);
  19. using TypeList<Tail...>::indexOf;
  20. };
  21.  
  22. template <typename TypeList, typename T>
  23. using IndexOf = std::integral_constant<int,
  24. TypeList::size - decltype(TypeList::indexOf(std::declval<T>()))::value - 1>;
  25.  
  26. struct Type00 { };
  27. struct Type01 { };
  28. struct Type02 { };
  29. struct Type03 { };
  30. struct TypeXX { };
  31. struct TypeYY { };
  32.  
  33. using MyTypeList = TypeList<
  34. Type00,
  35. Type01,
  36. Type02,
  37. Type03,
  38. TypeXX,
  39. TypeYY
  40. >;
  41.  
  42. int main()
  43. {
  44. std::cout << IndexOf<MyTypeList, Type00>::value
  45. << IndexOf<MyTypeList, Type01>::value
  46. << IndexOf<MyTypeList, Type02>::value
  47. << IndexOf<MyTypeList, Type03>::value
  48. << IndexOf<MyTypeList, TypeXX>::value
  49. << IndexOf<MyTypeList, TypeYY>::value;
  50. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
012345