Boost logo

Boost :

From: Jeff Flinn (TriumphSprint2000_at_[hidden])
Date: 2006-04-12 16:44:21


Peter Dimov wrote:
> Valentin Samko wrote:
>
>> We are currently working on resolving issues raised during the
>> committee meeting and hopefully will write a new paper on lambdas.
>> Any feedback on this topic will be highly appreciated.
>
> I was looking at the lambda papers recently and I came to the
> conclusion
> that what we actually need is local functions. To pick an example from

and function objects?

> N1958:
>
> void foo( myvec& v, const myset& s, int a )
> {
> // ...
> v.erase(
> std::remove_if(v.begin(), v.end(),
> bool(int x) {
> return std::abs(x) < a
> && s.find(x) != s.end(); }),
> v.end()
> );
> }
>
> What I really want is this:
>
> void foo( myvec& v, const myset& s, int a )
> {
> // ...
>
> inline bool f( int x ) { return std::abs( x ) < a && s.find( x ) !=
> s.end(); }

This goes beyond simple local functions, with the ability to access s and a
from the enclosing function scope. I like it. :) Saves the need of a full
function object with explicit reference members to s and a.

> v.erase( std::remove_if( v.begin(), v.end(), f ), v.end() );
> }
>
> for obvious readability reasons. This syntax also allows me to use a
> more descriptive name instead of f, and the consistency with ordinary
> function definitions will make it easier to teach. It may be somewhat
> easier to parse or specify, but I haven't considered this in detail.

I've never understood the reason that local function objects were not able
to be used with templates. IMO, algorithms would have been much more readily
adopted.

Jeff


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