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