c++11新特性——function和bind

本文介绍了C++11中std::function和std::bind的使用,展示了如何通过它们封装不同类型的可调用对象。std::function作为一个通用的函数包装器,可以包装普通函数、函数指针、仿函数和lambda表达式,实现函数表的制作。而std::bind则是一个函数适配器,允许预设部分参数并生成新的可调用对象。通过示例代码详细解释了两者的应用和区别。
摘要由CSDN通过智能技术生成

背景

在C++11中,提供了std::function和std::bind两个方法来对可调用对象进行统一和封装。

一、什么是可调用对象

  • 普通函数
  • 函数指针
  • 仿函数(类中重载()运算符)
  • lambda 表达式

可调用对象:简单来说就是,可以像函数那样加个()就可以调用的对象

1.1 不同类型的可调用对象举例
int add(int, int) { return a+b; }  // 普通函数
auto sub = [](int a, int b) { return a-b; } // lambda表达式
struct mod{
	int operator()(int a, int b) { return a%b; }  // 仿函数
};

上面三种不同类型的可调用对象,有相同的调用形式。假设,我们需要将上述函数对象做成一个函数表来调用,代码示例:

#include <iostream>
#include <map>

using namespace std;
int add(int a, int b) { return a+b; }  // 普通函数
auto sub = [](int a, int b) { return a-b; }; // lambda表达式
struct mod{
	int operator()(int a, int b) { return a%b; }  // 仿函数
};

map<char, int(*)(int,int)> _table;
int main()
{
	_table.insert({'+', add});
	_table.insert({'-', sub});
	// _table.insert({'-', mod()});  // 编译报错
	int ret = _table['+'](10, 20);  // 等价于add(10,20);
	std::cout << ret << std::endl;

	ret = _table['-'](30, 20);     // 等价于sub(30,20);
	std::cout << ret << std::endl;
	
	// ret = _table['%'](10, 2);
	// std::cout << ret << std::endl;
	return 0;
}

仿函数插入失败,这个时候,function登场!

二、function函数包装器

std::function是一个函数包装器,包含在头文件#include《functional》中。该函数包装器模板能包装任何类型的可调用实体,如普通函数、函数对象、lambda表达式。

2.1 function 完成1.1函数表制作
#include <iostream>
#include <map>
#include <functional>  // 包含头文件
using namespace std;

int add(int a, int b) { return a+b; }  // 普通函数
auto sub = [](int a, int b) { return a-b; }; // lambda表达式
struct mod{
	int operator()(int a, int b) { return a%b; }  // 仿函数
};
// 重新声明map
map<char, function<int(int,int)>> _table; 
int main()
{
	function<int(int,int)> func1 = add;
	function<int(int,int)> func2 = sub;
	function<int(int,int)> func3 = mod();
	_table.insert({'+', func1});
	_table.insert({'-', func2});
	_table.insert({'%', func3});  

	int ret = _table['+'](10, 20); // 等价于add(10,20);
	std::cout << ret << std::endl;
	ret = _table['-'](30, 20);    // 等价于sub(30,20);
	std::cout << ret << std::endl;
	ret = _table['%'](10, 2);    // 等价于mod(10,2);
	std::cout << ret << std::endl;
	return 0;
}

运行结果:
在这里插入图片描述

2.2 function 基本语法

完整代码示例

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

void func1(int a) { cout << a << endl; }

class A{ 
public: 
	A(string name) : name_(name){} 
	void func3(int i) const {
		cout <<name_ << ", " << i << endl;
	} 
private: 
	string name_;
};


int main() { 
	cout << "----------- main1 -----------------" << endl; 
	//1. 保存普通函数 
	std::function<void(int a)> func1_; 
	func1_ = func1; 
	func1_(2); 
	//2. 保存lambda表达式 
	cout << "------- main2 -----------------" << endl; 
	std::function<void()> func2_ = [](){cout << "hello lambda" << endl;}; 
	func2_(); //hello world 

	//3 保存成员函数 
	cout << "\n---------- main3 -----------------" << endl; 
	std::function<void(const A&,int)> func3_ = &A::func3;  // 别忘了&符号
	A a("lwang"); 
	func3_(a, 1); 
	return 0;
}

运行结果:
在这里插入图片描述

三、bind 绑定器

  1. 只是绑定函数,参数需要自己传;
  2. 绑定函数的时候,也把参数绑定
3.1 作用:

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

3.2 用法:
template<class F, class… Args>
bind(F&&f, Args&&… args);  

auto newCallable = bind(callable, arg_list);
3.3 代码示例
#include <iostream> 
#include <functional> 
using namespace std; 
class A {
public: 
	void fun_3(int k,int m) { 
		cout<<"print: k="<<k<<",m="<<m<<endl; 
	} 
};

void fun_1(int x, int y, int z) { 
	cout<<"print: x=" <<x<<", y="<< y << ", z=" <<z<<endl; 
}
void fun_2(int &a,int &b) { 
	a++; 
	b++; 
	cout<<"print: a=" <<a<<",b="<<b<<endl; 
}

int main(int argc, char * argv[]) 
{ 
	//f1的类型为 function<void(int, int, int)> 
	auto f1 = std::bind(fun_1, 1, 2, 3); //表示绑定函数 fun 的第一,二,三个参数值为: 1 2 3 
	f1(); 

	//表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f2 的第一,二个参数 指定3 可变模板参数
	auto f2 = std::bind(fun_1, placeholders::_1,placeholders::_2,3); 
	f2(1,2);

	//表示绑定函数 fun 的第三个参数为 3,而fun 的第一,二个参数分别由调用 f3 的第二,一个参数 指定 //注意: f2 和 f3 的区别。
	auto f3 = std::bind(fun_1,placeholders::_2,placeholders::_1,3);  
	f3(1,2);

	int m = 2; 
	int n = 3; 
	//表示绑定fun_2的第一个参数为n, fun_2的第二个参数由调用f4的第一个参数(_1)指定。
	auto f4 = std::bind(fun_2, placeholders::_1, n);  
	f4(m); //print: m=3,n=4 
	cout<<"m="<<m<<endl;//m=3 说明:bind对于不事先绑定的参数,通过std::placeholders传递的参数是通过引用传递的,如m 
	cout<<"n="<<n<<endl;//n=3 说明:bind对于预先绑定的函数参数是通过值传递的,如n

	A a; 
	//f5的类型为 function<void(int, int)> 
	auto f5 = std::bind(&A::fun_3, a, placeholders::_1,placeholders::_2); //使用 auto关键字 
	f5(10,20);//调用a.fun_3(10,20),print: k=10,m=20 

	std::function<void(int,int)> fc = std::bind(&A::fun_3, a,std::placeholders::_1,std::placeholders::_2); 
	fc(10,20); //调用a.fun_3(10,20) print: k=10,m=20 
	return 0; 
}

3.3.1 运行结果:

在这里插入图片描述
3.3. 2 结论:

bind对于不事先绑定的参数,即通过std::placeholders传递的参数是通过引用传递的,bind对于预先绑定的函数参数是通过值传递的。

文章参考于<零声教育>的C/C++linux服务期高级架构

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值