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


    1.可调用对象

    1.1 函数指针

    #include 
    using namespace std;
    
    void fun(int v)
    {
    	cout << "fun()函数执行了,v = " << v << endl;
    }
    
    int main()
    {
    	void(*pfun)(int) = fun;
    	pfun(15);
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    1.2 函数对象

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

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

    #include 
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

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

    #include 
    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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    1.4 类成员函数指针

    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2.std::function

    头文件为 #include

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

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

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

    2.1 包装普通函数

    #include 
    #include 
    using namespace std;
    
    void fun(int v)
    {
    	cout << "fun()函数执行了,v = " << v << endl;
    }
    
    int main()
    {
    	std::function<void(int)> myf = fun;
    	myf(100);
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    2.2 包装函数指针

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    2.3 包装函数对象

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

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

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    2.5 包装类的静态成员函数

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

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

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3.std::bind()

    头文件:#include

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

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

    3.1 绑定普通函数

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3.2 placeholders占位符

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

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在这里插入图片描述

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

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    在这里插入图片描述

    3.3 绑定函数对象

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    在这里插入图片描述

    3.4 绑定类的成员函数

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

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述

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

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述

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

    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    在这里插入图片描述

  • 相关阅读:
    Locust如何实现逐步负载?
    人工智能热潮推动光芯片与光器件需求飙升
    C语言:贪吃蛇游戏(万字解读超详细)
    【考研】数据结构考点——直接选择排序
    网络安全的基本概念
    LeetCode(1)合并两个有序数组【数组/字符串】【简单】
    从0到1搭建Halo博客系统教程
    基于GAN的时序缺失数据填补前言(1)——RNN介绍及pytorch代码实现
    基于PLC控制的门禁系统设计【附源码】
    中介子方程二十二
  • 原文地址:https://blog.csdn.net/qq_42815188/article/details/128056305