c++11 std::bind使用

何为bind

bind是这样一种机制,它可以预先把指定可调用实体的某些参数绑定到已有的变量,产生一个新的可调 用实体,这种机制在回调函数的使用过程中也颇为有用。其实最早在C++98的时候,就已经有了std::bind1st和std::bind2nd分别用来绑定functor的两个参数,具体代码就不演示了,查查资料就知道了。这个特性在当时并没有引起太多的重视,可以说是食之无味。
C++11中提供了std::bind,可以说是一种飞跃的提升,bind本身是一种延迟计算的思想,它本身可以绑定普通函数、全局函数、静态函数、类静态函数甚至是类成员函数。

#include <iostream>
#include <functional>
using namespace std;

int TestFunc(int a, char c, float f)
{
    cout << a << endl;
    cout << c << endl;
    cout << f << endl;

    return a;
}

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

    cout << "=================================\n";

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

    cout << "=================================\n";

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

    return 0;
}

从上面的代码可以看到,bind能够在绑定时候就同时绑定一部分参数,未提供的参数则使用占位符表示,然后在运行时传入实际的参数值。

PS:绑定的参数将会以值传递的方式传递给具体函数,占位符将会以引用传递。

众所周知,静态成员函数其实可以看做是全局函数,而非静态成员函数则需要传递this指针作为第一个参数,所以std::bind能很容易地绑定成员函数。

bind最终将会生成一个可调用对象,这个对象可以直接赋值给std::function对象,而std::bind绑定的可调用对象可以是Lambda表达式或者类成员函数等可调用对象,它能随意绑定任何函数,将所有的函数都能统一到std::function


来自:http://segmentfault.com/a/1190000003897709


  1 #include <iostream>
  2 #include <typeinfo>
  3 #include <functional>
  4 #include <string.h>
  1 #include <iostream>
  2 #include <typeinfo>
  3 #include <functional>
  4 #include <string.h>
  5 using namespace std;
  6 
  7 int add(int a, int b, int c)
  8 {
  9         return a+b+c;
 10 }
 11 
 12 class Utils{
 13 public:
 14         Utils(const char* name){
 15                 strcpy(_name, name);
 16         }
 17 
 18         void SayHello(const char* name) const{
 19                 std::cout<<_name<<" say : hello "<<name<<endl;
 20         }
 21 
 22         static int getId(){
 23                 return 1001;
 24         }
 25 
 26 
 27 private:
 28         char _name[32];
 29 };

 32 int main()
 33 {
 34         auto add2 = std::bind(add, std::placeholders::_1, 1, 2);
 35         int i=add2(5);
 36         std::cout<<i<<std::endl;
 37         std::cout<<typeid(add2).name()<<endl;
 38 
 39         cout<<"------------------------------------"<<endl;
 40 
 41         Utils util("xiaoming");
 42         //绑定类成员
 43         auto sayHello = std::bind(&Utils::SayHello, util, std::placeholders::_1);
 44         sayHello("xiaodonmg");
 45 
 46         auto SayHelloKit = std::bind(&Utils::SayHello, util, "kit");
 47         SayHelloKit();
 48 
 49 
 50         //绑定静态成员
 51         auto getId = std::bind(&Utils::getId);
 52         cout<<getId()<<endl;
 53 
 54 
 55 
 56         return 0;
 57 }
 58 




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值