【发布时间】:2019-10-20 18:05:20
【问题描述】:
这是在不使用折叠的情况下评估参数包的唯一方法吗(因为它需要使用运算符)?
#include <iostream>
template<int ...Is, typename Function>
void eval(Function&& f)
{
// (f(Is)...);
auto op = [&f](int i){f(i); return 0;};
auto doNothing = [](auto...){};
doNothing(op(Is)...);
}
int main()
{
eval<0,1,2>([](int x){std::cout << x << "\n";});
}
基本上我想做(f(Is)...),但由于某种原因,这在 C++ 中是不允许的。有没有比使用上述解决方法更优雅的方法来实现这一点?
【问题讨论】:
-
@user7769147 这不是真的,这很好。
-
永远不要低估逗号运算符的威力
-
您只需要在
f(Is)和...之间加一个逗号。(f(Is)...)是一个参数包扩展,可以作为函数参数出现。(f(Is),...)是逗号运算符的折叠表达式。 -
since it requires the use of operators: coma 是运算符!
标签: c++ templates c++17 variadic-templates fold-expression