【STL十六】函数对象:包装器(std::function)——绑定器(std::bind)——函数适配器

一、包装器(std::function)

1、简介

  • std::function模板类是一个通用的可调用对象的包装器,
  • 作用:简单的、统一的方式处理可调用对象。

std::function 的实例能存储、复制及调用任何可调用 (Callable) 目标——函数、 lambda 表达式、 bind 表达式或其他函数对象,还有指向成员函数指针和指向数据成员指针。存储的可调用对象被称为 std::function 的目标。
若 std::function 不含目标,则称它为空。调用空 std::function 的目标导致抛出 std::bad_function_call 异常。

2、头文件

#include <functional>

3、构造函数

template< class >
class function; 

4、demo

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

// 普通函数
void show(int bh, const string& message) {
    
    
	cout << "普通函数" << bh << "," << message << endl;
}

struct AA	// 类中有静态成员函数。
{
    
    
	static void show(int bh, const string& message) {
    
    
		cout << "成员函数" << bh << "," << message << endl;
	}
};

struct BB	// 仿函数。
{
    
    
	void operator()(int bh, const string& message) {
    
    
		cout << "仿函数 " << bh << "," << message << endl;
	}
};

struct CC	// 类中有普通成员函数。
{
    
    
	void show(int bh, const string& message) {
    
    
		cout << "成员函数" << bh << "," << message << endl;
	}
};
 
struct DD		                                 // 可以被转换为普通函数指针的类。
{
    
    
	using Fun = void (*)(int, const string&);    // 函数指针的别名。
	operator Fun() {
    
    
		return show;	                         // 返回普通函数show的地址。
	}
};

int main()
{
    
    
	using Fun = void(int, const string&);             // 函数类型的别名。

	// 普通函数。
	void(*fp1)(int, const string&) = show;	          // 声明函数指针,指向函数对象。
	fp1(1, "大唐不夜城。");						      // 用函数指针调用普通函数。
	function<void(int, const string&)> fn1 = show;    // 包装普通全局函数show。等价于 function<Fun> fn1 = show;
	fn1(1, "大唐不夜城。");							// 用function对象调用普通全局函数show。

	// 类的静态成员函数。
	void(*fp3)(int, const string&) = AA::show;	        // 用函数指针指向类的静态成员函数。
	fp3(2, "古城濮阳。");							    // 用函数指针调用类的静态成员函数。
	function<void(int, const string&)> fn3 = AA::show;	// 包装类的静态成员函数。
	fn3(2, "古城濮阳。");							    // 用function对象调用类的静态成员函数。

	// 仿函数。
	BB bb;
	bb(3, "古城西安。");		                        // 用仿函数对象调用仿函数。
	function<void(int, const string&)> fn4 = BB();		// 包装仿函数。
	fn4(3, "古城西安。");							    // 用function对象调用仿函数。

	// 创建lambda对象。
	auto lb = [](int bh, const string& message) {
    
    
		cout << "lambda表达式" << bh << "," << message << endl;
	};
	lb(4, "古城咸阳。");                                   // 调用lambda函数。
	function<void(int, const string&)> fn5 = lb;		   // 包装lamba函数。
	fn5(4, "古城咸阳。");								   // 用function对象调用lamba函数。

	// 类的非静态成员函数。
	CC cc;
	void (CC:: * fp11)(int, const string&) = &CC::show;		    // 定义类成员函数的指针。
	(cc.*fp11)(5, "咸阳秦始皇。");								// 用类成员函数的指针调用类的成员函数。
	function<void(CC&, int, const string&)> fn11 = &CC::show;	// 包装成员函数。
	fn11(cc, 5, "咸阳秦始皇。");						  		   // 用function对象调用成员函数。

	// 可以被转换为函数指针的类对象。
	DD dd;
	dd(6, "钟楼。");						            // 用可以被转换为函数指针的类对象调用普通函数。
	function<void(int, const string&)> fn12 = dd;	// 包装可以被转换为函数指针的类。
	fn12(6, "钟楼。");								// 用function对象调用它。
}

输出

普通函数1,大唐不夜城。
普通函数1,大唐不夜城。
成员函数2,古城濮阳。
成员函数2,古城濮阳。
仿函数 3,古城西安。
仿函数 3,古城西安。
lambda表达式4,古城咸阳。
lambda表达式4,古城咸阳。
成员函数5,咸阳秦始皇。
成员函数5,咸阳秦始皇。
普通函数6,钟楼。
普通函数6,钟楼。

5、异常

  • std::function对象未包装可调用对象,使用std::function对象将抛出std::bad_function_call异常
  • demo
#include <iostream>
#include <functional>
using namespace std;

// 普通函数
void show(int bh, const string& message) {
    
    
	cout << "普通函数" << bh << "," << message << endl;
}
 
struct DD		                                 // 可以被转换为普通函数指针的类。
{
    
    
	using Fun = void (*)(int, const string&);    // 函数指针的别名。
	operator Fun() {
    
    
		return show;	                         // 返回普通函数show的地址。
	}
};

int main()
{
    
    
	DD dd;

	function<void(int, const string&)> fx /*= dd*/;
	try {
    
      
		 fx(1, "xxxxxxxxxxxxxx。");
	}
	catch (std::bad_function_call e) {
    
    
		cout << "抛出了std::bad_function_call异常。";
	}
}

输出

抛出了std::bad_function_call异常。

二、绑定器(std::bind)

1、简介

  • std::bind()模板函数是一个通用的函数适配器(绑定器),
  • 它用一个可调用对象及其参数,生成一个新的可调用对象,以适应模板。
  • 接收一个函数名作为参数,生成一个新的函数。

使用std::bind可以将可调用对象和参数一起绑定,绑定后的结果使用std::function进行保存,并延迟调用到任何我们需要的时候。

2、头文件

#include <functional>

3、构造函数

template< class Fx, class... Args >
  	function<> bind (Fx&& fx, Args&...args);
  • Fx:需要绑定的可调用对象(可以是前两节课介绍的那六种,也可以是function对象)。
  • args:绑定参数列表,可以是左值、右值和参数占位符std::placeholders::_n,如果参数不是占位符,缺省为值传递,std:: ref(参数)则为引用传递。
  • std::bind()返回std::function的对象。

std::bind()的本质是仿函数。

4、demo:简单场景

  • bind场景:普通函数绑定、参数位置不同、少一个参数、多一个参数;
  • _1,_n在命名空间std::placeholders内
#include <iostream>
#include <functional>
using namespace std;

// 普通函数
void show(int bh, const string& message) {
    
    
	cout << "第" << bh << "个," << message << endl;
}

int main()
{
    
    
	function<void(int, const string&)> fn1 = show;
	function<void(int, const string&)> fn2 = bind(show, placeholders::_1, placeholders::_2);
	fn1(1, "hello 西安。");
	fn2(2, "hello 西安。");

	// 调用参数位置
	function<void(const string&, int)> fn3 = bind(show, placeholders::_2, placeholders::_1);
	fn3("hello 西安。", 3);

	// 少一个参数
	function<void(const string&)> fn4 = bind(show, 4, placeholders::_1);
	fn4("hello 西安。");

	// 多一个参数
	function<void(int, const string&, int)> fn5 = bind(show, placeholders::_1, placeholders::_2);
	fn5(5, "hello 西安。", 88);
}

输出

第1个,hello 西安。
第2个,hello 西安。
第3个,hello 西安。
第4个,hello 西安。
第5个,hello 西安。

5、demo:值传递、引用传递

  • 值传递
#include <iostream>
#include <functional>
using namespace std;

// 普通函数
void show(int bh, const string& message) {
    
    
	cout << "第" << bh << "个," << message << endl;
}

int main()
{
    
    

	// 少一个参数
	int n = 1;
	function<void(const string&)> fn1 = bind(show, n, placeholders::_1);
	fn1("hello 西安。");
	n = 2;
	fn1("hello 西安。");
}

输出

第1个,hello 西安。
第1个,hello 西安。

  • 引用传递
#include <iostream>
#include <functional>
using namespace std;

// 普通函数
void show(int bh, const string& message) {
    
    
	cout << "第" << bh << "个," << message << endl;
}

int main()
{
    
    
	// 少一个参数
	int n = 1;
	function<void(const string&)> fn1 = bind(show, std::ref(n), placeholders::_1);
	fn1("hello 西安。");
	n = 2;
	fn1("hello 西安。");
}

输出

第1个,hello 西安。
第2个,hello 西安。

6、demo:绑定6种可调用对象

  • bind 可以把六种可调用对象的调用方式统一起来
#include <iostream>
#include <functional>
using namespace std;

// 普通函数
void show(int bh, const string& message) {
    
    
	cout << "第" << bh << "个," << message << endl;
}

struct AA	// 类中有静态成员函数。
{
    
    
	static void show(int bh, const string& message) {
    
    
		cout << "第" << bh << "个," << message << endl;
	}
};

struct BB	// 仿函数。
{
    
    
	void operator()(int bh, const string& message) {
    
    
		cout << "第" << bh << "个," << message << endl;
	}
};

struct CC	// 类中有普通成员函数。
{
    
    
	void show(int bh, const string& message) {
    
    
		cout << "第" << bh << "个," << message << endl;
	}
};

struct DD		// 可以被转换为普通函数指针的类。
{
    
    
	using Fun = void (*)(int, const string&);    // 函数指针的别名。
	operator Fun() {
    
    
		return show;	// 返回普通函数show的地址。
	}
};

int main()
{
    
    
	// 普通函数。
	function<void(int, const string&)> fn1 = bind(show, placeholders::_1, placeholders::_2);    // 绑定普通全局函数show。
	fn1(1, "长安归故里。");										                                // 用function对象调用普通全局函数show。

	// 类的静态成员函数。
	function<void(int, const string&)> fn3 = bind(AA::show, placeholders::_1, placeholders::_2);	// 绑定类的静态成员函数。
	fn3(2, "长安归故里。");										                           		   // 用function对象调用类的静态成员函数。

	// 仿函数。
	function<void(int, const string&)> fn4 = bind(BB(), placeholders::_1, placeholders::_2);		// 绑定仿函数。
	fn4(3, "长安归故里。");									                                	     // 用function对象调用仿函数。

	// 创建lambda对象。
	auto lb = [](int bh, const string& message) {
    
    
		cout << "第" << bh << "个," << message << endl;
	};
	function<void(int, const string&)> fn5 = bind(lb, placeholders::_1, placeholders::_2);			// 绑定lamba函数。
	fn5(4, "长安归故里。");										                                     // 用function对象调用lamba函数。

	// 类的非静态成员函数。
	CC cc;
	//function<void(CC&, int, const string&)> fn11 = bind(&CC::show, placeholders::_1, placeholders::_2, placeholders::_3);		// 绑定成员函数。
	//fn11(cc, 5, "我是一只傻傻鸟。");										             	                // 用function对象调用成员函数。
	function<void(int, const string&)> fn11 = bind(&CC::show, &cc, placeholders::_1, placeholders::_2);		// 绑定成员函数。
	fn11(5, "长安归故里。");											                                    // 用function对象调用成员函数。

	// 可以被转换为函数指针的类对象。
	DD dd;
	function<void(int, const string&)> fn12 = bind(dd, placeholders::_1, placeholders::_2);			// 绑定可以被转换为函数指针的类。
	fn12(6, "长安归故里。");										                                // 用function对象调用它。
}

输出

第1个,长安归故里。
第2个,长安归故里。
第3个,长安归故里。
第4个,长安归故里。
第5个,长安归故里。
第6个,长安归故里。

三、函数适配器

我们在【STL十二】适配器——容器适配器介绍过:stl适配器分为 、容器适配器、迭代器适配器、函数适配器(function adapters)。

1、函数适配器(function adapters)

  • 函数适配器:通过转换或者修改其他函数对象使其功能得到扩展。可以将预先定义的仿函数和其他数值结合在一起。
  • 函数适配器种类
    • 否定器
    • 绑定器
    • 函数指针适配器

2、很多函数适配器在c++17就被废弃了

从c++标准相关网站我们可以看到,比如,1、C++ STL 容器库 中文文档,很多函数适配器在c++17开始,就被废弃了。所以我们就不再介绍了。
在这里插入图片描述

3、bind就是一种函数适配器(绑定器)

在这里插入图片描述

参考
1、C++ STL 容器库 中文文档
2、STL教程:C++ STL快速入门
3、https://www.apiref.com/cpp-zh/cpp/header.html
4、https://en.cppreference.com/w/cpp/container
5、WIKI教程_C ++标准库_C++ Library - <iterator>
6、哔哩哔哩_系统化学习C++_C++11神器之可调用对象包装器和绑定器

猜你喜欢

转载自blog.csdn.net/junxuezheng/article/details/129911988