Ad

Generate Integer Sequence For Template Parameter Pack

- 1 answer

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.

Ad

Answer

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>());
Ad
source: stackoverflow.com
Ad