How to unpack std::tuple from C++ template?
Sure, you just need another level of indirection (as usual):
// this function declaration is used just for the type
// transformation, and needs no definition
template <typename... Types>
auto unpack(std::tuple<Types...>) -> std::tuple<std::vector<Types>...> ;
template <typename Tuple>
class MyClass
{
// use the return type of unpack
decltype(unpack(std::declval<Tuple>())) my_tuple;
};
And now you can instantiate MyClass
with a tuple
, like this:
MyClass<std::tuple<int, double>> m;
which contains a field my_tuple
of type
std::tuple<std::vector<int>, std::vector<double>>
Here's a working demo.
Yes, and to add to cigien's answer, another way to go about it would be to unpack it through a template specialization.
template<typename Tuple>
class MyClass;
template<typename... Ts>
class MyClass<std::tuple<Ts...>>
{
// ...
using VectorTuple = std::tuple<std::vector<Ts>...>;
// ...
};