存在三种类型的序列,例如std::string
、int
、int
。 C++ 中是否有模板允许函数将无限数量的序列作为参数?
Function("Peter", 27, 89,
"Max", 25, 72,
"Frank", 32, 94,
"David", 31, 98);
最佳答案
问题是“你必须用那个序列做什么”。
可以采用任意序列的参数并将它们以 3 乘 3 的形式传递给仅采用 3 的函数:
void Function(string n, int a, int b)
{ /* base action here */ }
template<class... Others>
void Function(string n1, int a1, int b1, const Others&... t)
{
Function(n1, a1, b1);
Function(t...);
}
只有当参数类型与基函数类型重复匹配时,它才会编译。
关于c++ - 是否有无限参数序列的模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17123888/