boost::function 与 boost::bind 用法详解

boost::function
是一个函数对象的“容器”,概念上像是C/C++中函数指针类型的泛化,是一种“智能函数指针”。它以对象的形式封装了原始的函数指针或函数对象,能够容纳任意符合函数签名的可调用对象。因此,它可以被用于回调机制,暂时保管函数或函数对象,在之后需要的时机在调用,使回调机制拥有更多的弹性。

既然boost::function经常被用于回调机制,我们就先看看什么是回调函数吧.

回调函数就是一个通过函数指针调用的函数。如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数。回调函数不是由该函数的实现方直接调用,而是在特定的事件或条件发生时由另外的一方调用的,用于对该事件或条件进行响应。
我们先来看看c/c++中函数指针在普通函数和类成员函数的使用吧。

#include <iostream>

using namespace std;
class CEventQuery;

typedef bool (*CommonFuncPoint)(void);                //普通函数指针
typedef bool (CEventQuery::*ClassFuncPoint)(int);   //类成员函数指针

bool CommonFunc(void)
{
    //这是个回调函数,当特定的事件或条件发生
    //的时候,会被调用。
    cout << "CallBackFunc Common Function Call!" << endl;
    //这里会有特定事件或条件发生时需要处理的事
    //....
    
    return true;
}

class CEventQuery
{
public:
    CEventQuery(CommonFuncPoint eventFunc)
        :m_Event(eventFunc){}
    ~CEventQuery(){}
    void Query(void)
    {
        //这里会检测某个事件或条件的发生        
        //比方说这里查询到了事件发生或条件的改变,那么就回调处理函数
        m_Event();
    }
    bool DoSomething(int iValue)
    {
        cout << "Class Function DoSomething Parament : " << iValue << endl;
        return true;
    }
private:
    CommonFuncPoint m_Event;
};

int main(void)
{
    CEventQuery tEventQuery(CommonFunc);
    tEventQuery.Query();
    ClassFuncPoint ClassFunc1 = &CEventQuery::DoSomething;
    (tEventQuery.*ClassFunc1)(10);
    tEventQuery.DoSomething(20);
    system("pause");
    return 0;
}

运行结果如下所示:
CallBackFunc Common Function Call!
Class Function DoSomething Parament : 10
Class Function DoSomething Parament : 20

不足的地方是,这种方式的调用只适用于函数指针,不适用于非函数指针(比如函数对象),这就是局限性。

与之前使用函数指针不同,function就像是一个函数的容器,也可以把function想象成一个泛化的函数指针,只要符合它声明中的函数类型,任何普通函数,成员函数,函数对象都可以存储在function对象中,然后在任何需要的时候被调用。
boost::function能够代替函数指针,并且能接受函数或函数对象,增加了程序的灵活性。当然,带来好处的同时,也必然有弊端。boost::function相比函数指针来说体积稍大一点,速度上稍慢一点。不过相比于boost::function带来的灵活性相比速度和体积就显得举足轻重了。

查看Boost官方文档,Boost.Function 有两种形式:首选形式和便携式形式, 其语法如下:
首选语法 :boost::function<float (int x, int y)> f;
便携式语法:boost::function2<float, int, int> f;

这两种形式等价,大家可以选择一种自己喜欢使用的形式即可(有一些较老的编译器不支持便携式)。
作为示范,我们的例子展示了:普通函数、类成员函数、函数对象的使用。当然function可以配合bind使用,存储bind表达式的结果,使bind可以被多次调用。目前没用到,下次再补充。

#include <iostream>                     
#include <boost/function.hpp>
using namespace std;
template<typename Func>
class CFuncDemo
{
public:
    CFuncDemo(int iValue):m_iValue(iValue){}
    template<typename CallBackFunc>
    void acceptFunc(CallBackFunc Func)
    {
        m_Func = Func;
    }
    
    void CommonResult()
    {
        m_Func(m_iValue);
    }
   template<typename T>
   void ClassMemberResult(T &t)
    {
        m_Func(t, m_iValue);
    }
    
   void FuncObjResult()
    {
        m_Func(m_iValue);
    }
private:
    Func m_Func;
    int m_iValue;
};

//普通函数
void CommonFunc(int iValue)
{
    cout << "CallBack Common Function" << endl;
    cout << "2 * iValue = " << 2 * iValue << endl;
}

//这个类有个成员函数完成类似的功能
class CMultiple
{
public:
    void ClassFunc(int iValue)
    {
        cout << "CallBack Class Member Function" << endl;
        cout << "3 * iValue = " << 3 * iValue << endl;
    }
};

//这里有个函数对象同样也完成对应的功能
class CFuncObj
{
public:
    void operator()(int iValue)
    {
        cout << "CallBack Function Object" << endl;
        cout << "4 * iValue = " << 4 * iValue << endl;
    }
};

int main(void)
{
    //作为示范的测试用例,这里我们展示了:普通函数、类成员函数、函数对象的使用
    //普通函数
    CFuncDemo<boost::function<void(int)>> tFuncCommon(10);
    tFuncCommon.acceptFunc(CommonFunc);
    tFuncCommon.CommonResult();
    
    //类成员函数
    CMultiple tMember;
    CFuncDemo<boost::function<void(CMultiple &, int)>> tFuncClassMember(10);
    tFuncClassMember.acceptFunc(&CMultiple::ClassFunc);
    tFuncClassMember.ClassMemberResult(tMember);

    //函数对象
    CFuncObj tObj;
    CFuncDemo<boost::function<void(int)>> tFuncObj(10);
    //tFuncObj.acceptFunc(tObj);

    //function使用拷贝语义保存参数,使用ref它允许以引用的方式传递参数
    tFuncObj.acceptFunc(boost::ref(tObj));
    tFuncObj.FuncObjResult();

     system("pause");
     return 0;
}

下面是测试结果:
CallBack Common Function
2 * iValue = 20
CallBack Class Member Function
3 * iValue = 30
CallBack Function Object
4 * iValue = 40

boost::bind
在这里插入图片描述在这里插入图片描述

一般情况下,bind 与 function 配合使用。
bind与function还可以将类型完成不同的函数(成员函数与非成员函数)包装成统一的函数调用接口。如下示例:
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值