boost::bind四种应用场景的例子

 

  
普通函数

int f( int a, int b ){return a + b;}
boost::bind( f, _1, 9 )( 1 )

成员函数

struct demo{int f( int a, int b ){return a + b;}};
demo a, &ra=a;
demo *p = &a;
boost::bind( &demo::f, a, _1, 20 )( 10 )

成员变量

typedef std::pair<int, std::string> pair_t;
pair_t p( 123, "string" );
boost::bind( &pair_t::first, p )();
boost::bind( &pair_t::second, p )();

函数对象

struct sf{int operator()( int a, int b ){return a + b;}};
boost::bind<int>( sf(), _1, _2 )( 11, 22 )

ref库使用ref库包装对象的引用可以让bind 存储对象引用的拷贝,从而降低了拷贝的代价

变量:int g( int a, int b, int c ){return a + b + c;}
int x = 10;
boost::bind( g, _1, boost::cref( x ), boost::ref( x ) )( 11 );

函数对象:struct sf{int operator()( int a, int b ){return a + b;}};
sf af;
boost::bind<int>( boost::ref( af ), _1, _2 )( 11, 22 );

 

转载地址:http://blog.csdn.net/huang_xw/article/details/8452785

转载于:https://www.cnblogs.com/cthu/p/5158584.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值