boost::bind四种应用场景的例子
2016-01-25 20:06
387 查看
普通函数 | 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 ); |
相关文章推荐
- 程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1
- OJ 之常规练习题(三)
- Kafka实战-KafkaOffsetMonitor
- 带有边框效果的图片裁剪
- 大学生 一带一路
- 移动端 Retina屏 各大主流网站1px的解决方案
- 接口测试-Swagger介绍
- 如何在网页上生成验证码?
- 如何在网页上生成验证码?
- NSAttributedString,并富文本,正则表达式,超链接,水印
- opencl 插值取样的一个代码
- 【完全版】线段树
- Android TV Input Framework(TIF)--3 显示Tv Input内容
- #梦断代码#first blood
- android app自动化测试之UIAutomator
- Java 多线程问题例子
- 为何ViewController释放后不走dealloc
- Ant
- 2015年终总结
- 极客DIY:廉价电视棒玩转GNSS-SDR,实现GPS实时定位