What is the advantage of "lambda expressions"?
c++, lambda, syntax
Solution
It is more readable because it places the implementation of the function right into the spot where it is called, thus keeping the natural top-to-bottom flow of code unchanged.
You see, ordinary functions have their pros and cons. On the one hand, functions help reduce repetitions in the code, this making the code more structured and more readable. On the other hand, functions break the natural flow of code and transfer control to a completely different location. This can reduce the readability for rather obvious reasons: it is like reading a book that is riddled with nested forward and backward references.
So, in order to properly take advantage of the properties of ordinary functions, one should use them to implement well-thought-through, complete and isolated abstractions. That way ordinary functions will improve the readability of the code.
But for small "disposable" one-time-use utility code, ordinary functions don't work so well. They can actually makes the code significantly less readable. This is where lambda functions come in. They allow one to inject that disposable utility code straight into the point of the call, where it is necessary.
Problem
The reason for lambda expressions is implicitly generate function objects in a "more convenient way". As you can see from the example below, it is not only less convenient and longer, but also has a confusing syntax and notation. Are there any uses of this, where it actually makes code more readable? ``` cout << count(vec, [&](int a){ return a < x; }) << endl; // lambda cout << count(vec, Less_than<int> (x)) << endl; // normal functor def-n ```