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?
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.
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.
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...
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;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With