这是不使用折叠来评估参数包的唯一方法(因为它需要使用运算符)?
#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 ++中是不允许的。是否有比使用上面介绍的解决方法更优雅的方法?
有一个更简单的解决方案: