In C++11, what is a lambda expression?
A: It's the object of an autogenerated class with overloading operator() const under the hood.
Closure is a type of object that is produced by the compiler.
This 'closure' idea is similar to C++11's bind notion.
Lambdas, on the other hand, usually produce better code.
Full inlining is also possible with calls through closures.
Q: When do you think I'd utilise one?
A: Define "simple and tiny logic" and request that the compiler generate the code from the preceding question.
You tell the compiler the expressions you wish to be inside the operator ().
The compiler will produce everything else for you.
Q: What kind of problem do they tackle that couldn't be solved before they were introduced?
A: It's some form of syntactic sugar, like using operators instead of functions for custom add, subtract, and other operations...
However, wrapping 1-3 lines of genuine logic to some classes, and so on, saves additional lines of needless code!
Some engineers believe that if the number of lines is reduced, there is a lower likelihood of mistakes (which I agree with).
Example of usage
auto x = [=](int arg1){printf("%i", ...READ MORE
answered
Jun 15, 2022
in C++
by
Damon
• 4,960 points
•
458 views