【深入理解C++】可调用对象、std::function、std::bind()

1.可调用对象

1.1 函数指针

#include <iostream>
using namespace std;

void fun(int v)
{
	cout << "fun()函数执行了,v = " << v << endl;
}

int main()
{
	void(*pfun)(int) = fun;
	pfun(15);

	return 0;
}

1.2 函数对象

仿函数是一个重载了 operator() 运算符、能行使函数功能的类,这个类也称为函数对象类,这个类的对象就是函数对象。

函数对象本质上是一个对象,但其使用形式看起来和函数调用一样,因而得名。

#include <iostream>
using namespace std;

class TC
{
public:
	void operator()(int tv) // 函数调用运算符
	{
		cout << "TC::operator()执行了,tv = " << tv << endl;
	}
};

int main()
{
	TC tc;
	tc(20); // 等价于tc.operator()(20);

	return 0;
}

1.3 可被转换为函数指针的类对象

#include <iostream>
using namespace std;

class TC
{
public:
	using pfun = void(*)(int); // 使用using指定别名

	static void fun(int tv) // 静态成员函数
	{
		cout << "TC::fun()静态成员函数执行了,tv = " << tv << endl;
	}

	operator pfun() // 类型转换运算符,能把本类类型对象转换成一个函数指针
	{
		return fun;
	}
};

int main()
{
	TC tc;
	tc(50); // 等价于tc.operator TC::pfun()(50);

	return 0;
}

1.4 类成员函数指针

#include <iostream>
using namespace std;

class TC
{
public:
	void fun(int tv)
	{
		cout << "TC::fun()非静态成员函数执行了,tv = " << tv << endl;
	}
};

int main()
{
	TC tc;
	void(TC::*pfun)(int) = &TC::fun; // 类成员函数指针变量pfun的定义与初始化
	(tc.*pfun)(50); // 对象tc通过类成员函数指针pfun调用成员函数fun()

	return 0;
}

2.std::function

头文件为 #include <functional>

从上面的讲解中,我们可以发现:可调用对象的定义方法五花八门,保存和传递的过程很繁琐。为此,C++11 提供了解决方案,即可调用对象包装器 std::function。

std::function 是一个类模板,通过给它指定模板参数,它就能够用统一的方式来处理各种可调用对象,方便统一的保存和调用。

std::function 的应用场景:回调函数和作为函数参数。

2.1 包装普通函数

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

void fun(int v)
{
	cout << "fun()函数执行了,v = " << v << endl;
}

int main()
{
	std::function<void(int)> myf = fun;
	myf(100);

	return 0;
}

2.2 包装函数指针

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

void fun(int v)
{
	cout << "fun()函数执行了,v = " << v << endl;
}

int main()
{
	void(*pfun)(int) = fun;

	std::function<void(int)> myf = pfun;
	myf(15);

	return 0;
}

2.3 包装函数对象

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

class TC
{
public:
	void operator()(int tv) // 函数调用运算符
	{
		cout << "TC::operator()执行了,tv = " << tv << endl;
	}
};

int main()
{
	TC tc;
	std::function<void(int)> myf = tc;
	myf(100);

	return 0;
}

2.4 包装可被转换为函数指针的类对象

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

class TC
{
public:
	using pfun = void(*)(int); // 使用using指定别名

	static void fun(int tv) // 静态成员函数
	{
		cout << "TC::fun()静态成员函数执行了,tv = " << tv << endl;
	}

	operator pfun() // 类型转换运算符,能把本类类型对象转换成一个函数指针
	{
		return fun;
	}
};

int main()
{
	TC tc;
	std::function<void(int)> myf = tc;
	myf(100);

	return 0;
}

2.5 包装类的静态成员函数

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

class TC
{
public:
	static int fun(int tv) // 静态成员函数
	{
		cout << "TC::fun()静态成员函数执行了,tv = " << tv << endl;
		return tv;
	}
};

int main()
{
	// 静态成员函数不包含隐藏的this指针
	std::function<int(int)> myf = TC::fun;
	cout << myf(100) << endl;

	return 0;
}

2.6 包装类的非静态成员函数

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

class TC
{
public:
	int fun(int tv) // 非静态成员函数
	{
		cout << "TC::fun()非静态成员函数执行了,tv = " << tv << endl;
		return tv;
	}
};

int main()
{
	// 非静态成员函数包含一个隐藏的this指针,所以形参需要多定义一个类型
	std::function<int(TC, int)> myf = &TC::fun;
	cout << myf(TC(), 100) << endl;

	return 0;
}

3.std::bind()

头文件:#include <functional>

std::bind() 是一个函数模板,它接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。具体说来,std::bind() 用于将可调用对象及其参数绑定到一起,返回一个仿函数,因此可以直接调用,也可以使用 std::function 进行保存。

std::bind() 的思想实际上是一种延迟计算的思想,将可调用对象保存起来,然后在需要的时候再调用,而且这种绑定是非常灵活的,不论是普通函数、函数对象、还是成员函数都可以绑定,而且其参数可以支持占位符。

3.1 绑定普通函数

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

void fun(int x, int y, int z)
{
	cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}

int main()
{
	auto bf = std::bind(fun, 10, 20, 30);
	bf();

	return 0;
}

3.2 placeholders占位符

如果函数有多个参数,可以绑定部分参数,其他的参数在调用的时候指定。

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

void fun(int x, int y, int z)
{
	cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}

int main()
{
	auto bf = std::bind(fun, placeholders::_1, 20, placeholders::_2);
	bf(10, 30);

	return 0;
}

在这里插入图片描述

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

void fun(int x, int y, int z)
{
	cout << "x = " << x << ", y = " << y << ", z = " << z << endl;
}

int main()
{
	std::bind(fun, placeholders::_1, 20, placeholders::_1)(10, 30);

	return 0;
}

在这里插入图片描述

需要注意的是,std::bind() 对于预先绑定的函数参数是通过值传递的,对于通过 placeholders 占位的参数是通过引用传递的。

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

void fun(int& x, int& y)
{
	x++;
	y++;
}

int main()
{
	int a = 2;
	int b = 3;
	
	auto bf = std::bind(fun, a, placeholders::_1);
	bf(b);

	cout << "a = " << a << ", b = " << b << endl;

	return 0;
}

在这里插入图片描述

3.3 绑定函数对象

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

class CQ
{
public:
	CQ()
	{
		cout << "构造函数 - " << this << endl;
	}
	CQ(const CQ&)
	{
		cout << "拷贝构造函数 - " << this << endl;
	}
	~CQ()
	{
		cout << "析构函数 - " << this << endl;
	}
	void operator()() // 函数调用运算符
	{
		cout << "operator() - " << this << endl;
	}
};

int main()
{
	auto bf = std::bind(CQ());
	bf();

	return 0;
}

在这里插入图片描述

3.4 绑定类的成员函数

在下面代码中,第二个参数为 cq,这会导致调用 CQ 类的拷贝构造函数生成一个 CQ 类型的临时对象作为 std::bind() 的返回值。

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

class CQ
{
public:
	CQ()
	{
		cout << "构造函数 - " << this << endl;
	}
	CQ(const CQ&)
	{
		cout << "拷贝构造函数 - " << this << endl;
	}
	~CQ()
	{
		cout << "析构函数 - " << this << endl;
	}
	void fun(int x, int y)
	{
		cout << "x = " << x << ", y = " << y << endl;
	}
};

int main()
{
	CQ cq;
	auto bf = std::bind(&CQ::fun, cq, std::placeholders::_1, std::placeholders::_2);
	bf(10, 30);

	return 0;
}

在这里插入图片描述

在下面代码中,第二个参数为 &cq,这样就不生成 CQ 类型的临时对象了,此时 std::bind() 返回的就是 cq 对象本身。

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

class CQ
{
public:
	CQ()
	{
		cout << "构造函数 - " << this << endl;
	}
	CQ(const CQ&)
	{
		cout << "拷贝构造函数 - " << this << endl;
	}
	~CQ()
	{
		cout << "析构函数 - " << this << endl;
	}
	void fun(int x, int y)
	{
		cout << "x = " << x << ", y = " << y << endl;
	}
};

int main()
{
	CQ cq;
	auto bf = std::bind(&CQ::fun, &cq, std::placeholders::_1, std::placeholders::_2);
	bf(10, 30);

	return 0;
}

在这里插入图片描述

4.std::bind()和std::function配合使用

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

class CQ
{
public:
	CQ()
	{
		cout << "构造函数 - " << this << endl;
	}
	CQ(const CQ&)
	{
		cout << "拷贝构造函数 - " << this << endl;
	}
	~CQ()
	{
		cout << "析构函数 - " << this << endl;
	}
	void fun(int x, int y)
	{
		cout << "x = " << x << ", y = " << y << endl;
	}
};

int main()
{
	CQ cq;
	std::function<void(int, int)> bf = std::bind(&CQ::fun, cq, std::placeholders::_1, std::placeholders::_2);
	bf(10, 30);

	return 0;
}

在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值