2

这与上一个问题有关: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;
}

在.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();
}

但是我怎样才能绑定一个返回值呢?IE:

在.h中:

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

在.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;

}
4

2 回答 2

6

如果您想要的是一个返回void值但在执行此操作之前为其分配值_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
}
于 2011-10-31T22:12:49.410 回答
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;
}
于 2011-10-31T21:55:46.253 回答