本片文章主要介绍boost::function的用法。 boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象。

1.  介绍

Boost.Function 库包含了一个类族的函数对象的包装。它的概念很像广义上的回调函数。其有着和函数指针相同的特性但是又包含了一个调用的接口。一个函数指针能够在能以地方被调用或者作为一个回调函数。boost.function能够代替函数指针并提供更大的灵活性。

2. 使用

Boost.Function 有两种形式:首选形式和便携式形式, 其语法如下:

首选形式 便携式形式
boost::function<float(int x, int y)>f boost::function2<float, int, int>f

但是便携式形式不是所有的编译器都支持的, 所以这里我只介绍首选形式。

2.1 普通函数

我们可以看下如下的例子:


 1 void do_sum(int *values, int n)
2 {
3 int sum(0);
4 for (int i = 0; i < n; ++i)
5 {
6 sum += values[i];
7 }
8 cout << sum << endl;
9 };
10 int _tmain(int argc, _TCHAR* argv[])
11 {
12 boost::function<void(int *values, int n)> sum;
13 sum = &do_sum;
14 int a[] = {1,2,3,4,5};
15 sum(a, 5);
16 return 0;
17 }

sum 可以理解为一个广义的函数对象了,其只用就是保存函数do_sum, 然后再调用之。

2.2 成员函数

在很多系统中, 对于类的成员函数的回调需要做特殊处理的。这个特殊的处理就是“参数绑定”。当然这个超出了我们讨论的范围了。 boost::function对于成员函数的使用可以看下如下代码:


 1 class X{
2 public:
3 int foo(int a)
4 {
5 cout << a <<endl;
6 return a;
7 }
8 };
9 int _tmain(int argc, _TCHAR* argv[])
10 {
11 boost::function<int(X*, int)>f;
12 f = &X::foo;
13 X x;
14 f(&x, 5);
15 return 0;
16 }

我们发现, 对类的成员函数的对象化从语法是没有多大的区别。

3. 一个典型的例子

上面的几个例子没有体现出boost::function的作用来, 这里在写一个例子。比如当程序执行到某一处的时候想绑定某一个函数, 但是不想立即执行, 我们就可以声明一个函数对象,给此对象绑定相应的函数, 做一些其他事情,然后再来执行绑定的函数, 代码如下:


 1 void print(int a)
2 {
3 cout << a << endl;
4 }
5 typedef boost::function<void (int)> SuccessPrint;
6 int _tmain(int argc, _TCHAR* argv[])
7 {
8 vector<SuccessPrint> printList;
9 SuccessPrint printOne = boost::bind(print, _1);
10 printList.push_back(printOne);
11 SuccessPrint printTwo = boost::bind(print, _1);
12 printList.push_back(printTwo);
13 SuccessPrint printThree = boost::bind(print, _1);
14 printList.push_back(printTwo);
15 // do something else
16 for (int i = 0; i < printList.size(); ++i)
17 printList.at(i)(i);
18 return 0;
19 }

上述代码中首先把声明一个函数对象 typedef boost::function<void (int)> SuccessPrint, 然后把print绑定到斥对象中, 放入vector中, 到最后才来执行这print()函数

boost::function 介绍的更多相关文章

  1. boost::function和boost::bind 介绍

    一. boost::function介绍 原文:http://www.cnblogs.com/sld666666/archive/2010/12/16/1907591.html 本片文章主要介绍boo ...

  2. boost::function的用法

    本片文章主要介绍boost::function的用法. boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象. 1.  介绍 Boost.Func ...

  3. boost::bind 和 boost::function 基本用法

    这是一篇介绍bind和function用法的文章,起因是近来读陈硕的文章,提到用bind和function替代继承,于是就熟悉了下bind和function的用法,都是一些网上都有的知识,记录一下,期 ...

  4. boost function对象

    本文根据boost的教程整理. 主要介绍boost function对象的用法. boost function boost function是什么 boost function是一组类和模板组合,用于 ...

  5. Boost Asio介绍--之一

    原文:http://www.tuicool.com/articles/YbeYR3 Boost Asio介绍--之一 时间 2014-03-26 17:57:39  CSDN博客 原文  http:/ ...

  6. 以boost::function和boost:bind取代虚函数

    转自:http://blog.csdn.net/Solstice/archive/2008/10/13/3066268.aspx 这是一篇比较情绪化的blog,中心思想是“继承就像一条贼船,上去就下不 ...

  7. boost::function实践——来自《Beyond the C++ Standard Library ( An Introduction to Boost )》

    代码段1: #include <boost/function.hpp> #include <iostream> float mul_ints(int x, int y) { r ...

  8. 关于boost::function与boost::bind函数的使用心得

    最近开始写一个线程池,期间想用一个通用的函数模板来使得各个线程执行不同的任务,找到了Boost库中的function函数. Boost::function是一个函数包装器,也即一个函数模板,可以用来代 ...

  9. [转] boost::function用法详解

    http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost/fun ...

随机推荐

  1. JAVA-Enum 枚举

    [参考]枚举类名建议带上 Enum 后缀,枚举成员名称需要全大写,单词间用下划线隔开. 说明:枚举其实就是特殊的类,域成员均为常量,且构造方法被默认强制是私有. 正例:枚举名字为 ProcessSta ...

  2. 5、JPA-映射-单向多对一

    多个订单对应一个用户 实体类 Customer package com.jpa.yingshe; import javax.persistence.*; @Table(name = "JPA ...

  3. Linux记录-shell实现脚本监控服务器及web应用

    1.apache web 服务器 1 2 3 4 5 6 7 8 9 10 !/bin/bash # 表示请求链接3秒钟,不要返回的测试数据 nc -w 3 localhost 80 &> ...

  4. ACM-ICPC 2018 南京赛区网络预赛 J Sum (思维+打表)

    https://nanti.jisuanke.com/t/30999 题意 f(i)表示i能拆分成两个数的乘积,且要求这两个数中各自都没有出现超过1次的质因子的方案数.每次给出n,求∑(n,i=1)f ...

  5. ACM-ICPC 2018 南京赛区网络预赛 G Lpl and Energy-saving Lamps(模拟+线段树)

    https://nanti.jisuanke.com/t/30996 题意 每天增加m个灯泡,n个房间,能一次性换就换,模拟换灯泡过程.询问第几天的状态 分析 离线做,按题意模拟.比赛时线段树写挫了. ...

  6. bzoj千题计划311:bzoj5017: [Snoi2017]炸弹(线段树优化tarjan构图)

    https://www.lydsy.com/JudgeOnline/problem.php?id=5017 暴力: 对于每一个炸弹,枚举所有的炸弹,看它爆炸能不能引爆那个炸弹 如果能,由这个炸弹向引爆 ...

  7. python要点

    1.数据类型 字符串: ''或""表示单行,写三对符合表示多行, r'这种写法转义符不生效' 布尔值:True, False 空值: None 类型转换 print ), ), b ...

  8. elementUi模态框使用baiduMap错误记录

    报错如下,可能是因为目标div还没生成 elementUi文档说明 在dom生成后再调用,正常 buildMap(){ let map = new BMap.Map("allmap" ...

  9. 前端基础之JQuery - day15

    写在前面 上课第15天,打卡: 张国臂掖,以通西域: ########### # 课上简书 # ########## http://jquery.cuishifeng.cn/index.html JQ ...

  10. Webpack2学习记录-1

    1.安装前准备 安装 webpack 之前,需要安装 node,这时最新的是 6,npm 是 4.如果有老的 node 项目在跑建议安装下 nvm. 2.建议安装在局部,即在项目下的 node_mod ...