(转)boost::bind介绍
转自:http://www.cnblogs.com/sld666666/archive/2010/12/14/1905980.html
这篇文章介绍boost::bind()的用法, 文章的主要内容是参考boost的文档。
1. 目的
boost::bind 是std::bindlist 和 std::bind2nd的结合体。它提供一个任意的函数对象(仿函数)、函数、函数指针、成员函数指针。 它可以绑定任意的参数。bind 没有对函数对象有任何的要求。
2. 把bind()用在函数和函数指针上
有如下代码:
1 void f(int a, int b)
2 {
3 cout << a + b << endl;
4 }
5 void g(int a, int b, int c)
6 {
7 cout << a + b + c << endl;;
8 }
当调用boost::bind(f, 1, 2);的时候, 它会产生一个空的函数对象,这个对象没有参数, 返回 f(1,2).当然我们也可以给它加个参数:
1 int a = 10;
2 boost::bind(f, _1, 5)(a);
3 int x(10),y(20),z(30);
4 boost::bind(g,_1,_2,_3)(x, y, z);
结果:

作为和std::bindlst的对比我们可以看下如下的代码:
1 std::bind1st(std::ptr_fun(f), 5)(x); // f(5, x)
2 boost::bind(f, 5, _1)(x); // f(5, x)
是不是boost::bind()简单多了。
3. 把bind()用在函数对象(仿函数)上
bind()不仅能够用在函数上,而且可以接受任意的函数对象(仿函数)。如:
1 class F
2 {
3 public:
4 int operator()(int a, int b)
5 {
6 cout << a+b <<endl;
7 return a+b;
8 }
9 double operator()(double a, double b)
10 {
11 cout << a+b<< endl;
12 return a +b;
13 }
14 };
15 int _tmain(int argc, _TCHAR* argv[])
16 {
17 F f;
18 int a[] = {1, 2, 3, 4, 5, 6,7};
19 double aDouble[] = {1.1, 2.2, 3.3, 4.4,5.5,6.6,7.7};
20 for_each(a, a+7, boost::bind<int>(f, _1, _1));
21 for_each(aDouble, aDouble+7, boost::bind<double>(f, _1, _1));
22 return 0;
23 }
4. 把bind()用在成员变量和成员函数上
指向成员变量的指针和指向成员函数的指针和仿函数不一样, 因为他们没有提供operater()。boost用它的第一个参数接受类成员的指针,这样就像用boost::mem_fn()把类成员的指针转化为仿函数一样。如:
bind(&X::f, args)
就等于
bind<R>(mem_fn(&X::f), args)//R 是x::f的返回值。
列如:
1 struct X
2 {
3 bool f(int a)
4 {
5 cout << a <<endl;
6 return static_cast<bool>(a);
7 }
8 };
9 int _tmain(int argc, _TCHAR* argv[])
10 {
11 X x;
12 boost::shared_ptr<X> p(new X);
13 int i = 5;
14 boost::bind(&X::f, &x, _1)(i); // (&x)->f(i);
15 boost::bind(&X::f, x, _1)(i); //(copy x).f(i);
16 boost::bind(&X::f, p, _1)(i); //(copy p)->f(i);
17 return 0;
18 }
boost::bind()的基本用法就这些, 在使用的过程中发现确实比较爽, 但是这不知道这是不是常常被人批判的语法糖。
(转)boost::bind介绍的更多相关文章
- boost::bind 介绍
boost::bind 介绍 这篇文章介绍boost::bind()的用法, 文章的主要内容是参考boost的文档. 1. 目的 boost::bind 是std::bindlist 和 std: ...
- boost::function和boost::bind 介绍
一. boost::function介绍 原文:http://www.cnblogs.com/sld666666/archive/2010/12/16/1907591.html 本片文章主要介绍boo ...
- boost::bind 和 boost::function 基本用法
这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...
- 手把手教你实现boost::bind
前言 boost::bind操作想必大家都使用过,它特别神奇,能够绑定函数与参数,绑定后能够改变参数数量,并且还可以使用占位符.它可以绑定普通函数也可以绑定类成员函数.好多小伙伴试图看过boost:: ...
- boost::function 介绍
本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1. 介绍 Boost.Func ...
- boost asio 学习(二)了解boost::bind
2.了解boost::bind使用boost::bind封装一个函数,考虑以下例子示例2a #include <iostream> #include <boost/bind.hpp& ...
- boost::bind的简单实现
前言 在上一篇blog中简单的实现了boost::function,支持带有2个参数的函数/函数指针,函数对象,函数适配器/bind类,以及带有1个参数的成员函数指针. 本文接着来介绍如何实现一个简单 ...
- 1,Boost -> Bind
#include <boost/bind.hpp> #include <boost/shared_ptr.hpp> #include <iostream> usin ...
- boost::bind
bind并不是一个单独的类或函数,而是非常庞大的家族,依据绑定的参数个数和要绑定的调用对象类型,总共有十个不同的形式,但它们的名字都叫bind. bind接受的第一个参数必须是一个可调用对象f,包括函 ...
随机推荐
- 一些好用的nginx第三方模块
一些好用的nginx第三方模块 转自;http://macken.iteye.com/blog/1963301 1.Development Kit https://github.com/simpl/ ...
- NSTimer运行机制和线程问题
A.首先要理解NSTimer运行机制和Runloop之间的关系: 1.IOS的Run Loops机制 Run Loops是线程的基础部份,任何线程,包括主结程,都包含了一个run loop对象,Coc ...
- Qt 经典出错信息之”Basic XLib functionality test failed!”(Z..z..)
此完整出错信息是在./configure阶段 Basic XLib functionality test failed! You might need to modify the include an ...
- cocos2d-x Action
转自:http://codingnow.cn/cocos2d-x/775.html 从结构图可以看出,动作类的基类是CCAction,通过继承它可以实现很多种动作. CCFiniteTimeActio ...
- 授予普通域用户远程桌面连接DC/客户端权限
普通域用户通过远程桌面登录DC: 1)将该用户加入到 Remote Desktop Users 组中
- careercup-中等难题
17.1 编写一个函数,不用临时变量,直接交换两函数. 解法: 方法一:这个是经典面试题,也相当直接.我们将用a0表示a的初值,b0表示b的初始值,用diff表示a0-b0的值. 让我们将a>b ...
- Redis的安装、配置 --转载
原文地址:http://blog.sina.com.cn/s/blog_505bf9af0101ehhp.html redis的安装.配置 安装步骤如下:下载redis安装包:$ cd /opt/ap ...
- CSS——选择器
css选择器 css选择器可分为:标签(元素)选择器,ID选择器,类选择器,属性选择器,后代选择器,子代选择器,相邻兄弟选择器和兄弟选择器.... 标签选择器: //E{attr:value;attr ...
- GCC生成的汇编代码
假设我们写了一个C代码文件 code.c包含下面代码: int accum = 0; int sum(int x, int y){ int t = x + y; accum += t; return ...
- WebDriver: Getting it to play nicely with Xvfb
http://www.markhneedham.com/blog/2011/12/15/webdriver-getting-it-to-play-nicely-with-xvfb/ Thoughts ...