Template parameter package attribute

We have a class of templates:

template<int i> class A { ... }; 

But how to declare a template class wrapper:

 template<int... is> Pack { private: A<is...> attrs; }; 

Or who has class set A?

+6
source share
2 answers

Using std::tuple , using an example

 #include <tuple> template <int i> class A { }; template <int... is> class Pack { std::tuple<A<is>...> attrs; }; int main() { Pack<2,3,5,7,11,13> p; } 

Another way could be inheritance

 template <int i> class A { }; template <int... is> class Pack : A<is>... { }; int main() { Pack<2,3,5,7,11,13> p; } 
+9
source

The best approach I know is to use a list of types :

 template<class...> struct type_list{}; template<int...Is> using a_pack = type_list<A<Is>...>; 

With a list type, it is very easy to convert or perform operations on each member. For example, let's create a list_type from std: vector with the previous code:

 template<class> struct vectors_of; template<class...As> struct vectors_of<type_list<As...>>{ using type=type_list<std::vector<As>...>; }; using vectors_of_a = typename vectors_of<a_pack<1,2>>::type; 

They have a lot of documentation about type lists. This is one of the main metaprogrammer tools from this book: Modern C ++ Design (which uses pre-C ++ 11). With C ++ 11 it is even easier to use.

+3
source

Source: https://habr.com/ru/post/1011630/


All Articles