博客
关于我
【C++11】std::function和std::bind
阅读量:602 次
发布时间:2019-03-13

本文共 680 字,大约阅读时间需要 2 分钟。

std::function + std::bind使得所有函数能够统一到

std::function。在实际应用中,这种统一方式可以让类成员函数以一种优雅的方式被用在回调、线程函数等多个场景中。

以下是一个示例代码:

#include        #include          using std::placeholders::_1;using std::placeholders::_2;class Test{    public:    int add(const int a, const int b)    {        return a + b;    }}//int main(){    Test t;    std::function< int(int, int) > pfun = std::bind(&Test::add, &t, _1, _2);    //auto pfun = std::bind(&Test::add, &t, _1, _2);        // 直接调用      pfun(2, 3);     // 用于线程函数    std::thread t(pfun, 4, 6);     return 0;}

通过糖霜&std::bind,我们可以将类成员函数统一到标准的
std::function类型中,这使得函数能够方便地被用在多种场景中。

在实际应用中,std::function和std::bind提供了一种强大的方式来处理函数编织和灵活配置,这在诸如回调处理、线程函数执行等场景中尤为重要。

转载地址:http://ftyaz.baihongyu.com/

你可能感兴趣的文章
wxWidgets源码分析(8) - MVC架构
查看>>
wxWidgets源码分析(9) - wxString
查看>>
[白话解析] 深入浅出熵的概念 & 决策树之ID3算法
查看>>
[梁山好汉说IT] 梁山好汉和抢劫银行
查看>>
[源码解析] 消息队列 Kombu 之 基本架构
查看>>
[源码分析] 消息队列 Kombu 之 启动过程
查看>>
抉择之苦
查看>>
wx.NET CLI wrapper for wxWidgets
查看>>
Silverlight for linux 和 DLR(Dynamic Language Runtime)
查看>>
ASP.NET MVC Action Filters
查看>>
Windows SharePoint Services 3.0 Service Pack 2
查看>>
Powershell中禁止执行脚本解决办法
查看>>
HTTP协议状态码详解(HTTP Status Code)
查看>>
OO_Unit2 多线程电梯总结
查看>>
git clone 出现fatal: unable to access ‘https://github 错误解决方法
查看>>
04_Mysql配置文件(重要参数)
查看>>
python 加密算法及其相关模块的学习(hashlib,RSA,random,string,math)
查看>>
JavaSE总结
查看>>
手动造轮子——基于.NetCore的RPC框架DotNetCoreRpc
查看>>
Python IO编程
查看>>