Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Efficiency:</strong><br> This is basically functions versus functors which is what a lambda is under the hood. functors can actually be faster, this is because they are easier to inline, not all compilers will not inline function pointers (though it is possible). The reason for this is that when you pass a function pointer the compiler only knows the type of the function whereas the functor has the whole function because the type is unique. </p> <p><strong>Readability:</strong><br> This part is more or less opinion based. Function pointers IMO are pretty annoying, they have ugly types and are not as versatile as functors. For example functors can easily be passed around at runtime. On the other hand writing out a full function is more readable than a big lambda.</p> <p>The last point I would like to make is that a lambda like a functor can have a state (unlike a function, unless you count static vars), with a lambda you can capture a variable. Because of the compiler inlining limitations it is probably better not to pass around function pointers. </p> <p>So I would say for small functions passed to stdlib it's better to use lambdas, and for larger ones implement a functor, function pointers are really no longer idiomatic in c++11.</p> <p>Unfortunately it seems to me the way you are using them is not good. The compiler would have no problem inlining those as a normal function. And a function called <code>print_vector</code> or <code>make_unique</code> is better suited to it's own real function as it could be used in many other places. </p> <p><strong>Maintainability:</strong><br> In this case I would say lambda's have a low maintainability, as they cannot be reused outside the function and they look messy since they crowd the function they are in. So it would be better to define them outside.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload