Home | Projects | Notes > C++ Programming > Stateful Lambda Expressions

Stateful Lambda Expressions

 

C++ Stateful Lambda Expressions

A stateful lambda expression in C++ is a lambda that captures variables from its surrounding scope, allowing it to retain state between calls or access external context. This is done through the capture list, the part in square brackets [] before the parameter list.

The Structure of a Stateful Lambda Expression

captured_variables - Non-empty capture list: Defines what information/variables should be captured.

Compilation of Stateless Lambda Expressions

Compilation of Stateful Lambda Expressions

The Ways a Lambda Expression can Capture "Variables"

 

Note

With so many possible combinations of default and explicit captures, it's difficult to cover them all. However, what we've discussed should equip you for most situations where stateful lambdas are needed.