Search⌘ K

Lambda Functions

Explore how to use lambda functions in C++ to write concise, unnamed functions that can capture local variables and maintain state. Understand the syntax, capturing mechanisms, and benefits of lambdas as function objects. Learn how C++14 generic lambdas work and how to design effective lambdas for practical programming.

A lambda function, or lambda, is a function without a name.

A lambda can be written in-place and doesn’t require complete implementation outside the scope of the main program.

A cool feature of lambdas is that they can be treated as data. Hence, they can be stored or copied in variables.

Syntax #

widget
  • []: Captures the used variables.
  • (): Necessary for parameters.
  • ->: Necessary for complex lambda functions.
  • {}: Function body, per default const.
    • []() mutable -> {...} has a non-constant function body.

What exactly do we mean by capture?

Function vs. function

...