bind for_each and a range of functors
Let's say I have a std::list of a functor with a member function virtual operator()( int, int ) Is there an easy way to call each function in this list using for_each? Intuitively I'd expect to be able to do something like: void call_each( std::list<functor>& l ) { std::for_each( l.begin(), l.end(), boost::bind(_1,5,20) ); } Thanks -- Michael Marcin
AMDG Michael Marcin wrote:
void call_each( std::list<functor>& l ) { std::for_each( l.begin(), l.end(), boost::bind(_1,5,20) ); }
Boost.Lambda supports this. Boost.Bind doesn't. std::for_each( l.begin(), l.end(), boost::bind(&functor::operator(), _1,5,20) ); In Christ, Steven Watanabe
Michael Marcin:
Let's say I have a std::list of a functor with a member function
virtual operator()( int, int )
Is there an easy way to call each function in this list using for_each?
Intuitively I'd expect to be able to do something like:
void call_each( std::list<functor>& l ) { std::for_each( l.begin(), l.end(), boost::bind(_1,5,20) ); }
With boost::bind, which doesn't evaluate its first argument, you'll need to use apply<>, as explained in http://www.boost.org/doc/libs/1_36_0/libs/bind/bind.html#nested_binds
Peter Dimov wrote:
Michael Marcin:
Let's say I have a std::list of a functor with a member function
virtual operator()( int, int )
Is there an easy way to call each function in this list using for_each?
Intuitively I'd expect to be able to do something like:
void call_each( std::list<functor>& l ) { std::for_each( l.begin(), l.end(), boost::bind(_1,5,20) ); }
With boost::bind, which doesn't evaluate its first argument, you'll need to use apply<>, as explained in
http://www.boost.org/doc/libs/1_36_0/libs/bind/bind.html#nested_binds
Ah I knew it would be obvious. Missed that thanks! -- Michael Marcin
On Thu, Oct 16, 2008 at 11:12 PM, Peter Dimov
With boost::bind, which doesn't evaluate its first argument, you'll need to use apply<>, as explained in
http://www.boost.org/doc/libs/1_36_0/libs/bind/bind.html#nested_binds
I understand the "how" of that, but not the "why". Why does bind not evaluate if first argument? Thanks - Rob.
Robert Jones:
On Thu, Oct 16, 2008 at 11:12 PM, Peter Dimov
wrote: With boost::bind, which doesn't evaluate its first argument, you'll need to use apply<>, as explained in
http://www.boost.org/doc/libs/1_36_0/libs/bind/bind.html#nested_binds
I understand the "how" of that, but not the "why". Why does bind not evaluate if first argument?
With apologies for the late response: The first argument is not evaluated because this is what's needed most often. It's common to use bind as an implementation detail as in template<class F> void g1( F f ) { g2( boost::bind( f, _2, _1 ) ); } and, since the caller of g1 may well pass boost::bind(...) to it, evaluating f would mean more boost::protect's. The evaluation of the first argument is practically only needed for cases of the form bind( _1, _2 ). We could have made it so that placeholders are evaluated, but nested binds are not; this would cover nearly all use cases. The reason for not doing that in boost::bind is technical (and somewhat historical): the object returned by boost::bind always has a fixed result_type, and bind( _1, _2 ) cannot have one. There is no technical reason that prevents bind<R>( _1, _2 ) from being handled, though. I just missed this possibility, and nobody has suggested it (to the best of my knowledge). -- Peter Dimov http://www.pdplayer.com
participants (4)
-
Michael Marcin
-
Peter Dimov
-
Robert Jones
-
Steven Watanabe