评估参数包

问题描述 投票:0回答:1

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

#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 ++中是不允许的。是否有比使用上面介绍的解决方法更优雅的方法?

c++ templates c++17 variadic-templates fold
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.