当前位置: 首页 > news >正文

[原创](Modern C++)现代C++的std::bind花式绑定,使用方式大全.

[简介]
常用网名: 猪头三
出生日期: 1981.XX.XX
QQ联系: 643439947
个人网站: 80x86汇编小站 https://www.x86asm.org
编程生涯: 2001年~至今[共22年]
职业生涯: 20年
开发语言: C/C++、80x86ASM、PHP、Perl、Objective-C、Object Pascal、C#、Python
开发工具: Visual Studio、Delphi、XCode、Eclipse、C++ Builder
技能种类: 逆向 驱动 磁盘 文件
研发领域: Windows应用软件安全/Windows系统内核安全/Windows系统磁盘数据安全/macOS应用软件安全
项目经历: 磁盘性能优化/文件系统数据恢复/文件信息采集/敏感文件监测跟踪/网络安全检测

[序言]
std::bind是一个非常重要, 且非常实用的技术, 使用场景非常广泛. 简单通俗易懂解释就是: std::bind可以根据原有的函数对象, 映射出一个新的函数对象, 在映射的过程中, 新的函数对象的函数列表, 可以根据实际使用情况进行缩减.

[下面是几个常见的例子]

<1: 绑定普通函数>

void g_fun_BindedFunc(int int_param_A, int int_param_B)
{Application->MessageBox(System::Sysutils::IntToStr(int_param_A+int_param_B).c_str(), L"") ;return ;
}
auto bind_Func = std::bind(g_fun_BindedFunc,std::placeholders::_1,5) ; // 绑定
bind_Func(1,3) ; // 也可以是bind_Func(1). 因为这的3是所属第二个参数, 但该参数没有进行绑定, 所以形成无效参数.

<2: 绑定普通的模板函数>

template <class T>
void g_fun_BindedFunc(T int_param_A, T int_param_B)
{Application->MessageBox(System::Sysutils::IntToStr(int_param_A+int_param_B).c_str(), L"") ;return ;
}
auto bind_Func = std::bind(g_fun_BindedFunc<int>,std::placeholders::_1,5) ; // 绑定
bind_Func(1,3) ; // 也可以是bind_Func(1). 因为这的3是所属第二个参数, 但该参数没有进行绑定, 所以形成无效参数.

<3: 嵌套绑定>

void g_fun_BindedFunc(int int_param_A, int int_param_B)
{Application->MessageBox(System::Sysutils::IntToStr(int_param_A+int_param_B).c_str(), L"") ;return ;
}int g_fun_SubFunc(int int_param_A)
{return int_param_A ;
}auto bind_Func = std::bind(g_fun_BindedFunc,std::placeholders::_1,std::bind(g_fun_SubFunc, std::placeholders::_2)) ; // 嵌套绑定
bind_Func(1,2) ;

<4: 绑定类公有成员函数>

class BIND
{
public:int mpu_fun_SubFunc(int int_param_A){return int_param_A ;}int mpu_int_Data{10} ;};
BIND class_Bind ;
auto bind_Func = std::bind(&BIND::mpu_fun_SubFunc,&class_Bind,std::placeholders::_1) ; // 绑定类成员函数int int_Data = bind_Func(10) ;

<5: 绑定类共有成员变量>

class BIND
{
public:int mpu_fun_SubFunc(int int_param_A){return int_param_A ;}int mpu_int_Data{10} ;};
BIND class_Bind ;
auto bind_PublicMember = std::bind(&BIND::mpu_int_Data,std::placeholders::_1) ; // 绑定类成员变量int int_Data = bind_PublicMember(class_Bind) ;

<6: 绑定Lambda表达式>

auto test_Lambda = [](int int_param_A, int int_param_B) {return int_param_A + int_param_B ;} ;auto bind_Lambda = std::bind(test_Lambda,std::placeholders::_1,std::placeholders::_2) ; // 绑定Lambda表达式int int_Data = bind_Lambda(1, 2) ;
Application->MessageBox(System::Sysutils::IntToStr(int_Data).c_str(), L"") ;

[结尾]
std::bind目前以我的水平, 只能总结出6种情况的写法, 如果大家还有其他更高级的用法, 请留言给我, 我会及时更新.

http://www.lryc.cn/news/349498.html

相关文章:

  • Unity射击游戏开发教程:(13)如何在Unity中播放音效
  • Swift—手写防抖、手写图片预加载与多张图片拼接
  • Redis过期键删除策略
  • 413 Request Entity Too Large
  • 工业无风扇计算机的优点
  • 个人学习计划
  • 【电控实物-LK电机】
  • 《Mybatis》系列文章目录
  • ARM机密计算组件
  • Linux 生态与工具
  • RHCE网络服务配置之循环执行的例行性工作
  • vue3使用el-radio-group获取表格数据无法选中问题
  • java并发
  • Python Selenium Grid 环境快速搭建
  • 电机控制系列模块解析(21)—— 弱磁控制
  • 【3dmax笔记】022:文件合并、导入、导出
  • 8、QT——QLabel使用小记2
  • python实现读取串口数据-使用LibModbus库实现一个实时读取串口数据
  • 类与面向对象编程(Object-Oriented Programming, OOP)
  • 引用数据类型 栈内存 堆内存
  • 【class8】人工智能初步(图像识别-----卷积神经网络)
  • Node.js安装及环境配置(超详细!保姆级!!)
  • 利用反射如何动态生成sql
  • SpringBoot项目中使用Redis,Mybatis和JWT
  • CSS2(一):CSS选择器
  • LeetCode题练习与总结:不同的二叉搜索树--96
  • 第八十一章 将 Web 应用程序与远程 Web 服务器结合使用 - 如果从 Web 服务器提供静态文件
  • AVL树、红黑树
  • Vscode编辑器 js 输入log自动补全
  • structured concurrency