Types of mixing and non-types in the parameters of the variation pattern?

Is it possible to mix types and nontypes in the parameters of a variation template? If I were to pass std::array , for example, to this class as a parameter of T , I would also need to pass the type for the array and the length, but the way I tried it throws an error when it reaches a value because it expects Types only for Types :

 template < template<class, std::size_t> class T, class ... Types> class C { T<Types...> storage; }; int main(){ C<std::array, int, 3> c; } 

Error message:

 error: template argument for template type parameter must be a type Container<std::array, int, 3> c; ^ 

Is there a way to convey types and values ​​in a variational context?

+7
c ++ variadic-templates
source share
2 answers

As I can see, you alreadty hardcoded the number and types of parameters that class T should accept as a template parameter. Here you do not need variable templates. Just do it instead:

 template < template<class, std::size_t> class T, class A, std::size_t N> class C { T<A, N> storage; }; int main(){ C<std::array, int, 3> c; // works! } 

If you want to use custom templates, add it also to the template template:

 template < template<typename...> class T, typename... Types> class C { T<Types...> storage; }; 

If you want to use this version, but want to use std::array , you can create an alias std::array that already has a size:

 template<typename T> using array3 = std::array<T, 3>; C<array3, int> c; 

Alternatively, you can also create an alias for the template template that allows you to select a size:

 template<std::size_t n> struct sized_array { template<typename T> using array = std::array<T, n>; }; C<sized_array<5>::array, int>; 
+2
source share

Is it possible to mix types and nontypes in the parameters of a variation template?

Not. You cannot mix and match. But since you can transfer a value to a type, but not vice versa, you can just stay in the world of types:

 template <template<class...> class T, class ... Types> class C { T<Types...> storage; }; 

And then the thing is simply to make std::array work only with types:

 template <class T, class N> using my_array = std::array<T, N::value>; template <size_t N> using size_ = std::integral_constant<size_t, N>; 

So your original example:

 C<my_array, int, size_<3>> c; 
+4
source share

All Articles