There is a class method template with parameter pack I want to call, defined as:
class C {
template<int ... prp> void function() {}
}
For a given integer N, I need all integers up to N as template arguments for the parameter pack.
constexpr int N = 2;
C c;
c.function<0, 1>();
I have tried using std::integer_sequence, but it can't be used here.
c.function<std::make_integer_sequence<int, N>>();
I also found this answer: Passing std::integer_sequence as template parameter to a meta function Could I pass the function inside the partial specialization, e.g. using std::function? I was not able to use template arguments with std::function.
Additionally, the class has multiple function I would like to call the same way.
c.function1<0, 1>();
c.function2<0, 1>();
There must be a nice way to solve this but I wasn't successful yet. Still trying to understand TMP.
CodePudding user response:
What about passing the std::integer_sequence as argument?
I mean something as follows
class C {
template <int ... prp>
void function (std::integer_sequence<int, prp...>)
{ /* ... */ }
}
that you can call using std::make_integer_sequence
constexpr int N = 2;
C c;
// this call function<0, 1>(std::integer_sequence<int, 0, 1>);
// the prp variadic list (0, 1, in this case) is deduced
c.function(std::make_integer_sequence<int, N>{});
(caution: code not tested)
CodePudding user response:
You might create helper function:
template <int... Is>
void helper(C& c, std::integer_sequence<int, Is...>)
{
c.function1<Is...>();
c.function2<Is...>();
}
And call it
constexpr int N = 2;
C c;
helper(c, std::make_integer_sequence<int, N>());
