Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7774435
  • 博文数量: 701
  • 博客积分: 2150
  • 博客等级: 上尉
  • 技术积分: 13233
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-29 16:28
个人简介

天行健,君子以自强不息!

文章分类

全部博文(701)

文章存档

2019年(2)

2018年(12)

2017年(76)

2016年(120)

2015年(178)

2014年(129)

2013年(123)

2012年(61)

分类: C/C++

2015-12-22 17:33:18

这几天学习Cocos2d-x,看到了以下的一段代码:

// new callbacks based on C++11
#define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
#define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
#define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)

都是定义的一些宏,如果你看了上面的这段代码,觉的很简单,那么这篇文章你完全可以pass了;
如果你对上面定义的这些宏,完全不知道是什么意思,那么,这篇文章就属于你,完全属于你的菜。

通过这篇文章,我将带你进入C++11中std::bind的世界,让我们起航吧。

一、先来看看std::bind1st和std::bind2nd

bind是这样一种机制,它可以预先把指定可调用实体的某些参数绑定到已有的变量,产生一个新的可调用实体,
这种机制在回调函数的使用过程中也颇为有用。
C++98中,有两个函数bind1st和bind2nd,它们分别可以用来绑定functor的第一个和第二个参数,它们都是只可以绑定一个参数。
各种限制,使得bind1st和bind2nd的可用性大大降低。

如果通过上面的内容,你还没有明白std::bind1st和std::bind2nd到底是何方圣神到底是什么东西,那就看这段代码示例:

  1. #include <iostream>
  2. #include <functional>
  3. #include <algorithm>
  4. #include <vector>
  5. using namespace std;

  6. int main()
  7. {
  8.     vector<int> coll;
  9.     for (int i = 1; i <= 10; ++i)
  10.     {
  11.         coll.push_back(i);
  12.     }


  13.     // 查找元素值大于10的元素的个数
  14.     // 也就是使得10 < elem成立的元素个数
  15.     int res = count_if(coll.begin(), coll.end(), bind1st(less<int>(), 10));
  16.     cout << res << endl;


  17.     // 查找元素值小于10的元素的个数
  18.     // 也就是使得elem < 10成立的元素个数
  19.     res = count_if(coll.begin(), coll.end(), bind2nd(less<int>(), 10));
  20.     cout << res << endl;


  21.     return 0;
  22. }


通过上面的代码明白了std::bind1st和std::bind2nd了么?
还没有明白?好吧,我接着往细了讲。

对于上面的代码,less()其实是一个仿函数,如果没有std::bind1st和std::bind2nd,
那么我们可以这样使用less(),代码如下:

less functor = less();
bool bRet = functor(10, 20); // 返回true

看到了么?less()这个仿函数对象是需要两个参数的,比如10<20进行比较,那么10叫做left参数,20叫做right参数。

当使用std::bind1st的时候,就表示绑定了left参数,也就是left参数不变了,而right参数就是对应容器中的element;
当使用std::bind2nd的时候,就表示绑定了right参数,也就是right参数不变了,而left参数就是对应容器中的element。
这下应该讲明白了。

二、再来看看std::bind

C++11中提供了std::bind。
bind()函数的意义就像它的函数名一样,是用来绑定函数调用的某些参数的。

bind的思想实际上是一种延迟计算的思想,将可调用对象保存起来,然后在需要的时候再调用。
而且这种绑定是非常灵活的,不论是普通函数、函数对象、还是成员函数都可以绑定,
而且其参数可以支持占位符,比如你可以这样绑定一个二元函数auto f = bind(&func, _1, _2);,调用的时候通过f(1,2)实现调用。

简单的认为就是std::bind就是std::bind1st和std::bind2nd的加强版。

三、怎么使用std::bind

一个知识点厉不厉害,归根到底还是要经过实践的考验,下面就来看看std::bind到底怎么用。

先看看《C++11中的std::function》中那段代码,
std::function可以绑定全局函数,静态函数,但是绑定类的成员函数时,必须要借助std::bind的帮忙。
但是话又说回来,不借助std::bind也是可以完成的,只需要传一个*this变量进去就好了,比如:

  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;

  4. class View
  5. {
  6. public:
  7.     void onClick(int x, int y)
  8.     {
  9.         cout << "X : " << x << ", Y : " << y << endl;
  10.     }
  11. };


  12. // 定义function类型, 三个参数
  13. function<void(View, int, int)> clickCallback;


  14. int main(int argc, const char * argv[])
  15. {
  16.     View button;


  17.     // 指向成员函数
  18.     clickCallback = &View::onClick;


  19.     // 进行调用
  20.     clickCallback(button, 10, 123);
  21.     return 0;
  22. }
再来一段示例谈谈怎么使用std::bind代码:

  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;


  4. int TestFunc(int a, char c, float f)
  5. {
  6.     cout << a << endl;
  7.     cout << c << endl;
  8.     cout << f << endl;


  9.     return a;
  10. }


  11. int main()
  12. {
  13.     auto bindFunc1 = bind(TestFunc, std::placeholders::_1, 'A', 100.1);
  14.     bindFunc1(10);


  15.     cout << "=================================\n";


  16.     auto bindFunc2 = bind(TestFunc, std::placeholders::_2, std::placeholders::_1, 100.1);
  17.     bindFunc2('B', 10);


  18.     cout << "=================================\n";


  19.     auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
  20.     bindFunc3(100.1, 30, 'C');


  21.     return 0;
  22. }

上面这段代码主要说的是bind中std::placeholders的使用。 std::placeholders是一个占位符。
当使用bind生成一个新的可调用对象时,
std::placeholders表示新的可调用对象的第几个参数和原函数的第几个参数进行匹配,这么说有点绕。比如:

auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
bindFunc3(100.1, 30, 'C');

可以看到,在bind的时候,第一个位置是TestFunc,除了这个,参数的第一个位置为占位符std::placeholders::_2,
这就表示,调用bindFunc3的时候,它的第二个参数和TestFunc的第一个参数匹配,以此类推。

四、使用std::bind的一些需要注意的地方:

bind预先绑定的参数需要传具体的变量或值进去,对于预先绑定的参数,是pass-by-value的;
对于不事先绑定的参数,需要传std::placeholders进去,从1开始,依次递增。placeholder是pass-by-reference的;

bind的返回值是可调用实体,可以直接赋给std::function对象;
对于绑定的指针、引用类型的参数,使用者需要保证在可调用实体调用之前,这些参数是可用的;
类的this可以通过对象或者指针来绑定。

五、为什么要用std::bind

当我们厌倦了使用std::bind1st和std::bind2nd的时候,现在有了std::bind,
你完全可以放弃使用std::bind1st和std::bind2nd了。

std::bind绑定的参数的个数不受限制,绑定的具体哪些参数也不受限制,由用户指定,这个bind才是真正意义上的绑定。
在Cocos2d-x中,我们可以看到,使用std::bind生成一个可调用对象,这个对象可以直接赋值给std::function对象;
在类中有一个std::function的变量,这个std::function由std::bind来赋值,
而std::bind绑定的可调用对象可以是Lambda表达式或者类成员函数等可调用对象,这个是Cocos2d-x中的一般用法。

以后遇到了“奇葩”用法再继续总结了,一次也总结不完的。

六、总结

又是一篇总结怎么使用的文章,如果你觉的看的不过瘾,觉的我的文章写的不痛不痒的,还想看点更深的东西,
比如std::bind是如何实现的啊?好吧,这篇文章确实没有说这些深层次的东西,
推荐这篇文章《bind原理图释》http://www.cnblogs.com/xusd-null/p/3698969.html,
希望这篇文章能满足你哦。
阅读(2284) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~