当前位置:   article > 正文

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

[原创](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: 绑定普通函数>

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

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

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

<3: 嵌套绑定>

  1. void g_fun_BindedFunc(int int_param_A, int int_param_B)
  2. {
  3.     Application->MessageBox(System::Sysutils::IntToStr(int_param_A+int_param_B).c_str(), L"") ;
  4.     return ;
  5. }
  6. int g_fun_SubFunc(int int_param_A)
  7. {
  8.     return int_param_A ;
  9. }
  10. auto bind_Func = std::bind(g_fun_BindedFunc,
  11.                            std::placeholders::_1,
  12.                            std::bind(g_fun_SubFunc, std::placeholders::_2)) ; // 嵌套绑定
  13. bind_Func(1,2) ;

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

  1. class BIND
  2. {
  3. public:
  4.     int mpu_fun_SubFunc(int int_param_A)
  5.     {
  6.         return int_param_A ;
  7.     }
  8.     int mpu_int_Data{10} ;
  9. };
  10. BIND class_Bind ;
  11. auto bind_Func = std::bind(&BIND::mpu_fun_SubFunc,
  12.                            &class_Bind,
  13.                            std::placeholders::_1) ; // 绑定类成员函数
  14. int int_Data = bind_Func(10) ;

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

  1. class BIND
  2. {
  3. public:
  4.     int mpu_fun_SubFunc(int int_param_A)
  5.     {
  6.         return int_param_A ;
  7.     }
  8.     int mpu_int_Data{10} ;
  9. };
  10. BIND class_Bind ;
  11. auto bind_PublicMember = std::bind(&BIND::mpu_int_Data,
  12.                                    std::placeholders::_1) ; // 绑定类成员变量
  13. int int_Data = bind_PublicMember(class_Bind) ;

<6: 绑定Lambda表达式>

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

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

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
  

闽ICP备14008679号