Boost logo

Boost :

From: Jaakko Jarvi (jarvi_at_[hidden])
Date: 2006-04-13 11:04:25

On Apr 12, 2006, at 7:38 PM, Darren Cook wrote:

> Peter Dimov wrote:
>> 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(); }
>> 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 also really like this syntax, and the other example suggested by
> Peter:
> inline f( x, y ) { return x < y; }
> It seems natural and easy to understand. I think not having to specify
> parameter types when they can be worked out by the compiler is also
> important.
> But I'm not a compiler writer. Are there good reasons not to do it
> this way?

In current C++, there would be no good reasons to not do it this way.
In C++0x, assuming it supports constrained templates, separate type-
checking of such lambdas will be
more difficult, n1968 has a discussion about this topic.
By separate type-checking I mean that the body of the lambda function
is type-checked at the point of its definition, rather than
at the point of where the lambda is invoked.

We are looking into this issue, and are fairly confident that
_essentially_ separate type checking can still be achieved
even if parameter types are not specified. n1968 hints at how this is
accomplished. In any case, there will necessarily
be some separation of the point of definition and the point of type
checking because of auto:

auto f = <> (x, y) { return x + y; } // no information to type-check
the body

... a lot of code here ...

transform(a.begin(), a.end(), b.begin(), f) // we can get
information from the constraints of transform to type check the body
of f



Boost list run by bdawes at, gregod at, cpdaniel at, john at