2

This is related to this previous question: Using boost::bind with boost::function: retrieve binded variable type.

I can bind a function like this:

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

But how can I bind a return value ? i.e.:

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

}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Smash
  • 3,722
  • 4
  • 34
  • 54
  • 1
    You have a `function` -- where do you expect a `double` to come into this at all? If you want a nullary `function<>` that returns a `double`, try `function`... – ildjarn Oct 31 '11 at 21:53
  • look at the last line of the `bar` function, this is how the input argument is bound, the function type is erased and becomes `function>` but `_myint` is still linked.. I want the same for the return value. The point is to be able to store all the boost::functions in the same vector ;) – Smash Oct 31 '11 at 21:56
  • But you've **explicitly** specified that you want `void` as the return value (`function`); either you want `void` or you want `double`, you can't have both. – ildjarn Oct 31 '11 at 21:58
  • I agree the `returnval = f();` shouldn't work, but there should be a way to bind the return value just as there is a way to bind the input value... – Smash Oct 31 '11 at 21:59
  • No, Boost.Bind binds functions and *parameters*. The execution of the function and the consequential return of a value is exactly the same as if you called a regular function. – totowtwo Oct 31 '11 at 22:04

2 Answers2

6

If what you want is a nullary function that returns void but assigns a value to _myDouble with the result of foo() before doing so, then you cannot do this easily with just Boost.Bind. However, Boost has another library specifically catered to this sort of thing -- 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
}
ildjarn
  • 62,044
  • 9
  • 127
  • 211
1

problem 1: myVector needs to be a class member. problem 2: myVector is interested in functions that return doubles and take no arguments, which would be boost::function<double()>

then, to bind _mydouble to the parameter of foo, call boost::bind(&MyClass::foo, this, MyClass::_mydouble) which should give you a compilation warning about casting a double to an int for when foo is called.

The closest you can come with Boost.Bind is providing the toreturn as a parameter.

#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;
}
totowtwo
  • 2,101
  • 1
  • 14
  • 21
  • 2
    `boost::function` Please don't propagate this hideous C-ism, prefer `boost::function`. – ildjarn Oct 31 '11 at 21:56
  • 1: myVector obviously is a class member :P and see comments for why it is interested in `` functions – Smash Oct 31 '11 at 22:03
  • 3
    No, its not obvious. Boil your code down to something that exhibits your problem with a tool like ideone.com and THEN post your code. – totowtwo Oct 31 '11 at 22:06