zoukankan      html  css  js  c++  java
  • C++11中的std::bind

    看看这段代码

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

    1 // new callbacks based on C++11
    2 #define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
    3  
    4 #define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
    5  
    6 #define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
    7  
    8 #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  
     7 int main()
     8 {
     9     vector<int> coll;
    10     for (int i = 1; i <= 10; ++i)
    11     {
    12         coll.push_back(i);
    13     }
    14  
    15     // 查找元素值大于10的元素的个数
    16     // 也就是使得10 < elem成立的元素个数 
    17     int res = count_if(coll.begin(), coll.end(), bind1st(less<int>(), 10));
    18     cout << res << endl;
    19  
    20     // 查找元素值小于10的元素的个数
    21     // 也就是使得elem < 10成立的元素个数 
    22     res = count_if(coll.begin(), coll.end(), bind2nd(less<int>(), 10));
    23     cout << res << endl;
    24  
    25     return 0;
    26 }

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

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

    1 less<int> functor = less<int>();
    2 bool bRet = functor(10, 20); // 返回true

    看到了么?less<int>()这个仿函数对象是需要两个参数的,比如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::bind1ststd::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  
     5 class View
     6 {
     7 public:
     8     void onClick(int x, int y)
     9     {
    10         cout << "X : " << x << ", Y : " << y << endl;
    11     }
    12 };
    13  
    14 // 定义function类型, 三个参数
    15 function<void(View, int, int)> clickCallback;
    16  
    17 int main(int argc, const char * argv[])
    18 {
    19     View button;
    20  
    21     // 指向成员函数
    22     clickCallback = &View::onClick;
    23  
    24     // 进行调用
    25     clickCallback(button, 10, 123);
    26     return 0;
    27 }

    再来一段示例谈谈怎么使用std::bind代码:

     1 #include <iostream>
     2 #include <functional>
     3 using namespace std;
     4  
     5 int TestFunc(int a, char c, float f)
     6 {
     7     cout << a << endl;
     8     cout << c << endl;
     9     cout << f << endl;
    10  
    11     return a;
    12 }
    13  
    14 int main()
    15 {
    16     auto bindFunc1 = bind(TestFunc, std::placeholders::_1, 'A', 100.1);
    17     bindFunc1(10);
    18  
    19     cout << "=================================
    ";
    20  
    21     auto bindFunc2 = bind(TestFunc, std::placeholders::_2, std::placeholders::_1, 100.1);
    22     bindFunc2('B', 10);
    23  
    24     cout << "=================================
    ";
    25  
    26     auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
    27     bindFunc3(100.1, 30, 'C');
    28  
    29     return 0;
    30 }

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

    1 auto bindFunc3 = bind(TestFunc, std::placeholders::_2, std::placeholders::_3, std::placeholders::_1);
    2  
    3 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::bind1ststd::bind2nd的时候,现在有了std::bind,你完全可以放弃使用std::bind1ststd::bind2nd了。std::bind绑定的参数的个数不受限制,绑定的具体哪些参数也不受限制,由用户指定,这个bind才是真正意义上的绑定。

    在Cocos2d-x中,我们可以看到,使用std::bind生成一个可调用对象,这个对象可以直接赋值给std::function对象;在类中有一个std::function的变量,这个std::functionstd::bind来赋值,而std::bind绑定的可调用对象可以是Lambda表达式或者类成员函数等可调用对象,这个是Cocos2d-x中的一般用法。

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

     

    总结

    又是一篇总结怎么使用的文章,如果你觉的看的不过瘾,觉的我的文章写的不痛不痒的,还想看点更深的东西,比如std::bind是如何实现的啊?好吧,这篇文章确实没有说这些深层次的东西,推荐这篇文章《bind原理图释》,希望这篇文章能满足你哦。

  • 相关阅读:
    微信小程序(9)--音频及视频弹幕
    VUE 插槽
    ES6 export
    实现服务端向客户端推消息
    制作镜像语言网站
    xss 防护总结
    一步一步webpack 3
    一步步学习webpack2 配置
    一步步学习webpack 1
    node 插件
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/14148466.html
Copyright © 2011-2022 走看看