c++ - boost 函数和 boost 绑定(bind) : Bind the return value?

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

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

我可以像这样绑定(bind)一个函数:

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

但是我怎样才能绑定(bind)一个返回值呢?即:

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

}

最佳答案

如果你想要的是一个返回 void 但在执行之前用 foo() 的结果给 _myDouble 赋值的空函数所以,你不能仅使用 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
}

关于c++ - boost 函数和 boost 绑定(bind) : Bind the return value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7960174/

相关文章:

c++ - XML 节点模板——我应该使用 XSLT 吗?

c++ - clang-tidy:当容器包含特定类时发出警告

c++ - boost::bind 作为左值对象

c++ - 使用 Boost Variants 在 C++ 中表示 JSON 数据

c++ - boost::bind 是否有 QPointer 特化

c++ - 如何从循环中的 vector 调用 shared_ptr<boost::signal>?

c++ - 对于可以具有不同状态的结构,我应该使用哪种设计模式?

c++ - 与 stringstream 的链接错误

c++ - 使用 boost::shared_ptr<> 处理管理

c++ - boost::mpl::integral_c 之类的模板可以注册到 Boost.Typeof 吗?