评估参数包

Evaluating a parameter pack

本文关键字:参数 评估      更新时间:2023-10-16

这是不使用折叠来计算参数包的唯一方法吗(因为它需要使用运算符(?

#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++是不允许的。有没有比使用上面介绍的解决方法更优雅的方法来实现这一点?

有一个更简单的解决方案:

#include <iostream>
template<int ...Is, typename Function>
void eval(Function&& f)
{
(f(Is),...);
}
int main()
{
eval<0,1,2>([](int x){std::cout << x << "n";});
}