Boost logo

Boost :

From: Valentin Samko (boost_at_[hidden])
Date: 2006-04-12 18:43:38


>>> inline bool f( int x ) { return std::abs( x ) < a && s.find( x )
PD> The 'f' above is not really a function, it's a function object, a named
PD> lambda. The only difference with Valentin Samko's lambda proposal:
PD> http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2006/n1958.pdf
PD> is in the syntax. I'd prefer to not be forced to spell the type of the
PD> arguments:
PD> inline bool less( x, y ) { return x < y; }

This is what I mentioned in Annex A/2. I just did not want to
overcomplicate the proposal with template lambdas as this
functionality can be added later.

With the "auto" keyword one would be able to write

auto f = <> less(x, y) { return x < y; };

where "f" is a function object, somewhat equivalent to a local
function. This is from a mixture of n1958 and n1968 which we are
working on.

PD> It doesn't beat Dave's _1 < _2 challenge, but I'm not sure that it has to.

The problem with _1 < _2 is that you still need to put it inside
{...} to limit the scope of the lambda, and some prefix like <> to
avoid the forward lookup which must be done by the compiler. And once
you add that, the only difference is that with _1 and _2 you do not
have to declare the arguments and need to add the explicit "return"
keyword.

A bigger problem is how to access local variables in the lambda. There
are reasonable objections to all the default options and we may end up
disabling access to the local scope variables by default. Also, if I
understood Herb Sutter right, he wants to see all the static and namespace
scope variables (referenced in the lambda function) to be copied to
that lambda object by default by value. This would be more of a
problem.

Valentin Samko
http://val.samko.info


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