赞
踩
[简介]
常用网名: 猪头三
出生日期: 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种情况的写法, 如果大家还有其他更高级的用法, 请留言给我, 我会及时更新.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。