函数作为可变参数模板的参数

wrw*_*rwt 17 c++ templates variadic-templates

假设我们有一个班级

template <int(*F)(int, int)>
class A {
    // ...
};
Run Code Online (Sandbox Code Playgroud)

它需要一个函数作为模板参数.

现在我想制作一个可变参数模板,它将函数作为模板参数.

template <int(*F...)(int, int)> // this won't compile
template <int(*F)(int, int)...> // this won't compile either
Run Code Online (Sandbox Code Playgroud)

怎么做得好?

Jar*_*d42 18

你可能会这样做

using Function_t = int(*)(int, int);

template <Function_t ... Fs> struct s{};
Run Code Online (Sandbox Code Playgroud)

否则,如果你不想使用typedef

template <int(*...Fs)(int, int)> struct s{};
Run Code Online (Sandbox Code Playgroud)

注意:第二个版本不能是匿名的(Fs必需),因为ISO C++ 11要求带括号的包声明具有名称.


dom*_*om0 12

template <int(*...F)(int, int)>
class A {
    // ...
};
Run Code Online (Sandbox Code Playgroud)