Boost logo

Boost :

From: Jonathan Wakely (cow_at_[hidden])
Date: 2005-05-18 04:22:18


On Wed, May 18, 2005 at 12:05:53PM +0800, alex wrote:

> I test the sample in the section "Nesting STL algorithm invocations":
> --------------------------------------
>
> int a[100][200];
> int sum = 0;
>
> std::for_each(a, a + 100,
> bind(ll::for_each(), _1, _1 + 200, protect(sum += _1)));
> ------------------------------------------------If I remove the protect
> function, and I will get a complie error.Thx!

It prevents the inner expression from being evaluated too early.

There is a _little_ more information here:
http://boost.org/libs/bind/bind.html#nested_binds (*)

The _1 in "sum += _1" is not the same _1, so you protect it so it isn't
evaluated until processing the inner expression.

Without the protect it tries to add each array in the range [a,a+100) to
sum, but adding int[] to int is an error.

jon

(*) Bind maintainers, is the first "not" in this text an mistake?
    
    Sometimes it is necessary not to evaluate the first argument, but
    *not* to evaluate some of the other arguments, even when they are
    nested *bind* subexpressions.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk