std::ptr_fun - 将函数指针转换成函数对象

在编译上一篇日志 ScopeGuard:编写Exception-Safe代码 中的ScopeGuard程序时发生了一个错误,代码加下:

 
  
void Decrement( int & x) { -- x; }

int test_ByRef( int & count) {
ScopeGuard guard
= MakeGuard(Decrement, ByRef(count));
}

 

代码很简单就是传一个函数指针和变量应用给MakeGuard,看似没任何问题,但编译就出错:

 
   
ScopeGuard.cpp: In instantiation of `ScopeGuardImpl1 < void ()( int & ), RefHolder < int > > ' :
ScopeGuard.cpp: 114 : instantiated from here
ScopeGuard.cpp:
47 : error: field `ScopeGuardImpl1 < void ()( int & ), RefHolder < int > > ::fun_ ' invalidly declared function type

 

出错信息指出成员变量fun_不是有效的函数类型,网上查了一下发现原因是Decrement只是一个函数指针(function pointer),并不能被用作type去定义一个变量,需要用std::ptr_fun将其转换成函数对象(function object),修改如下:

 
  
int test_ByRef( int & count) {
ScopeGuard guard
= MakeGuard(std::ptr_fun(Decrement), ByRef(count));
}

 

但std::ptr_fun有其局限,它只能处理一元函数(unary function)和二元函数(binary function),所以最佳选择还是使用boost::bind。

 

参考文章:

1. ptr_fun

2. Adaptable Binary Function

3. How to use bind1st and bind2nd ?

转载于:https://www.cnblogs.com/edwardlost/archive/2010/12/03/1895359.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值