Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform parameter pack types

I have multiple classes which share the same public typedefs like

struct A { using Container = std::array<A, 3>; };
struct B { using Container = std::vector<B>; };
struct C { using Container = std::array<C, 5>; };

Now I have a class which gets a parameter pack containing only valid classes but it has to store a tuple of the containers. Pseudo-code:

template <typename... Modules>
struct Collector
{
std::tuple<Modules...::Container> mContainers;
};

Is there an elegant way to apply the ::Container during unpacking?

like image 916
Alexander Weinrauch Avatar asked Nov 19 '18 00:11

Alexander Weinrauch


People also ask

What is parameter pack in c++?

Parameter packs (C++11) A parameter pack can be a type of parameter for templates. Unlike previous parameters, which can only bind to a single argument, a parameter pack can pack multiple parameters into a single parameter by placing an ellipsis to the left of the parameter name.

What is Variadic template in C++?

Variadic templates are class or function templates, that can take any variable(zero or more) number of arguments. In C++, templates can have a fixed number of parameters only that have to be specified at the time of declaration. However, variadic templates help to overcome this issue.

What is Pack expansion?

Pack expansion (C++11) A pack expansion is an expression that contains one or more parameter packs followed by an ellipsis to indicate that the parameter packs are expanded. Consider the following example: template<class...T> void func(T...a){}; template<class...U> void func1(U...b){ func(b...


1 Answers

You can use a helper trait

template<typename T>
using ContainerOf = typename T::Container;

template <typename... Modules>
struct Collector
{
std::tuple<ContainerOf<Modules>...> mContainers;
};

alternatively, you can also inline this trait like so without a helper trait:

template <typename... Modules>
struct Collector
{
std::tuple<typename Modules::Container...> mContainers;
};
like image 146
WorldSEnder Avatar answered Sep 28 '22 12:09

WorldSEnder