[Development] RFC: lambda or lambda return from lambda?

Kevin Kofler kevin.kofler at chello.at
Mon Feb 1 14:52:19 CET 2016


Marc Mutz wrote:
> But I find the inlined lambda worse than an explicit loop. This is
> write-only code, imo. Esp. since we can't (yet) use auto in the parameter
> list, but even then, I'd always give a lambda a name (cf. my mail in
> response to Christian).

But writing it inline and without a name is the whole point of a lambda. If 
you're giving it a name anyway, you may as well declare a named functor 
class.

> Named functions have two problems: a) that many compilers don't inline the
> code. So at a minimum, you'd write a forwarding lambda, or the function
> would be an auto variable holding a stateless lambda (the difference
> between the two is almost non-existent, anyway). And b) that they cannot
> carry state. Lambdas can.

Named functor classes can, too.

And I actually like Sergio Martins's solution best: Write a 
removeIfFirstEquals(fields, name) wrapper that hides the lambda entirely. 
There, you can also inline the lambda because the name of the containing 
function (removeIfFirstEquals) makes it very clear what the lambda does. And 
the calling code does not have to care how the "if first equals" test is 
actually implemented.

        Kevin Kofler




More information about the Development mailing list