Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unpack std::tuple from C++ template?

I have a class that is expected to take std::tuple of certain types (arbitrary count is allowed) as a template argument. I want to declare a tuple of vectors of the types. For instance, I have

template <typename T>
class MyClass {/*...*/}

I instantiate it

MyClass<std::tuple<int, bool>> my_class;

and I aim to create a field like this

std::tuple<std::vector<int>, std::vector<bool>> my_tuple;

I know that if I had a class definition

template <typename... T>
class MyClass2 {/*...*/}

I could instantiate it by passing the data types without a tuple

MyClass<int, bool> my_class2;

and then simply create the field

std::tuple<std::vector<int>, std::vector<bool>> my_tuple2;

using

std::tuple<std::vector<T>...> my_tuple2;

Hence, I was thinking that I should somehow unpack the data types from the tuple. Is it possible?

like image 467
Tatarkow Avatar asked Apr 16 '20 23:04

Tatarkow


1 Answers

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>...>;
    // ...
};
like image 183
Travis Avatar answered Sep 21 '22 04:09

Travis