您好,欢迎来到华拓科技网。
搜索
您的当前位置:首页【C++】:bind绑定器和function函数对象机制

【C++】:bind绑定器和function函数对象机制

来源:华拓科技网

⛺️不负时光,不负己✈️

引言

在C++中工程实践中,bind绑定器和function函数对象非常常用,而且bind+function简直就是无敌的存在。本篇博客中,我们力求用最小的成本搞懂它们,让你用起来得心应手

function函数对象

以减少理解成本为目地,我们先学习function。

function引入

function的作用是将具有相同调用形式的不同类型可调用对象进行类型统一。

相同的调用形式可以简单理解为:参数列表和返回值相同。

C++常见可调用对象有:函数、指针、匿名函数(lambda表达式)、函数对象(重载了函数调用运算符的类)以及使用bind创建的对象。
例如以下几个可调用对象具有相同的调用形式:

// 函数
int add(int a, int b) {
    return a + b;
}

// lambda表达式
auto sub = [](int a, int b) -> int {
  return a - b;
};

// 函数对象
class prod{
public:
    int operator() (int a, int b) {
        return a * b;
    }
};

这些可调用对象的调用形式相同,甚至拥有相同的功能,但是却由于类型千差万别无法统一处理。例如,我们想要统一调用以上可调用对象,使用‘+’来调用add函数,而使用‘-’调用sub匿名函数…
使用分支语句(if else if else)当然是可以做到的,但更推荐以下方式:

/*  使用map映射字符到可调用对象。
	但需要注意,映射的前提是function对不同类型的可调用对象
进行了类型统一。 */
map<char, function<int(int, int)>> cacul{
    {'+', add},
    {'-', sub},
    {'*', prod()}
};

cout << cacul['*'](5, 4) << endl;

细讲function

让我们来感受一下:
function的模板是 std::function<返回值类型(传入参数类型)> 方法名
这里传入参数类型可以是自己定义的

举几个简单的例子:

function<int(int, int)> func

这表示:定义了一个返回值为int,参数有两个,从左往右为int,int类型的函数指针

int sum(int a, int b)
{
	return a + b;
}
int main()
{
	using func_t = function<int(int, int)>;
	func_t func = sum;
	cout<<func(10,10);
}
int main()
{
	function<int(int, int)> func = sum;
	cout<<func(10,10);
}

用也许这样更容易被理解。

体验function在工程实践中的优势

假如我们要设计一个图书管理系统,该系统提供的服务有:借书、查询书、还书。假设这些函数的函数签名都是一样的「即返回值类型和参数类型都是相同的」。我们习惯将服务编号,比如用户输入1表示要获取借书服务,用户输入2表示查询书服务,用户输入3表示还书服务。
此时的开发者有两种设计方案「大体框架,不包括不同模块设计的具体细节」

  • 使用switch判断语句,但是这种框架的劣性在于如果增加一个模块需要进行大范围修改。
  • 使用function和map数据结构相结合。

接下来我们重点介绍第二种框架
本着说明问题,其他一切从简的原则 我们可以这样设计


#include<iostream>
#include<functional>
#include<map>
using namespace std;
void borrow()
{
	std::cout << "borrow books" << std::endl;
}
void lend()
{
	std::cout << "Return the book" << std::endl;

}
void search()
{
	std::cout << "Search book" << std::endl;
}
int main()
{
	using func_t = std::function<void()>;
	std::map<int, func_t> Map;
	Map.insert({ 1,borrow });
	Map.insert({ 2,lend });
	Map.insert({ 3,search });
	while (1)
	{
		int option = 0;
		std::cout << "请选择:";
		std::cin >> option;
		auto it = Map.find(option);
		if (it == Map.end())
		{
			cout << "没有这项服务,请重新选择:" << endl;
		}
		else
		{
			it->second();
		}
	}
}

模拟实现function函数对象机制

如上,我们系统的介绍了function的使用方法和应用,但是它究竟是如何实现的呢?我们一切来探究一下

#include<iostream>
#include<string>
template<class R,class A>
class myfunction<R(A)>
{
public:
	using PFUNC = R(*)A;
	myfunction(PFUNC pfunc)
		:_pfunc(pfunc)
	{}
	R operator()(A ter)
	{
		return _pfunc(str);
	}
priavte:
	PFUNC _pfunc;

};
void hello(std::string str)
{
	std::cout << str << std::endl;
}
int main()
{
	myfunction<void(std::string)> func_t = hello;
	func_t("hello world");
}

但是如果函数签名发生变化,我们就得写不同的部分特例化模板,其实我们可以这样做:

template<class T,class... A1>
class my_function<T(A1...)>
{
public:
	using func_t = T(*)(A1...);
	my_function(func_t func)
		:func_(func)
	{}
	R operator()(A1... args)
	{
		return func_(args...);
	}
private:
	func_t func_;
};

这就是模板的强大之处。

bind绑定器

关于bind绑定器,百度百科是这样说的:
std::bind 是 C++11 引入的一个标准库函数,它位于 functional 头文件中。std::bind 可以用来绑定函数的参数,或者将成员函数和对象绑定在一起,生成一个新的可调用对象(也称为函数对象)。这个新生成的对象可以像普通函数一样被调用,但其内部实际上会调用我们最初绑定的那个函数或成员函数。

基本语法

#include <functional> // 包含 std::bind 的头文件

auto bound_function = std::bind(function, arg1, arg2, ..., argN);
  • function 是要绑定的函数或可调用对象。
  • arg1, arg2, ..., argN 是传递给 function 的参数,可以是具体的值,也可以是占位符 _1, _2, ...(这些占位符定义在 <placeholders> 头文件中,通常通过 std::placeholders::_1 等方式访问)。

示例

1. 绑定普通函数
#include <iostream>
#include <functional>

void print_sum(int a, int b) {
    std::cout << "Sum: " << a + b << std::endl;
}

int main() {
    auto bound_print_sum = std::bind(print_sum, 5, 10);
    bound_print_sum(); // 输出: Sum: 15
    return 0;
}
2. 使用占位符
#include <iostream>
#include <functional>
#include <placeholders> // 包含 std::placeholders 的头文件

void print_sum(int a, int b) {
    std::cout << "Sum: " << a + b << std::endl;
}

int main() {
    using namespace std::placeholders;
    auto bound_print_sum = std::bind(print_sum, _1, 10);
    bound_print_sum(5); // 输出: Sum: 15
    bound_print_sum(20); // 输出: Sum: 30
    return 0;
}
3. 绑定成员函数
#include <iostream>
#include <functional>
#include <placeholders>

class MyClass {
public:
    void print_sum(int a, int b) {
        std::cout << "Sum: " << a + b << std::endl;
    }
};

int main() {
    MyClass obj;
    using namespace std::placeholders;
    auto bound_print_sum = std::bind(&MyClass::print_sum, &obj, _1, 10);
    bound_print_sum(5); // 输出: Sum: 15
    return 0;
}

注意,在绑定成员函数时,第一个参数需要是对象的指针或引用。

4. 绑定 lambda 表达式
#include <iostream>
#include <functional>
#include <placeholders>

int main() {
    auto lambda = [](int a, int b) { std::cout << "Sum: " << a + b << std::endl; };
    auto bound_lambda = std::bind(lambda, _1, 20);
    bound_lambda(10); // 输出: Sum: 30
    return 0;
}

注意事项

通过上面的示例和解释,你应该对 std::bind 的基本用法有了初步的了解。在实际开发中,根据具体需求选择合适的工具和方法来实现功能。

使用bind+function完成线程池设计

何为线程池?

线程池是一种高效的设计方案。我们事先准备若干个线程,然后给不同的线程分配不同的任务。

设计代码

#include<iostream>
#include<functional>
#include<string>
#include<thread>
#include<vector>
using namespace std;
using namespace std::placeholders;
class Thread
{
public:
	Thread(function<void()>Way)
		:_Way(Way)
	{}
	thread start()
	{
		thread t(_Way);
		return t;

	}
private:
	function<void()> _Way;
};
class ThreadPoll
{
public:
	ThreadPoll()
	{}
	~ThreadPoll()
	{
		for (auto it : _poll)
		{
			delete it;
		}
	}
	void start(int size)
	{
		for (int i = 0; i < size; i++)
		{
			_poll.push_back(new Thread(std::bind(&ThreadPoll::ThreadWay, this, i)));

		}
		for (auto it : _poll)
		{
			_hander.push_back(it->start());
		}
		for (auto &it : _hander)
		{
			it.join();
		}
	}
private:
	void ThreadWay(int i)
	{
		cout << "thread %d run........" << i << endl;
	}
	vector<Thread*> _poll;
	vector<thread> _hander;
};
int main()
{
	ThreadPoll it;
	it.start(20);
}
``

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- huatuo6.cn 版权所有 赣ICP备2024042791号-9

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务