C++11中的function和bind

 在C++11中添加了两个函数绑定模板, 即function和bind。function模板类和bind模板函数,使用它们可以实现类似函数指针的功能,但却比函数指针更加灵活,特别是函数指向类的非静态成员函数时。

         std::function和std::bind都可以绑定到普通函数(包括类的静态函数)、类的成员函数 。

下面给出简单的示例 :

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  main.cpp  
  3. //  Cpp11StdBind  
  4. //  
  5. //  Created by mrsimple on 4/12/14.  
  6. //  Copyright (c) 2014 mrsimple. All rights reserved.  
  7. //  
  8.   
  9. #include <iostream>  
  10. #include <string>  
  11. #include <functional>  
  12. using namespace std;  
  13.   
  14. // 用于普通函数和类的静态成员函数  
  15. typedef function<void (int)> mdPoint;  
  16.   
  17. // 普通函数  
  18. void printNum(int num)  
  19. {  
  20.     cout<<__func__<<", the num is "<<num<<endl;  
  21. }  
  22.   
  23.   
  24. // class  
  25. class ThreadPool  
  26. {  
  27. public:  
  28.     ThreadPool() { cout << "max size : " << max_size<<endl; }  
  29.     ~ThreadPool() {}  
  30.     // 静态函数  
  31.     static void setThreadPoolMaxSize(int size) {  
  32.         max_size = size ;  
  33.         cout<<__func__<<", set thread pool size , " <<size<<endl;  
  34.     }  
  35.   
  36.       
  37.     // 非静态成员函数  
  38.     void setCoreThread(int core) {  
  39.         core_size = core ;  
  40.         cout<<__func__<<", core size , " <<core<<endl;  
  41.   
  42.     }  
  43.   
  44.     void outputCoreSize()  
  45.     {  
  46.         cout<<__func__<<", core size "<<core_size<<endl;  
  47.     }  
  48.       
  49.     // 使用bind来绑定回调函数  
  50.     void bindCallback()  
  51.     {  
  52.         mdPoint mp = bind(&ThreadPool::setCoreThread, this, placeholders::_1) ;  
  53.         mp(10);  
  54.     }  
  55. public:  
  56.    static int max_size;  
  57.     // 非静态成员变量, 可以直接初始化数据  
  58.     int core_size = 5;  
  59. } ;  
  60.   
  61. // 定义静态成员变量  
  62. int ThreadPool::max_size = 100 ;  
  63.   
  64.   
  65. // main  
  66. int main(int argc, const char * argv[])  
  67. {  
  68.     cout<<ThreadPool::max_size<<endl;  
  69.       
  70.     // 使用function来绑定函数指针  
  71.     mdPoint mp = (&printNum);  
  72.     mp(123);  
  73.       
  74.     // 指向类的静态函数  
  75.     mp = (&ThreadPool::setThreadPoolMaxSize);  
  76.     mp(456);  
  77.       
  78.     auto tp = new ThreadPool();  
  79.     tp->bindCallback() ;  
  80.       
  81.     // 带参数  
  82.     auto cb2 = bind(&ThreadPool::setCoreThread, tp, placeholders::_1) ;  
  83.     cb2(66);  
  84.   
  85.     // 使用bind来设置回调函数, 不带参数的  
  86.     auto callback = bind(&ThreadPool::outputCoreSize, tp) ;  
  87.     callback();  
  88.       
  89.     // lambda的bind  
  90.     auto lmd = bind([] { cout<<"this is lambda."<<endl;}) ;  
  91.     lmd();  
  92.       
  93.     // 传递参数  
  94.     auto lmdp = bind([](string name) { cout <<name<<endl; }, placeholders::_1) ;  
  95.     lmdp("mr.simple");  
  96.       
  97.     // 在placeholders位置上设置参数  
  98.     auto lmdp2 = bind([](string name) { cout << "new name " <<name<<endl; } , "MR.SIMPLE -- NEW .");  
  99.     lmdp2();  
  100.     return 0;  
  101. }  

function的回调类的成员函数:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <iostream>  
  2. #include <functional>  
  3.   
  4. using namespace std;  
  5.   
  6.   
  7.   
  8. //  
  9. class View{  
  10.   
  11. public:  
  12.     void onClick(int x, int y)  
  13.     {  
  14.         cout<<"X : "<<x<<", Y : "<<y<<endl;  
  15.     }  
  16. } ;  
  17.   
  18. // 定义function类型, 三个参数  
  19. function<void (View*, intint)> clickCallback ;  
  20. // 普通函数  
  21. function<int (intint)> f ;  
  22.   
  23.   
  24. //  
  25. int add(int a, int b) {  
  26.     return a + b ;  
  27. }  
  28.   
  29.   
  30. //  
  31. int main(int argc, const char * argv[])  
  32. {  
  33.   
  34.     View button ;  
  35.     // 指向成员函数  
  36.     clickCallback = &View::onClick ;  
  37.     // 进行调用  
  38.     clickCallback(&button, 10, 123);  
  39.       
  40.     f = add ;  
  41.     cout<<"result : "<<f(4,5)<<endl;  
  42.   
  43.     return 0;  
  44. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值