C ++ was Turing completed long before C ++ 11. A lambda does not exist to do something impossible. You can do a lot without them. They are designed to optimize some code fragments.
For example, to provide the ability to create functors on the fly in the standard library of algorithms. Of course, you can determine one at your discretion. But this requires going beyond your function and the name of a new type. As an example, there are many templates for a simple call to std::transform .
They keep the code localized, don't require you to pollute any names, and are easy to use when you hang it.
As a bonus, in his talk about functional C ++ , Kevlin Henny reduces an example function from a framework to something that really screams “use lambda.” Therefore, to borrow his conversation a little, consider the function of registering commands:
void register_command(std::string cmdName, std::function<void()> f);
You want to register a command that simply turns on the light bulb. How many templates do you need to write for a custom functor compared to the following?
register_command("Turn On Light-bulb", [&light_controller]() { light_controller.on(); });
Think about it, and which code base would you rather support.
Storyteller
source share