c++11_14学习之函数对象包装器function与bind
创始人
2025-05-28 21:16:28

文章目录

  • 1.可调用对象
    • 1.1 函数指针
    • 1.2 函数对象
    • 1.3 可被转换为函数指针的类对象
    • 1.4 类成员函数指针
  • 2.std::function
    • 2.1 包装普通函数
    • 2.2 包装函数指针
    • 2.3 包装函数对象
    • 2.4 包装可被转换为函数指针的类对象
    • 2.5 包装类的静态成员函数
    • 2.6 包装类的非静态成员函数
  • 3.std::bind()
    • 3.1 绑定普通函数
    • 3.2 placeholders占位符
    • 3.4 绑定类的成员函数
  • 4.std::bind()和std::function配合使用


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 函数对象

仿函数是一个重载了 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.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.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;
}

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 myf = fun;myf(100);return 0;
}

2.2 包装函数指针

#include 
#include 
using namespace std;void fun(int v)
{cout << "fun()函数执行了,v = " << v << endl;
}int main()
{void(*pfun)(int) = fun;std::function myf = pfun;myf(15);return 0;
}

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 myf = tc;myf(100);return 0;
}

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 myf = tc;myf(100);return 0;
}

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 myf = TC::fun;cout << myf(100) << endl;return 0;
}

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 myf = &TC::fun;cout << myf(TC(), 100) << endl;return 0;
}

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;
}

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;
}
#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;
}

需要注意的是,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;
}

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;
}

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;
}

在下面代码中,第二个参数为 &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;
}

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 bf = std::bind(&CQ::fun, cq, std::placeholders::_1, std::placeholders::_2);bf(10, 30);return 0;
}

原文链接:https://blog.csdn.net/qq_42815188/article/details/128056305

相关内容

热门资讯

荼蘼什么意思 岁月缱绻葳蕤生香... 感谢作者【辰夕】的原创独家授权分享编辑整理:【多肉植物百科】百科君坐标:云南 曲靖春而至,季节流转,...
excel表格怎么筛选 表格如... 现在需要在业绩单里重点跟踪笔记本电脑的六七八月销售业绩,如何能够快速筛选出来?方法很简单,分三步走:...
长白山自助游攻略 吉林长白山游... 昨天介绍了西坡的景点详细请看链接:一个人的旅行,据说能看到长白山天池全凭运气,您的运气如何?今日介绍...
应用未安装解决办法 平板应用未... ---IT小技术,每天Get一个小技能!一、前言描述苹果IPad2居然不能安装怎么办?与此IPad不...
excel表格怎么筛选 表格如... 现在需要在业绩单里重点跟踪笔记本电脑的六七八月销售业绩,如何能够快速筛选出来?方法很简单,分三步走:...