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

Christian Kandeler christian.kandeler at theqtcompany.com
Mon Feb 1 10:23:34 CET 2016


On 02/01/2016 11:08 AM, Marc Mutz wrote:
> We're seeing increasing use of lambdas in dev, and I'm a bit unhappy about the
> result.
>
> E.g. (not picking on Anton here, I have done the same before):
>
>   auto firstEqualsName = [&name](const QPair<QByteArray, QByteArray> &header)
>   {
>        return qstricmp(name.constData(), header.first) == 0;
>   };
>   fields.erase(std::remove_if(fields.begin(), fields.end(),
>                               firstEqualsName),
>                fields.end());
>
> This is one way to write a unary predicate. But it hides the fact that the
> predicate depends on the parameter 'name' (an argument to the function this
> lambda is defined in).
>
> With classical function objects, one would have written - at the call site:
>
>   fields.erase(std::remove_if(fields.begin(), fields.end(),
>                               FirstEquals(name)),
>                fields.end());
>
> See the difference?

Yes, but it is offset by another difference: As opposed to the function 
object, the lambda is defined right above the call site (or at least 
very close to it), so you can easily see that it captures an additional 
variable.
I therefore think that this is not a problem.


Christian



More information about the Development mailing list