boost::function

来源:互联网 发布:淘宝购物付款流程图 编辑:程序博客网 时间:2024/05/01 14:49

一、介绍

boost::function 就是一个函数的包装器(function wrapper),用来定义函数对象。它的概念很像广义上的回调函数。它以对象的形式封装了原始的函数指针或函数对象,能够容纳任意符合函数签名的可调用对象。因此,它可以被用于回调机制,暂时保管函数或函数对象,在之后需要的时机在调用,使回调机制拥有更多弹性。回调函数的一种形式就是:C语言中的typedef 返回类型 (*pointer)(参数列表),如:

typedef boost::function<void(bool, int, int)> RequestCallback;

二、使用

头文件:

 #include<boost/function.hpp> using namespace boost;

示例:

int fsum(int i, int j){    return i + j;}class Person{public:    void operator() (std::string name, int age)    {        std::cout << name << ": " << age << std::endl;    }};class Car{public:    Car(){}    virtual ~Car(){}    void info(int i)    {        std::cout << "info = " << i << std::endl;    }};void test_function(){    // 1. 普通函数    boost::function<int(int, int)> func1;    func1 = fsum;    std::cout << "4 + 5 = " << func1(4, 5) << std::endl;    // 2. 函数对象    boost::function<void(std::string, int)> func2;    Person person;    func2 = person;    func2("myname", 30);    // 3. 成员函数    boost::function<void(Car*, int)> func3;    func3 = &Car::info;    Car car;    func3(&car, 25);    // 4. 空函数    boost::function<int(int, int)> func4;    assert(func4.empty());    assert(!func1.empty());    func1.clear();    assert(func1.empty());    try    {        func1(4, 5);    }    catch (std::exception& e)    {        std::cout << e.what() << std::endl;    }}

三、高级用法

function可以配合bind使用,存储bind表达式的结果,使bind可以被多次调用,bind函数返回的是一个函数对象。
示例1:

#include "boost/bind.hpp" namespace {   void overload(int param1, float param2, int param3) {}   void overload(int param) {}   void overload(float param) {}   class Class   {   public: // interface     void overload(float param) {}     void overload(int param) {}   }; } // namespace int main(int arg, char** argv) {   // non-member   boost::bind(::overload, _1, _2, _3); // can bind normally   typedef void (*NonMemberFuncType)(int);   boost::bind(static_cast<NonMemberFuncType>(::overload), _1);   // member   Class* objPtr = new Class();   typedef void (Class::*OverloadFuncType)(float);   boost::bind(static_cast<OverloadFuncType>(&Class::overload), objPtr, _1); } // main 

示例2:

#include "boost/function.hpp" #include "boost/bind.hpp" #include <string> #include <iostream> namespace {   void function(int number, float floatation, std::string string)   {     std::cout << "Int: \"" << number << "\" Float: \"" << floatation               << "\" String: \"" << string << "\"" << std::endl;   } } // namespace int main(int c, char** argv) {   // declare function pointer variables   boost::function<void(std::string, float, int)> shuffleFunction;   boost::function<void(void)> voidFunction;   boost::function<void(float)> reducedFunction;   // bind the methods   shuffleFunction = boost::bind(::function, _3, _2, _1);   voidFunction = boost::bind(::function, 5, 5.f, "five");   reducedFunction = boost::bind(::function, 13, _1, "empty");   // call the bound functions   shuffleFunction("String", 0.f, 0);   voidFunction();   reducedFunction(13.f); } // main 

参考:
http://blog.csdn.net/fengbangyue/article/details/7185340
http://blog.csdn.net/huang_xw/article/details/8249278
http://www.radmangames.com/programming/how-to-use-boost-bind

0 0