Boost程序库学习-bind与function

来源:互联网 发布:大英帝国 知乎 编辑:程序博客网 时间:2024/04/28 11:50

平时我们使用函数指针与函数对象的时候,需要定义:typedef void (*Func)(int,int);代码的可读性很差,boost::bind有如一个适配器,可以调用普通函数、成员函数、函数对象。而结合function可以作为一个回调函数。
所谓函数对象就是一个重载了operator()操作符的对象 。

1.bind

先见一个简单的例子:

#include <boost/assign.hpp>#include <boost/bind.hpp>#include <boost/foreach.hpp>#include <boost/function.hpp>#include <boost/ref.hpp>#include <boost/typeof/typeof.hpp>#include <boost/utility/result_of.hpp>#include <iostream>#include <math.h>#include <typeinfo>using namespace std;using namespace boost;using namespace boost::assign;int addTest(int a, int b){    return a + b;}void test1(){    //定义函数指针    typedef int (*Func1)(int x, int y);    //声明变量    Func1 func1 = addTest;    func1(3,5);    cout<<bind(addTest,_1,_2)(3,5)<<endl;}

第一种情况是使用函数指针来调用函数,第二种方式使用boost的bind,可以达到一样的效果。

原理:bind(addTest,_1,_2),_1,_2位占位符,这里的占位符可以不分先后,由后面的实参决定。bind()过程中,存储addTest,_1,_2的拷贝,得到一个临时的无参的函数对象,当operator()调用发生时,接受真正的参数,将_1,_2传递给addTest函数。
bind可以绑定普通函数,函数对象与成员函数:

1 普通函数

void print(int x){    cout << "test" << x << endl;}void bindTest1(){    //普通函数的绑定    typedef void (*Print1)(int);    Print1 t1 = print;    t1(1); //函数指针的调用    typedef boost::function<void(int)> Func;    // Func func1=t1;//将函数指针赋值于function    Func func1 = bind(print, _1);    func1(2);}

2 成员函数

struct point{    typedef int result_type;    int x, y;    point(int a = 0, int b = 0) : x(a), y(b) {}    void print(int z)    {        x = x + y + z;        cout << "(" << x << "," << y << ")" << endl;    }    int operator()(int z)    {        x = x + y + z;        cout << x << " " << y << endl;        return 0;    }    int add(int x, int y)    {        return x + y;    }};

该类中重载了operator(),所以这是一个函数对象。

void bindTest2(){        //成员函数的绑定    vector<point> v(10);    for_each(v.begin(), v.end(), bind(&point::print, _1, 1));}

其中for_each为boost中的标准算法 ,占位符_1,传入向量中的point对象 .bind成员函数不能直接调用operator(),因此必须传入对象地址,得到this指针才能在栈中调用成员函数,1为参数。

3 函数对象

void bindTest3(){    vector<point> v2(5);    for_each(v2.begin(), v2.end(), bind(point(), 1));}

2. function

是函数对象容器,以对象的形式封装原始函数指针活函数对象,可以暂时保管函数活函数对象,等到需要使用的时候再进行调用。与bind的结合,可以多次调用函数。
简单例子:

void functionTest(){    //function demo    function<int(int, int)> func;    func = f;    cout << func(10, 2) << endl;    //bind demo    cout << bind(f, _1, _2)(10, 2) << endl;    //成员函数调用    function<int(point &, int, int)> func1;    func1 = bind(&point::add, _1, _2, _3);    point p;    cout << func1(p, 10, 32) << endl;    function<int(int, int)> func2;    func2 = bind(&point::add, p, _1, _2);    cout << func2(38, 2) << endl;}

2) 适用于回调函数

class test_fun{  private:    int data1;    int data2;  public:    test_fun(int x, int y) : data1(x), data2(y) {}    typedef function<int(int, int)> Func;    Func fun;    template <typename CallBack>    void assignFun(CallBack f)    {        fun = f;    }    void doWork()    {        cout << "data is:" << fun(data1, data2) << endl;    }};int call_back_fun(int a, int b){    return a - b;}int call_back_fun1(int a,int b,int c){    return a+b-c;}void callbacktest(){    test_fun demoClass(23, 4);    demoClass.assignFun(call_back_fun);    demoClass.doWork();    //以绑定的方式调用函数    demoClass.assignFun(bind(call_back_fun1,_1,_2,4));    demoClass.doWork();}

采用function,一个模板可以接受多个函数,调用时机可灵活的处理。

0 0
原创粉丝点击