【发布时间】:2020-01-31 19:36:42
【问题描述】:
我是 C++ 14 的新手,我想将可变长度的 lambda 函数组合成一个 lambda,我应该怎么做?以下是我目前的工作
#include <iostream>
template<typename Ftype>
Ftype compose(const Ftype & fn) {
return fn;
}
template<typename Ftype, typename... Other>
auto compose(Ftype fn, Other... other) {
return [=](auto x){return other(fn(x))...;};
} ➤ expression contains unexpanded parameter pack 'other'
int main(void) {
auto add_func = [](const int x) { return x * 7; };
auto sub_func = [](const int x) { return x + 1; };
int res = compose(add_func, sub_func)(1);
std::cout << "Result: " << res << "\n";
}
但是我编译失败了,我想我可能使用了 lambda 或可变参数以某种方式错误。 有人可以帮我吗?
【问题讨论】:
标签: c++ lambda c++14 variadic-templates function-composition