c++ 11 中 function,lambda,bind之间关系
- 什么是 function、 lambda,bind?用来做什么?怎么用?
- function,lambda,bind 之间有什么关系?
- 具体怎么实现?
function 实例
//function 是一个抽象了函数参数以及函数返回值的类模板 #include <functional> #include <iostream> #include <algorithm> using namespace std; void hello(int count) { cout << "hello gq:" << count << endl; } class StaticFunc { public: static void hello(int count) { cout << "StaticFunc::hello gq:" << count << endl; } }; class Hello { public: void operator() (int count) { i += count; cout << "Hello::hello gq:" << i << endl; } void operator() (int a, int b) { cout << "Hello::hello gq:a+b=" << a + b << endl; } int i = 0; }; class CHello { public: void hello(int count) { cout << "CHello::hello gq:" << count << endl; } }; class LambdaHello { //注意:类比lambda表达式展开 public: LambdaHello(int _i) : i(_i) {} void operator()(int count) { i ++; cout << "lambda hello gq:" << count << " i=" << i << endl; } private: int i; }; class BindHello { //相当于auto f_hello9 = bind(&hello, 9); 的展开 public: BindHello(function<void(int)> _fn, int _count) : fn(_fn), count(_count) {}; void operator()() { fn(count); } private: function<void(int)> fn; int count; }; class BindCHello { //相当于f_hello11 = bind(&CHello::hello, &c, placeholders::_1); 的展开 public: typedef void (CHello::*Fn)(int); BindCHello(Fn _fn, CHello *_c) : fn(_fn), c(_c) {} void operator() (int count) { (c->*fn)(count); } private: Fn fn; CHello* c; }; int main(){ function<void(int)> f_hello1 = hello; //保存普通函数;<void(int)>抽象函数参数和返回值 f_hello1(1); //hello gq:1 function<void(int)> f_hello2 = &hello; //指向具体函数的地址或者具体函数 f_hello2(1); //hello gq:1 function<void(int)> f_hello3 = &StaticFunc::hello; //保存类的静态成员函数 f_hello3(2); //StaticFunc::hello gq:2 cout << "======保存仿函数:有状态,通过成员变量进行存储状态:======" << endl; function<void(int)> f_hello4 = Hello(); //调用默认构造生成仿函数对象 f_hello4(4); //Hello::hello gq:4 f_hello4(4); //Hello::hello gq:8 function<void(int,int)> f_hello5 = Hello(); f_hello5(5, 6); //Hello::hello gq:a+b=11 cout << "======保存类成员函数======" << endl; function<void(CHello*, int)> f_hello6 = &CHello::hello; CHello c; f_hello6(&c, 7); //CHello::hello gq:7 cout << "======保存lambda表达式======" << endl; int i = 0; auto f_hello7 = [i](int count) mutable -> void { //如若修改外部变量则采用[&i] i ++; cout << "lambda hello gq:" << count << " i=" << i << endl; }; f_hello7(8); //lambda hello gq:8 i=1 f_hello7(8); //lambda hello gq:8 i=2 cout << "外部变量 i=" << i << "不修改外部变量" << endl; auto f_hello8 = LambdaHello(i); //这里 i 参数相当于值传递 f_hello8(9); //lambda hello gq:9 i=1 cout << "======保存bind返回的函数对象======" << endl; auto f_hello9 = bind(&hello, 9); //绑定普通函数 对应 f_hello9(); //hello gq:9 auto f_hello10 = bind(&CHello::hello, &c, 10); //绑定类成员函数 f_hello10(); //CHello::hello gq:10 auto f_hello11 = bind(&CHello::hello, &c, placeholders::_1); //提供占位符 f_hello11(11); //CHello::hello gq:11 auto f_hello12 = BindHello(&hello, 9); //等价于f_hello9() f_hello12(); //hello gq:9 auto f_hello13 = BindCHello(&CHello::hello, &c); //等价于f_hello11() f_hello13(11); //CHello::hello gq:11 return 0; }
代码执行结果: hello gq:1 hello gq:1 StaticFunc::hello gq:2 ======保存仿函数:有状态,通过成员变量进行存储状态:====== Hello::hello gq:4 Hello::hello gq:8 Hello::hello gq:a+b=11 ======保存类成员函数====== CHello::hello gq:7 ======保存lambda表达式====== lambda hello gq:8 i=1 lambda hello gq:8 i=2 外部变量 i=0不修改外部变量 lambda hello gq:9 i=1 ======保存bind返回的函数对象====== hello gq:9 CHello::hello gq:10 CHello::hello gq:11 hello gq:9 CHello::hello gq:11
注意:
- lambda 表达式展开类比。
- bind 绑定普通成员函数展开。
- bind 绑定类成员函数展开。
充电站
推荐一个零声学院免费公开课程,个人觉得老师讲得不错,分享给大家:Linux,Nginx,ZeroMQ,MySQL,Redis,fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,TCP/IP,协程,DPDK等技术内容,立即学习