Peter Dimov
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
> 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());
Beautyful! Just copy-pasted and it worked just as you wrote it.
&x and &y since I'm assuming that you don't want x and y copied and stored in the closure.
Yes... Thanks Fernando Cacciola