Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2003-10-09 00:41:34


Brian McNamara <lorgon_at_[hidden]> wrote:
> On Wed, Oct 08, 2003 at 10:24:05PM -0400, David Abrahams wrote:

> Again, another reason I am un-fond of this is because I perceive it to
> be accident prone. The lambda library example from the DPCOOL paper is
> a good one. (These examples actually don't work, but for merely
> distracting reasons which I'll describe later[*].)
>
> // boost::lambda
> // print each element
> for_each( begin, end, cout << _1 << endl );
>
> // Now let's add leading text to each line
> for_each( begin, end, cout << "X is " << _1 << endl ); // whoops!

An alternative solution, which I prefer now, is to require that all arguments
(except literals) in a lambda expression be explicitly lazy:

    for_each( begin, end, cout_ << "X is " << _1 << endl_ );

Notice the trailing underscore.

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

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