Boost函数和boost绑定:绑定返回值?

时间:2011-10-31 21:51:08

标签: c++ boost boost-bind boost-function

这与之前的问题有关:Using boost::bind with boost::function: retrieve binded variable type

我可以绑定一个这样的函数:

在.h:

class MyClass
{
    void foo(int a);
    void bar();
    void execute(char* param);
    int _myint;
}

in .cpp

MyClass::bar()
{
    vector<boost::function<void(void)> myVector;
    myVector.push_back(boost::bind(&MyClass::foo, this, MyClass::_myint);
}
MyClass::execute(char* param)
{
    boost::function<void(void)> f  = myVector[0];
    _myint = atoi(param);
    f();
}

但是如何绑定返回值呢?即:

在.h:

class MyClass
{
    double foo(int a);
    void bar();
    void execute(char* param);
    int _myint;
    double _mydouble;
}

in .cpp

MyClass::bar()
{
    vector<boost::function<void(void)> myVector;
    //PROBLEM IS HERE: HOW DO I BIND "_mydouble"
    myVector.push_back(boost::bind<double>(&MyClass::foo, this, MyClass::_myint);
}
MyClass::execute(char* param)
{
    double returnval;
    boost::function<void(void)> f  = myVector[0];
    _myint = atoi(param);
    //THIS DOES NOT WORK: cannot convert 'void' to 'double'
    // returnval = f();
    //MAYBE THIS WOULD IF I COULD BIND...:
    // returnval = _mydouble;

}

2 个答案:

答案 0 :(得分:6)

如果你想要的是一个返回void的nullary函数,但在这之前为_myDouble赋值为foo(),那么你只能用Boost轻松完成.Bind。但是,Boost还有另一个专门针对这类事情的图书馆 - Boost.Phoenix

#include <iostream>
#include <vector>
#include <boost/function.hpp>
#include <boost/phoenix/phoenix.hpp>

struct MyClass
{
    MyClass() : _myVector(), _myInt(), _myDouble() { }
    void setMyInt(int i);
    void bar();
    void execute();

private:
    double foo(int const a) { return a * 2.; }

    std::vector<boost::function<void()> > _myVector;
    int _myInt;
    double _myDouble;
};

void MyClass::setMyInt(int const i)
{
    _myInt = i;
}

void MyClass::bar()
{
    using boost::phoenix::bind;

    _myVector.push_back(
        bind(&MyClass::_myDouble, this) =
            bind(&MyClass::foo, this, bind(&MyClass::_myInt, this))
    );
}

void MyClass::execute()
{
    if (_myVector.empty())
        return;

    _myVector.back()();
    double const returnval = _myDouble;
    std::cout << returnval << '\n';
}

int main()
{
    MyClass mc;
    mc.bar();

    mc.setMyInt(21);
    mc.execute();      // prints 42
    mc.setMyInt(3);
    mc.execute();      // prints 6  (using the same bound function!)
                       // i.e., bar has still only been called once and
                       // _myVector still contains only a single element;
                       // only mc._myInt was modified
}

答案 1 :(得分:1)

问题1:myVector需要成为班级成员。 问题2:myVector对返回双精度且不带参数的函数感兴趣,这些函数将是boost::function<double()>

然后,要将_mydouble绑定到foo的参数,请调用boost::bind(&MyClass::foo, this, MyClass::_mydouble),这会给你一个关于在调用foo时将double转换为int的编译警告。

最接近你可以使用Boost.Bind提供toreturn作为参数。

#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>

using namespace std;

class Foo {
        int myInt;
        double myDouble;
public:
        Foo() : myInt(3), myDouble(3.141592) { }
        void SetToMyInt(double& param)
        {
                param = myInt;
        }
        void SetToMyDouble(double& param)
        {
                param = myDouble;
        }
        double Execute()
        {
                double toReturn = 2;
                boost::function<void(double&)> f = boost::bind(&Foo::SetToMyDouble, this, _1);
                f(toReturn);
                return toReturn;
        }

};

int main() {
        Foo foo;
        std::cout << foo.Execute() << std::endl;
        return 0;
}