Multiple parameter packages - how?

I have the following problem:

#include <vector> #include <tuple> using namespace std; template< size_t... N_i, typename Ts... > class A { // ... private: std::vector<size_t> _v = { N_i... }; std::tuple<Ts...> _t; }; int main() { A<1> a; } 

As you can see above, I am trying to define several parameter packages as arguments to a class A template.
Unfortunately, the code does not compile:

error: expected qualifier of nested names before "Ts"

How can I define several parameter packages for this example?

+6
source share
2 answers

One way to achieve the ultimate goal is to use a nested template:

 template< size_t... N_i> class initial_values { public: template <typename Ts...> class A { // ... private: std::vector<size_t> _v = { N_i... }; std::tuple<Ts...> _t; }; }; 

You can then reference the template, for example:

 initial_values<1,2,3>::A<int, char> a; 
+8
source

Consider the error:

expected nested qualifier before "Ts"

This is due to what you wrote:

 template< size_t... N_i, typename Ts... > 

Instead:

 template< size_t... N_i, typename... Ts > 

However, if you fix it, the code will not compile.
This is because you cannot mix the two parameter packages in the way you did.
You must reorganize your code in order to somehow get them out of context.

As an example, you can use std::index_sequence and partial specialization:

 #include <vector> #include <tuple> #include<functional> using namespace std; template< typename... > class A; template< size_t... N_i, typename... Ts > class A<index_sequence<N_i...>, Ts...> { // ... private: std::vector<size_t> _v = { N_i... }; std::tuple<Ts...> _t; }; int main() { A<index_sequence<1>> a; } 
+2
source

All Articles