How to get types in C ++ 11 parameter package?

I'm not sure that I have formulated this question in the most efficient way, but now I'm just starting to use C ++ 11, and I'm having problems applying its new functions to the problem. I have the following conditional function:

template <typename ... Args>
std::vector<std::type_index> foo()

I would like to foo()return vectorthat contains a value type_indexfor each of the types in the parameter package Args. For example, foo<int, vector<int>, double>()will return vectorcontaining { type_index(typeid(int)), type_index(typeid(vector<int>)), type_index(typeid(double)) }.

It’s clear that I would like to iterate over the types in the package and call the conversion above for each of them (that is, for the type T, return type_index(typeid(T)). I feel that there should be a clean way to achieve this, but it’s not clear to me how to operate a machine with variational a template to make this work. Is my intuition correct?

+4
source share
1 answer

The easiest way to "iterate over" the types in a package is to use the package extension to repeat the desired pattern. In this case, you should write something like the following:

return { type_index(typeid(Args))... }
+5
source

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


All Articles