Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2004-04-13 07:01:24


Thorsten Ottosen wrote:
> Hi Peter,
>
> [snp]
>> Yes, of course it is possible to reject the example. But I don't
>> want to reject it. It's useful.
>
> Ok. Could you present an example?

This feature of bind is used to create stateful function objects. In a
"real" lambda facility it corresponds to a local static variable:

    for_each( first, last, lambda(v) { static int x = 0; f(v, ++x); } );

The bind equivalent is

    for_each( first, last, bind( f, _1, bind(incr, 0) ) );

It calls f(first[0], 1), f(first[1], 2), and so on.

> Let me explain what bothers me. I simply fail to see why
> a call-back created with bind() should silently behave differently
> from what it forwards to. IMO that will create confusion.

Bind's emphasis is on usefulness, not hand-holding. Its learning curve is
inherently steep and more importantly, it simply cannot be flattened by
making bind less useful. To focus on how to make it less confusing would
mean to penalize advanced users while, at the same time, not helping
beginners in any significant way.


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