【发布时间】:2021-04-23 14:21:10
【问题描述】:
我写了一个方便的函数,它获取一些参数然后发送它们 到正确的功能:
void combine(int a, int b, double one, double two) {
funA(a,b);
funB(one, two);
}
现在我想再做一次,但我的combine 函数和funA-funB 对都使用可变模板参数:
//somewhere earlier in the code
template<class U, class T> funA(U par1, T par2) {...}
template<class X, class Y> funB(X par1, Y par2) {...}
template<class ...ParamA, class ...ParamB>
void combine(ParamA...packA, ParamB...packB) {
funA(packA...);
funB(packB...);
}
这当然行不通,因为我需要一些方法将参数列表分成两半。
但更有趣的是,当我尝试使用类似的调用编译上述代码时
combine(10, 'a', 12.0, true)我收到这个错误:
In instantiation of ‘void combine(ParamA ..., ParamB ...) [with ParamA = {}; ParamB = {int, char, double, bool}]’:
...
error: no matching function for call to ‘funA()’
....
error: no matching function for call to ‘funB(int&, char&, double&, bool&)’
这表明ParamB“吃了”所有的参数列表。
所以,我的问题是:有没有办法将带有变量模板列表的函数的参数列表分成两半?。如果没有,我还能如何编写我的 combine 函数?
【问题讨论】: