ptr_fun

本文详细介绍了如何利用C++中的ptr_fun将普通函数转换为仿函数,进而适用于bind1st和bind2nd。通过示例代码演示了ptr_fun的应用,使得在不同场景下可以灵活调用普通函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

头文件:<functional>

ptr_fun是将一个普通的函数适配成一个仿函数(functor), 添加上argument_type和result type等类型,它的定义如下:

  1. template<class _Arg1,  
  2.     class _Arg2,  
  3.     class _Result> inline  
  4.     pointer_to_binary_function<_Arg1, _Arg2, _Result,  
  5.         _Result(__clrcall *)(_Arg1, _Arg2)>  
  6.             ptr_fun(_Result (__clrcall *_Left)(_Arg1, _Arg2))  
  7.     {    // return pointer_to_binary_function functor adapter  
  8.     return (pointer_to_binary_function<_Arg1, _Arg2, _Result,  
  9.         _Result (__clrcall *)(_Arg1, _Arg2)>(_Left));  
  10.     }  
下面的例子就是说明了使用ptr_fun将普通函数(两个参数, 如果有多个参数, 要改用boost::bind)适配成bind1st或bind2nd能够使用的functor,否则对bind1st或bind2nd直接绑定普通函数,则编译出错。
  1. #include <algorithm>    
  2. #include <functional>    
  3. #include <iostream>    
  4.   
  5. using namespace std;    
  6.   
  7. int sum(int arg1, int arg2)    
  8. {    
  9.     std::cout<< "arg1 = " << arg1 << std::endl;    
  10.     std::cout<< "arg2 = " << arg2 << std::endl;    
  11.   
  12.     int sum = arg1 + arg2;    
  13.     std::cout << "sum = " << sum << std::endl;    
  14.   
  15.     return sum;    
  16. }  
  17.   
  18. int main(int argc, char *argv[], char *env[])  
  19. {    
  20.     bind1st(ptr_fun(sum), 1)(2);        // the same as sum(1,2)    
  21.     bind2nd(ptr_fun(sum), 1)(2);        // the same as sum(2,1)    
  22.   
  23.     return 0;  
  24. }  
undefined1 cef_browser_host_create_browser (longlong param_1,undefined8 param_2,longlong param_3,longlong param_4,undefined8 param_5, undefined8 param_6) { undefined1 uVar1; double in_XMM0_Qa; undefined1 auStack_248 [32]; undefined8 local_228; undefined8 local_220; undefined8 local_210; undefined ***local_208; longlong local_200; undefined1 local_1f8; undefined8 local_1f0; undefined8 local_1e8; undefined **local_1e0; undefined8 local_1d8; undefined1 local_1d0 [272]; longlong local_c0; undefined **local_b8; undefined8 local_b0; undefined8 uStack_a8; undefined8 local_a0; undefined8 uStack_98; undefined8 local_90; undefined8 uStack_88; undefined8 local_80; undefined8 uStack_78; undefined8 local_70; undefined8 uStack_68; undefined8 local_60; undefined8 uStack_58; longlong local_50; ulonglong local_48; /* 0x81d0 13219 cef_browser_host_create_browser */ local_48 = DAT_189b06ea8 ^ (ulonglong)auStack_248; v8::internal::ConvertDouble<double>(in_XMM0_Qa); uVar1 = 0; if ((param_1 != 0) && (param_4 != 0)) { local_60 = 0; uStack_58 = 0; local_70 = 0; uStack_68 = 0; local_80 = 0; uStack_78 = 0; local_90 = 0; uStack_88 = 0; local_a0 = 0; uStack_98 = 0; local_b0 = 0; uStack_a8 = 0; local_50 = 0; local_b8 = &PTR_FUN_18849f438; FUN_1800083a2(&local_b8,param_1,0); local_1e0 = &PTR_FUN_18849f448; memset(local_1d0,0,0x118); local_1d8 = 0x118; FUN_18000c900(param_4,&local_1d8,0); FUN_180008408(&local_1e8,param_6); FUN_180008480(&local_1f0,param_5); local_208 = &PTR_FUN_18849f050; local_200 = 0; local_1f8 = 0; if (param_3 != 0) { FUN_1800039c0(&local_208); local_200 = param_3; } local_1f8 = 0; FUN_1800084f8(&local_210,param_2); local_220 = local_1e8; local_228 = local_1f0; uVar1 = FUN_182edb8c8(&local_b8,local_210,&local_208,&local_1e0); local_208 = &PTR_FUN_18849f050; FUN_1800039c0(&local_208); local_1e0 = &PTR_FUN_18849f448; if (local_c0 == 0) { FUN_18000c89e(&local_1d8); } local_b8 = &PTR_FUN_18849f438; if (local_50 == 0) { cef_string_utf16_clear(&uStack_a8); } } FUN_18848a3e0(local_48 ^ (ulonglong)auStack_248); return uVar1; }
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值