boost::function的用法

时间:2023-03-08 17:59:20
boost::function的用法

本片文章主要介绍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 普通函数

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

  void do_sum(int *values, int n)
   {
       );
       ; i < n; ++i)
       {
           sum += values[i];
       }
       cout << sum << endl;
   };
  int _tmain(int argc, _TCHAR* argv[])
  {
      boost::function<void(int *values, int n)> sum;
      sum = &do_sum;
      ,,,,};
      sum(a, );
      ;
  }

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

2.2 成员函数

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

 class X{
   public:
       int foo(int a)
       {
           cout << a <<endl;
           return a;
       }
   };
   int _tmain(int argc, _TCHAR* argv[])
  {
      boost::function<int(X*, int)>f;
      f = &X::foo;
      X x;
      f(&x, );
      ;
  }

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

3. 一个典型的例子

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

 void print(int a)
   {
       cout << a << endl;
   }
   typedef boost::function<void (int)> SuccessPrint;
   int _tmain(int argc, _TCHAR* argv[])
   {
       vector<SuccessPrint> printList;
       SuccessPrint printOne = boost::bind(print, _1);
       printList.push_back(printOne);
      SuccessPrint printTwo = boost::bind(print, _1);
      printList.push_back(printTwo);
      SuccessPrint printThree = boost::bind(print, _1);
      printList.push_back(printTwo);
      // do something else
      ; i < printList.size(); ++i)
          printList.at(i)(i);
      ;
  }

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