程序开发基础学习三(boost::bind 函数学习)

        by:武龙飞

        工作中要用到boost库,我的学习习惯是在学习新的知识的时候,将整个知识点的历史和发展现状过一遍。这样既能激发学习兴趣,同时对于知识点的记忆和学习能形成一个完整的系统结构。boost库最新版本是1.47,今天主要总结boost库里一个常用的函数bind,由于之前从来没有用过这个函数,对这个函数的一些妙用也不甚了解。

        我对bind函数的理解是,bind现在最大支持九个参数,通过对bind传递不同的参数,最后返回一个新的函数指针,通过这个函数指针的调用变相的产生的函数。我先用普通方法简单模拟一下,大概说明白bind 的原理就行。代码如下:

  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include "boost/bind.hpp"  
  4. #include "boost/function.hpp"  
  5. using namespace std;  
  6.   
  7.   
  8. void PrintHello(int i)  
  9. {  
  10.         cout<<"hello, world.\n";  
  11. }  
  12. void CallFuntion(void (*f)(int), int i)  
  13. {  
  14.         f(i);  
  15. }  
  16.   
  17.   
  18. int _tmain(int argc, _TCHAR* argv[])  
  19. {  
  20.        CallFuntion(PrintHello, 2);  
  21.        getchar();  
  22.        return 0;  
  23. }  

        这个就是通过传递函数PrintHello的函数指针给CallFuntion函数,当然了,有人说直接调用PrintHello多省事,呵呵,说的没错,看看下面bind的几种用法吧。

        1、bind构造普通函数

  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include "boost/bind.hpp"  
  4. #include "boost/function.hpp"  
  5. using namespace std;  
  6.   
  7. void PrintHello(int i)  
  8. {  
  9.     cout<<"hello, world.\n";  
  10. }  
  11. void CallFuntion(void (*f)(int), int i)  
  12. {  
  13.     f(i);  
  14. }  
  15.   
  16. void CallBindFunction(boost::function<void (void)> f)  
  17. {  
  18.     f();  
  19. }  
  20. int _tmain(int argc, _TCHAR* argv[])  
  21. {  
  22.     CallFuntion(PrintHello, 2);  
  23.     CallBindFunction(boost::bind(PrintHello, 2));  
  24.     getchar();  
  25.     return 0;  
  26. }  

        看见区别了吧,通过配合boost里的function函数,不就可以实现函数变参了,这就是bind的一种妙用。

         2、bind函数调整参数

  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include "boost/bind.hpp"  
  4. #include "boost/function.hpp"  
  5. using namespace std;  
  6.   
  7.   
  8. int Sum2(int i, int j)  
  9. {  
  10.     return i + j;  
  11. }  
  12.   
  13.   
  14. int _tmain(int argc, _TCHAR* argv[])  
  15. {  
  16.     cout<<Sum2(1, 2)<<endl; // Sum2(x, y);  
  17.   
  18.     boost::function<int (intint)> f11 = boost::bind(Sum2, _1, _1);     // Sum2(x, x);  
  19.   
  20.     cout<<f11(1, 2)<<endl;  
  21.     boost::function<int (intint)> f21 = boost::bind(Sum2, _2, _1);   // Sum2(y, x);  
  22.     cout<<f21(1, 2)<<endl;  
  23.   
  24.     boost::function<int (intint)> f12 = boost::bind(Sum2, _2, _2);     // Sum2(y, y);  
  25.     cout<<f12(1, 2)<<endl;  
  26.   
  27.     boost::function<int (intint)> f13 = boost::bind(Sum2, _1, 3);       // Sum2(x, 3);  
  28.     cout<<f13(1, 2)<<endl;  
  29.     getchar();  
  30.     return 0;  
  31. }  


        这就是通过bind函数构造出不同参数的函数,是不是大大加大了代码的复用性和易用性。同时可以在bind的参数里设置构造的新函数默认值。

        3、函数对象

  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include "boost/bind.hpp"  
  4. #include "boost/function.hpp"  
  5. #include "boost/ref.hpp"  
  6. using namespace std;  
  7.   
  8. struct F  
  9. {  
  10.     int s;  
  11.     int operator()(int i){ return s += i;}  
  12.     bool operator()(bool b){ return b;}  
  13. };  
  14.   
  15. int _tmain(int argc, _TCHAR* argv[])  
  16. {  
  17.     F f = {0};  
  18.     int arry[3] ={1, 2, 3};  
  19.     //boost::function<int (int)> sum = boost::bind(boost::ref(f), _1);  
  20.     boost::function<int (int)> sum = boost::bind<int>(boost::ref(f), _1);  
  21.     for_each(arry, arry + 3, sum);  
  22.     assert(f.s != 6);  
  23.     return 0;  
  24. }  

         这里有两个知识点,一个是bind构造的函数要表明返回类型,boost文档给出的解释是: 没有 typeof操作符,返回类型无法推导。其实我们自己也很容易看出来,对于上面申明的两个操作符重载,没有返回类型确实无法分辨。第二个知识点是,引用的问题,假如不添加引用符号ref的话,默认会复制一个新的函数对象,这样的话,前面定义的f的s值不会改变。

        4、成员函数

  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. #include "boost/bind.hpp"  
  4. #include "boost/function.hpp"  
  5. #include "boost/ref.hpp"  
  6. #include "boost/shared_ptr.hpp"  
  7. using namespace std;  
  8.   
  9. struct X  
  10. {  
  11.     void f(int count)  
  12.     {  
  13.         for (int i = 0; i < count; ++i)  
  14.         {  
  15.             cout<<"hello,world.\n";  
  16.         }  
  17.     }  
  18. };  
  19.   
  20. int _tmain(int argc, _TCHAR* argv[])  
  21. {  
  22.     X x;  
  23.     boost::shared_ptr<X> p(new X);  
  24.   
  25.     boost::function<void (int)> PrintHello1 = boost::bind(&X::f, boost::ref(x), _1); // x.f(i)  
  26.     PrintHello1(1);  
  27.   
  28.     boost::function<void (int)> PrintHello2 = boost::bind(&X::f, &x, _1); // (&x)->f(i)  
  29.     PrintHello2(2);  
  30.   
  31.     boost::function<void (int)> PrintHello3 = boost::bind(&X::f, x, _1);  // (internal copy of x).f(i)  
  32.     PrintHello2(3);  
  33.   
  34.     boost::function<void (int)> PrintHello4 = boost::bind(&X::f, p, _1);  // (internal copy of p).f(i)  
  35.     PrintHello2(4);  
  36.     return 0;  
  37. }  

        这里注意的是第三种方式和第四种方式,第三种方式里的x将被拷贝,如果x数据结构比较大的话,效率上讲就非常低了,需要注意。第四种也是拷贝,但这里还申明为shared_ptr类型,意思是在出了限定域内不会释放,这boost文档里给出的例子。没有别的意思,就是见到后注意下就行。

        bind基本就这几种用法,还有一些比较巧妙的用法,比如说将bind返回值当作参数传递给bind嵌套用法,还有将function<void (void)> 类型申明为成员函数。有兴趣的话可以看boost文档,或者九天雁的关于bind的用法总结。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值