Lyve
Lyve

Reputation: 250

Calling a template with template parameters from a std::integer_sequence

Scratching my head. Given that I have the following integer sequence:

std::integer_sequence<int,0,1,2>

And I have the following template:

template<int a, int b, int c> void myFunction() {}

Is there any way to call the template with the integer sequence as template parameters?

myFunction<std::integer_sequence<int,0,1,2>>(); This does not compile

I found some examples here on stack overflow how to pass the integer sequence as function parameters, but unfortunately this is not an option in my case. I can't use a parameter pack either as I already use a parameter pack for other things in the same context.

I am using C++17

Help is highly appreciated!

Upvotes: 2

Views: 177

Answers (1)

songyuanyao
songyuanyao

Reputation: 172924

You might write a helper template, e.g.

template<typename T, T... I>
auto helper(std::integer_sequence<T, I...>)
{
    myFunction<I...>();
}

Then call it as

helper(std::integer_sequence<int,0,1,2>{});

LIVE

Upvotes: 3

Related Questions