How to get type index in package with variational type?

for instance

template<typename T, typename... Ts> struct Index { enum {value = ???} }; 

and suppose that T is one of Ts and Ts has different types, such as

 Index<int, int, double>::value is 0 Index<double, int, double>::value is 1 
+5
source share
1 answer
 #include <type_traits> #include <cstddef> template <typename T, typename... Ts> struct Index; template <typename T, typename... Ts> struct Index<T, T, Ts...> : std::integral_constant<std::size_t, 0> {}; template <typename T, typename U, typename... Ts> struct Index<T, U, Ts...> : std::integral_constant<std::size_t, 1 + Index<T, Ts...>::value> {}; 

You might want to add a C ++ 14-mode variable template:

 template <typename T, typename... Ts> constexpr std::size_t Index_v = Index<T, Ts...>::value; 

Demo

+12
source

Source: https://habr.com/ru/post/1203903/


All Articles