【发布时间】:2021-12-22 03:50:14
【问题描述】:
我希望根据循环中的某些条件触发任意数量的函数,而不是执行递归调用。
如何转换 method0、method1 以调用可变参数 typename ... methods?
编辑: 由于您无法返回可变数量的类型,因此我稍微修改了示例。
template <typename method0, typename method1, typename ... Args>
void loop(Args ... args)
{
for (int i = 0; i < iter_max; i++)
{
if (method0::condition(args))
{
method0::call_modify(args);
}
if (method1::condition(args))
{
method1::call_modify(args);
}
//...
}
}
template<typename ... Args>
struct method0
{
static bool condition(Args& ... args)
{
//Trigger condition
}
static void call_modify(Args& ... args)
{
//Perform action on args
}
};
【问题讨论】:
-
methodX是具有静态方法condition和call的类? -
args = ...?你真的想用第 n 个参数调用 methodN::call 吗? -
您应该提供
method0和method1的外观。
标签: c++ templates variadic-templates non-type-template-parameter