Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ std::tuple<Ts...> to std::tuple<std::vector<T>...>>

Given a variadic template, I would like to define an object that would be a tuple of vector of each type in the variadic template.

Something like:

template <typename... Ts>
class C {    
   std::tuple<std::vector<T1>, std::vector<T2>,...,std::vector<Tn>> vectors;
};

It does not have to be one data member, I mean, if it is possible to accomplish this using more auxiliary types or objects, that would not be a problem.

I tried a combination of std::make_index_sequence with std::tuple, but I could not figure it out.

like image 791
canellas Avatar asked Sep 06 '25 00:09

canellas


2 Answers

You don't have to use a class here, you can make use of a simple type-alias template:

template<typename... Ts>
using VectorTuple = std::tuple<std::vector<Ts>...>;
like image 181
Azam Bham Avatar answered Sep 07 '25 14:09

Azam Bham


template <typename... Ts>
class C {    
   std::tuple<std::vector<Ts>...> vectors;
};

This expands the pattern (std::vector<Ts>) for each type in the pack (resulting in your desired std::tuple<std::vector<T1>, std::vector<T2>,...,std::vector<Tn>>)

like image 29
Artyer Avatar answered Sep 07 '25 14:09

Artyer