Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2003-08-07 12:39:40


Fernando Cacciola wrote:
> That is, instead of directly calling:
>
> X x ;
> Y y ;
> x.f(1);
> y.g('a',1.2);
>
> I want to do something that would look like:
>
> X x;
> Y y;
> ActionList actions;
> actions.push_back( make_closure(&X::f, x,1) );
> actions.push_back( make_closure(&Y::g, y,'a',1.2) );
> std::for_each(actions.begin(),actions.end(),execute_action());

typedef std::list< boost::function<void()> > ActionList;
#define make_closure boost::bind

struct execute_action
{
    template<class F> void operator()(F & f) const
    {
        f();
    }
};

ActionList actions;
actions.push_back( make_closure(&X::f, &x, 1) );
actions.push_back( make_closure(&Y::g, &y, 'a', 1.2) );
std::for_each(actions.begin(),actions.end(),execute_action());

&x and &y since I'm assuming that you don't want x and y copied and stored
in the closure.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net