C++11bind总结

bind函数的使用详解

可以将bind函数看作是一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。

调用bind的一般形式:auto newCallable = bind(callable,arg_list);`

其中,newCallable本身是一个可调用对象,arg_list是一个逗号分隔的参数列表,对应给定的callable的参数。即,当我们调用newCallable时,newCallable会调用callable,并传给它arg_list中的参数。

arg_list中的参数可能包含形如_n的名字,其中n是一个整数,这些参数是“占位符”,表示newCallable的参数,它们占据了传递给newCallable的参数的“位置”。数值n表示生成的可调用对象中参数的位置:_1为newCallable的第一个参数,_2为第二个参数,以此类推。

绑定普通函数:

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




int myplus(int a,int b, int c)
{
   printf("a = %d, b = %d, c = %d\r\n", a, b, c);
   return a+b+c;
}


int main()
{  
   auto func1 = std::bind(myplus, placeholders::_1, placeholders::_2, placeholders::_3);
   func1(1,2,3);
   //调用myplus时,第一个参数是func1的第一个参数(因为placeholders::_1出现在参数列表的第一个位置),第二个参数是func1的第二个参数,第三个参数是func1第三个参数
   //打印结果:a = 1, b = 2, c = 3
    
   auto func2 = std::bind(myplus, placeholders::_1, placeholders::_2, 4);
   func2(1,2);
   //调用myplus时,第一个参数是func2的第一个参数,第二个参数是func2的第二个参数,第三个参数固定为4
   //打印结果:a = 1, b = 2, c =4
          
   auto func3 = std::bind(myplus, placeholders::_3, placeholders::_2, placeholders::_1);
   func3(1,2,3);
   //调用myplus时,第三个参数是func3的第一个参数,第二个参数是func3的第二个参数,第三个参数为func3第一个参数
   //打印结果:a = 3, b = 2, c = 1   
    
   auto func4 = std::bind(myplus, 5, placeholders::_2, placeholders::_1);
   func4(1,2)
   //调用myplus时,第一个参数固定为5, 即a固定为5,,第二个参数(此时是c)是func4除了5的第一个,第一个参数(此时是b)是func4除了5的第二个
   //打印结果:a = 5, b = 2, c=1
   
   auto func5 = std::bind(myplus, 6, 7,8);
   func5();
   //调用myplus时,第一个参数固定为6,第二个参数固定为7,第三个参数固定为8 
   //打印结果:a = 6, b = 7, c =8

 
   
   func5(1,2);
   func5(1,2,3);
   func5(1,2,3,4);
   func5(1,2,3,4, "abc");
   //同上,此时不管传什么样的值给func5,打印结果都是a = 6, b = 7, c =8, 并且不会有编译的错误 
    
   return 0;
}

绑定类的成员函数

#include<iostream>
#include<functional>
using namespace std;
class Plus
{
   public:
   	int plus(int a,int b)
   	{
   	    return a+b;
   	}
}
int main()
{
   Plus p;
   // 指针形式调用成员函数
   auto func1 = std::bind(&Plus::plus,&p, placeholders::_1, placeholders::_2);
  // 对象形式调用成员函数
   auto func2 = std::bind(&Plus::plus,p, placeholders::_1, placeholders::_2);
   cout<<func1(1,2)<<endl; //3
   cout<<func2(1,2)<<endl; //3
   retunrn 0;
}

 绑定类静态成员函数

#include<iostream>
#include<functional>
using namespace std;
class Plus
{
	public:
		static int plus(int a,int b)
		{
		    return a+b;
		}
}
int main()
{
   auto func1 = std::bind(&Plus::plus, placeholders::_1, placeholders::_2);
   cout<<func1(1,2)<<endl; //3
   retunrn 0;
}

以下是使用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才是真正意义上的绑定。

参考链接:

C++11 bind函数_xqs_123的博客-CSDN博客_c++bind

C++11中的std::bind__大猪的博客-CSDN博客_c++ std::bind

C++ std::bind - 简书

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一条叫做nemo的鱼

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值