如何在C 11之前进行咖喱功能
How to do function currying before c++11
本文关键字:功能 更新时间:2023-10-16
我有一个有两个参数的函数,我想将第二个参数绑定到一个值并获取一个新函数对象。
我想要的是由c 11中的std :: bind完美支持:
int my_func(int a, int b) {...}
auto bind_func = std::bind (my_func,_1, 555);
或python:
bind_func = functools.partial(my_func, b=555)
但我想在C 03中进行操作,我知道Boost可以做到,但我不想为这个简单的要求调用Boost。
现在,我写了自己的模板来执行此操作,但是如果我可以使用标准库,那将是完美的。
有人知道我该怎么做吗?
谢谢!
函子非常容易制作。这是在2011年以前的C 。
示例:
struct my_func_functor // choose a name
{
int curry_;
my_func_functor(int curry) : curry_(curry) {}
int operator()(int a) { return my_func(a, curry_); }
};
// to use...
vector<int> v;
//...
// build the functor with the curry and let transform do the cooking...
std::transform(v.begin(), v.end(), v.begin(), my_func_functor(555));
您当然可以将想要的任何东西存储在函子中。包括参考。这与C 11的Lambdas背后使用的模型相同。您没有一个构造函数,而不是捕获子句,并且在通过operator()
进行实际调用时保存数据的结构。
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 对可变参数使用声明.如何选择正确的功能
- 询问在设计我的手臂模拟器功能表示格式1
- 功能原型的目的
- 这里在 Linux 中具有"CreatePipe"和"CreateProcessW"功能吗?