[c++] Variable capturing initializers

In C++, the concept of “variable capturing initializers” refers to the ability to capture and initialize local variables in lambda expressions.

What are Lambda Expressions?

Lambda expressions in C++ are anonymous functions that can capture variables from their surrounding scope. They provide a way to define small, inline functions without the need to explicitly define a separate named function.

Capturing Local Variables

When capturing local variables in a lambda expression, the variables can be captured by value or by reference, and they can also be initialized with a specific value.

In the following example, a lambda expression captures a local variable x by value and initializes it to 10:

int x = 5;
auto lambda = [y = x + 5] { return y; };

In this example, the local variable x is captured and initialized as y with the value x + 5.

Benefits of Variable Capturing Initializers

Using variable capturing initializers in lambda expressions can make the code more concise and readable. It allows for the initialization of captured variables within the lambda itself, reducing the need for additional lines of code to perform the initialization outside the lambda.

Furthermore, capturing and initializing variables within the lambda can help in avoiding potential issues related to mutability and the lifetime of captured variables.


Variable capturing initializers in C++ lambda expressions provide a convenient way to capture and initialize local variables within the context of a lambda. This feature adds flexibility and expressiveness to lambda expressions, allowing for more concise and expressive code.

By leveraging variable capturing initializers, C++ developers can write more functional-style code and take advantage of the expressive power of lambda expressions while maintaining readability and efficiency.

For more information, refer to the C++ documentation on lambda expressions.

Now that you’re familiar with variable capturing initializers, you can use them to enhance the functionality and readability of your lambda expressions in C++.