How to return TypeList back to the original parameter package

I am having trouble finding information on how to use C ++ 11 variadic TypeLists as containers for portable parameter packages. Suppose I had this piece of code.

#include <type_traits> 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> {}; 

I can use it to find the first index of a type inside a parameter package like this.

 int main() { using namespace std; cout << Index<int, int>::value << endl; //Prints 0 cout << Index<char, float, char>::value << endl; //Prints 1 cout << Index<int, double, char, int>::value << endl; //Prints 2 } 

How can I achieve this behavior for TypeList? I am trying to create something like this.

 template <typename ...> struct TypeList {}; int main() { using namespace std; using List = TypeList<char, short, long> cout << Index<short, ConvertToParameterPack<List>::types...>::value << endl; //Prints 1 } 

Where ConvertToParameterPack is the method to return the TypeList back to ParameterPack. If what I ask is not possible, are there any other ways to solve this problem?

+1
source share
1 answer
 template<std::size_t I> using index_t=std::integral_constant<std::size_t,I>; template<class T, class List> struct Index{}; template<class T, class...Ts> struct Index<T, TypeList<T,Ts...>>: index_t<0> {}; template<class T,class U, class...Ts> struct Index<T, TypeList<U,Ts...>>: index_t< 1+Index<T,TypeList<Ts...>::value > {} 
+1
source

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


All Articles