使用参数包在C++中组合 lambda 函数
Compose lambda function in C++ using parameter pack
我是 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或可变参数。 有人可以帮助我吗?
您的"递归"案例不包含对compose
的调用,这应该暗示您在哪里混淆了;)
return [=](auto x){ return compose(other...)(fn(x)); };
相关文章:
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 可组合的lambda/std::函数与std::可选
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 如何建立使用模板函数的lambda函数的尾部返回类型
- 混合组合和继承的C++问题
- 我需要将多个函数组合为一个函数
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 如何将lambda作为模板类的成员函数参数
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 在 lambda 捕获中声明的变量的类型推导
- 通过组合不同的类型来创建唯一的id
- 我可以将调用类的"this"传递给 lambda 函数吗?
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 如何组合lambda,vairadic函数和函数指针?
- 使用参数包在C++中组合 lambda 函数
- 组合 lambda 捕获时出现编译错误
- 如何使用for_each,transform,Iterator和lambda的某种组合将STD ::集合的字符串转换为小
- 在C++中组合两个lambda