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

Jędrzej Nowacki jedrzej.nowacki at theqtcompany.com
Tue Feb 2 09:24:22 CET 2016


On Monday 01 of February 2016 15:16:33 you wrote:
> I am a great fanboy of algorithms and the STL, as should be clear by now
You are excused ;-) But seriously I do not think there is anything wrong about 
it, STL is a bit obscure, but fast and sometimes a code needs to be fast. 

> 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).
Hmmm, In my opinion you do not like the most cool feature of lambdas :-) Each 
time I was forced to define a functor somewhere, in a completely different 
place then my code lived, I was sad and jealous of python lambda syntax. I 
value a code that can be read from top to down without major jumps. Naming 
lambdas causes my eyes to jump ("so for each element it calls a function 
FooBar... Ah which does that") and it also suggests that the lambda will be 
re-used ("ok, so now remember FooBar, as it will be used again..."). I would 
much prefer a simple inline comment before sequence of std::remove_if 
std::erase and others then named lambda.

Anyway I guess we are again hitting a "C++11 syntax that we were not used to" 
issue. So please do not create any policy about that, at least not for now. 

Btw. when you are taking an address of a function you force a compiler to de-
inline the function, I hope such de-optimization doesn't happen while naming 
lambdas.

> > For a bigger code we would actually require named functions. What do you 
> > think?
> 
> 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.
Sorry I used wrong wording, by named function I meant, assigned lambda, a 
functor or a declared function.  In general a callable with a name, defined in 
a different place then used.

A propos inline'ing of functions, have you tried anonymous namespaces? That 
should help.  At least once I forced gcc to inline the code...

Cheers,
 Jędrek



More information about the Development mailing list