Table of Contents
Webinar overview:
This presentation by product manager, David Millington, talks about the convenient way to define an anonymous function object added in C++11. This topic was chosen because while it’s extremely useful, the data we see is that there tend to be two groups of C++ developers: those who use them extensively, and those who barely use them.
When to use lambdas:
The main benefits of using lambdas are:
- Improves readability for you or your team.
- Anonymity makes them easier to maintain (no names needed for smaller functions/functors).
- Localizes functions to your code.
Furthermore, lambdas are especially useful if your logic goes inside something else. These code layering problems are a nuisance to reading code—lambdas make it easier to “localize” logic.
Comparing lambdas with traditional functor
On the left is a standard functor with structs and operators written traditionally. It works and functions just as a lambda would but it is longer and arguably more difficult to comprehend when viewed in the context of actual source code.
On the other hand, a lambda is seen as significantly shorter and easier to read. With the structure of a lambda, the code being called is emphasized directly after the functions. The syntax is also unmistakable; just look for the following method syntax:
- [ ] – capture state
- ( ) – function
- { } – body of method
Skip to 18:18 of the replay to learn more about lambda syntax and how to structure inline functions.
Summary: other tips for using lambdas
Here are a couple of other things you should look out for according to the presentation: