我是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或variadic参数。有人可以帮我吗?
您的“递归”案例不包含对compose
的调用,这应该是您在哪里混淆了;)]]
return [=](auto x){ return compose(other...)(fn(x)); };