具有某些模板类型的参数数量可变的模板函数

Templated function with variable number of params of certain templated type

本文关键字:函数 数数 类型 参数      更新时间:2023-10-16
template <int S>
struct Vec {};

现在,我想编写一个仅接受这些向量但具有不同模板参数值的函数。可以这样称呼:

f(Vec<1>(), Vec<2>(), Vec<3>());

如何编写这样的功能?我想使用参数包。可能是:

template<int... Ss>
f(Vec<Ss...> vecs);

我想让用户看到该函数的期望是从其声明而不是从编译错误中获得的。

您需要使用:

template <int... Ss>
void f(Vec<Ss>... vecs) { ... }

在我的设置中构建的程序:

template <int S>
struct Vec {};
template <int... Ss>
void f(Vec<Ss>... vecs)
{
}
int main()
{
   f(Vec<1>(), Vec<2>(), Vec<3>());
}