本片文章主要介绍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. centos 7.4 安装gitlab

    centos 7.4 安装gitlab #curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/scrip ...

  2. Flink HA 搭建坑

    目前网上能找到的做HA的教程基本都无法真正做到多机高可用,包括官方文档,经过很久的折腾,终于做到了多机高可用,希望其它人不再被坑. 集群模式安装 前提条件: 机器已经安装好Java环境 jobMana ...

  3. weblogic优化(内存、线程数和启动速度)

    一.为服务分配内存 1.一般如果服务所需的内存不一样,需要单独指定的话,我们是通过拷贝startWebLogic.sh文件,拷贝为startNode.sh(名字随意) 2.然后在startNode.s ...

  4. java io系列26之 RandomAccessFile

    本文主要介绍 RandomAccessFile. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_26.html 更多内容请参考:java io系列0 ...

  5. MyBatis-注解方式整合SSM

    Spring.Spring MVC.MyBatis 整合 一.依赖 <?xml version="1.0" encoding="UTF-8"?> & ...

  6. HDU - 6304(2018 Multi-University Training Contest 1) Chiaki Sequence Revisited(数学+思维)

    http://acm.hdu.edu.cn/showproblem.php?pid=6304 题意 给出一个数列的定义,a[1]=a[2]=1,a[n]=a[n-a[n-1]]+a[n-1-a[n-2 ...

  7. 百度编辑器 ueditor 会屏蔽过滤 body html head DOCTYPE ... 的解决办法

    百度编辑器很强,但有时候复制到html里时,会带有 body  html head 等标签,切到视图时,内容都不见了 是因为白名单 解决办法: 我测的是1.4.3版本 在 ueditor.config ...

  8. 'DataVisualization' does not exist in the namespace 'System.Web.UI'一例解决办法

    之前项目是vs2010 aspx项目,用vs2017打开后,非运行状态下有一行错误:CS0234 C# The type or namespace name 'DataVisualization' d ...

  9. spring注解第02课 包扫描@ComponentScan、@ComponentScans

    1.配置文件形式: <context:component-scan base-package="com.atguigu" /> spring会扫描此包下的@Servic ...

  10. 使用select正确处理EOF的str_cli函数

    void str_cli(FILE *fp, int sockfd) { int maxfdp1, stdineof; fd_set rset; char buf[MAXLINE]; int n; s ...