Inline lambdas

Summary: Use lambda definitions directly in parameters as much as possible:

Explanation

Lambda functions in C++ are also known as anonymous functions, as they do not have a name. You can give them a name and use the name when you call it:

However, this does not take advantage of the lambda features, and is equivalent to:

The main problem with the separate lambda definition above is that we cannot take advantage of capturing surrounding variables. To fix this, we could move the named lambda definition to just before the use:

However, this does not help us as the name compare is weak. It is better to just put the lambda definition directly in the parameter:

Then, if you want to use the capture, it is right there:

Exception

If you are going to reuse a lambda, then it makes sense to name it and use that name to ensure consistency and stress that the same functionality is being done.