Boost Users :
Subject: Re: [Boost-users] bind for_each and a range of functors
From: Peter Dimov (pdimov_at_[hidden])
Date: 2008-10-30 08:01:27
> On Thu, Oct 16, 2008 at 11:12 PM, Peter Dimov <pdimov_at_[hidden]> wrote:
>> With boost::bind, which doesn't evaluate its first argument, you'll need
>> use apply<>, as explained in
> 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
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