C++11新特性之std::function和bind绑定器
在C++中,存在可调用对象这一个概念,可调用对象有以下几种定义:
(1).是一个函数指针
(2).是一个具有operator()成员函数的类对象(仿函数)
(3).是一个可被转换为函数指针的类对象
(4).是一个类成员(函数指针)
一、可调用对象包装器----std::function
std::function是可调用对象的包装器,它是一个类模板,可以容纳除了类成员(函数)指针之外的所有可调用对象。通过执行它的类模板参数,它可以用统一的方式处理函数、函数对象、函数指针,并允许保存和延迟执行它们。
#include <QCoreApplication> #include <QDebug> void func() { qDebug() << __FUNCTION__; } class Foo { public: static int foo_func(int a) { qDebug() << __FUNCTION__ <<"Foo"; return a; } }; class Bar { public: int operator()(int a) { qDebug() <<__FUNCTION__ <<"Bar"; return a; } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); /* 绑定一个普通函数 */ std::function<void()> fr1 = func; fr1(); /* 绑定一个类的静态成员函数 */ std::function<int(int)> fr2 = Foo::foo_func; qDebug() << fr2(123); /* 绑定一个仿函数 */ Bar bar; fr2 = bar; qDebug() << fr2(123); return a.exec(); }
运行结果如下:
再来看下面的示例:
#include <QCoreApplication> #include <QDebug> class A { public: A(const std::function<void()>& f):callback_(f){}; void notify() { callback_(); /* 回调到上层 */ } private: std::function<void()> callback_; }; class Foo { public: void operator()() { qDebug() <<__FUNCTION__; } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); Foo foo; A aa(foo); aa.notify(); return a.exec(); }
从上面的示例中可以看出,std::function可以取代函数指针的作用,因为它可以保存函数延迟执行,所以比较适合作为回调函数。
二、std::bind绑定器
std::bind用来将可调用对象与其参数一起进行绑定,绑定后的结果可以使用std::function进行保存,并延迟调用到任何我们需要使用它的时候。
它主要有两大作用:
(1).将可调用对象与其参数一起绑定成一个仿函数
(2).将多元(参数个数为n,n>1)可调用对象转成一元或者(n-1)元可调用对象,即只绑定部分参数
#include <QCoreApplication> #include <QDebug> void call_when_even(int x, const std::function<void(int)>& f) { if (! (x & 1)) { f(x); } } void output(int x) { qDebug() << x; } void output_add_2(int x) { qDebug() << x + 2; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); { auto fr = std::bind(output, std::placeholders::_1); for (int i = 0; i <10; ++i) { call_when_even(i, fr); } } { auto fr = std::bind(output_add_2, std::placeholders::_1); for (int i = 0; i <10; ++i) { call_when_even(i, fr); } } return a.exec(); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?