Gontran Zipper
Gontran Zipper

Reputation: 61

Function parameter in variadic template

Is it possible to write a variadic template class with functions as template parameter ?

Theses are valid c++ declarations

template <typename ...Types> class foo;

template <int func(int)>  class bar;

Would it be possible to do something like

template <int func(int)...> class foobar; 

I tried a lot of different syntax like

template <int ...func(int)> class foobar; 

and nothing is compiling (I am using gcc 8.1.0 with -std=c++17)

Upvotes: 2

Views: 95

Answers (1)

Jarod42
Jarod42

Reputation: 217145

Syntax is:

template <int (*...Fs)(int)> class foobar {};

which allows

int f1(int);
int f2(int);

foobar<&f1, &f2, &f1> obj;

Using alias might help you to have more natural syntax:

using f_int_int = int (*)(int);
template <f_int_int...Fs> class foobar {};

or even (thanks to Yakk's comment):

template <typename T> using id_t = T;
template <id_t<int(*)(int)>...Fs> class foobar {};

Upvotes: 9

Related Questions