Lambdas—how to capture everything and stay sane

Speaker: Dawid Zalewski

Audience level: [ Beginner | Intermediate ]

Lambdas have been around for over a decade. Yet, as they continue to rapidly evolve, the understanding of how some of their features work is far from widespread among developers. Not surprisingly, given that even such routine tasks as capturing are governed by complex and ever-changing rules.

This talk focuses on lambdas’ captures. Staring with the question of what it means to capture into a lambada and what closures are, we’ll find out what really needs to be captured (not everything) and why captured objects cannot be changed (but not all of them). We'll learn what types of captures exist and how to mix them. From there, we’ll see how init captures allow for more complex behaviors, like capturing move-only objects or by const references. We’ll give capturing this the separate treatment it deserves, and discuss the related lifetime issues at lengths. Finally, we’ll shift attention to capturing parameter packs, structured binding and other less common scenarios.

After attending this talk, you’ll build a strong understanding of what lambdas are internally, and how the design of closure objects influences the capturing behavior. You will be better equipped to write captures that lead to improved performance and learn to avoid common mistakes. In short, you will be able to capture into lambdas with ease and confidence.