How to create a function with argument that would be result of boost::bind?
Asked Answered
R

2

5

So I want to create a function like:

void proxy_do_stuff(boost::bind return_here)
{
  return_here(); // call stuff pased into boost::bind
}

And I could call it like :

proxy_do_stuff(boost::bind(&myclass::myfunction, this, my_function_argument_value, etc_fun_argument));

How to do such thing?

Recap answered 31/8, 2011 at 5:37 Comment(0)
I
3
#include <boost/bind.hpp>

template<typename T>
void proxy_do_stuff(T return_here)
{
    return_here(); // call stuff pased into boost::bind
}

struct myclass
{
    void myfunction(int, int)
    {
    }
    void foo()
    {
        int my_function_argument_value = 3;
        int etc_fun_argument= 5;
        proxy_do_stuff(boost::bind(&myclass::myfunction, this, my_function_argument_value, etc_fun_argument));
    }
};

int main()
{
    myclass c;
    c.foo();
    return 0;
}
Innermost answered 31/8, 2011 at 5:53 Comment(2)
And how to use such function? Should not we call it like proxy_do_stuff<bla-bla-bla>(...)?Recap
I added the how to use it part. No need for bla-bla-bla.Innermost
L
4

The return type of boost::bind is of type boost::function. See below:

void proxy_do_stuff(boost::function<void()> return_here)
{
    return_here(); // call stuff pased into boost::bind
}
Lakendra answered 31/8, 2011 at 5:56 Comment(1)
+1 The return type is actually boost::_bi::bind_t<int, boost::_mfi::cmf2<int, myclass, int, int>, boost::_bi::list3<boost::_bi::value<myclass*>, boost::_bi::value<int>, boost::_bi::value<int> > > but you're converting it to a boost::function<void()>Innermost
I
3
#include <boost/bind.hpp>

template<typename T>
void proxy_do_stuff(T return_here)
{
    return_here(); // call stuff pased into boost::bind
}

struct myclass
{
    void myfunction(int, int)
    {
    }
    void foo()
    {
        int my_function_argument_value = 3;
        int etc_fun_argument= 5;
        proxy_do_stuff(boost::bind(&myclass::myfunction, this, my_function_argument_value, etc_fun_argument));
    }
};

int main()
{
    myclass c;
    c.foo();
    return 0;
}
Innermost answered 31/8, 2011 at 5:53 Comment(2)
And how to use such function? Should not we call it like proxy_do_stuff<bla-bla-bla>(...)?Recap
I added the how to use it part. No need for bla-bla-bla.Innermost

© 2022 - 2024 — McMap. All rights reserved.