Capturing variables in lambdas c++
Lambda expressions in the C++ programming language help C++ users define anonymous functions in a program. One of the most important features of C++ is the concept that lambda expressions provide users with the ability to capture variables from the surrounding program’s outer scope. This allows C++ users to apply lambdas to local variables (including data parameter members) from the scope in which the lambda expression is declared. There are several methods and processes for capturing these lambda expression variable data. C++ users can capture a lambda expression by value, by reference, or by grouping the two methods.

So, let’s take a closer look at C++ programming lambda expressions and their behavior.
Capture Clause in Lambda Expressions.
In C++ programming, the capture clause of a lambda is indicated or declared within square brackets ([]), and it controls which variable data types within the lambda can be accessed and controlled from within the surrounding parameter scope in the current program. C++ users can manage the capture and program scope of variables in a lambda expression in several ways.
Syntax of a Lambda Capture Clause.
[ capture ]( parameters ) -> return_type { function_body }
Element of a Lambda Capture Clause.
- [] – This is the capture clause square bracket in a lambda expression.
- parameters – This is the list of parameter variables defined in the lambda expression; they are defined like a regular function.
- return_type – This is the return type (optional) of a method in a lambda expression.
- function_body – This is the source code in a lambda expression that is executed when the lambda expression is invoked.
Types of capturing variables in a lambda expression.
Capturing by value in a lambda expression.
When C++ users capture a variable by value in a lambda expression, a copy of that variable is created and applied within the current lambda expression. Any modifications made to the variable within the current lambda logic do not affect the original variable in the outer scope.
Example of capturing a lambda expression by value.
#include <iostream>
int main() {
int p = 3;
int q = 4;
// Here we capture by value Lambda expressions
auto total = [p, q]() {
return p + q; // Here p and q are captured by value expression
};
std::cout << “The total of p and q is – ” << total() << std::endl; // Result is – 7
// Here we modify and update the p and q variable value in the outer scope
p = 11;
q = 19;
// Here the values of p and q inside the lambda are unaffected after modification
std::cout << “Total value [after update] – ” << total() << std::endl; // Result is – 7
return 0;
}
Explanation of capturing a lambda expression by value.
- In this example, lambda [p, q]() captures the p and q variables by value, which means it creates a copy of the p and q lambda parameters.
- Modifying the p and q values in the lambda’s outer scope has no effect on the values inside the lambda, because they were captured by value.
Capture by reference in a lambda expression.
When C++ users capture a lambda expression variable by reference in a program, the user-defined lambda logic does not create a copy of the existing variable. Instead, it directly accesses and manages the original variable from the outer scope. As a result, any modifications or updates defined inside the lambda expression affect the original variable in the outer scope.
Example of a lambda expression capturing by reference.
#include <iostream>
int main() {
int p = 6;
int q = 4;
// here we capture by reference Lambda expressions
auto total = [&p, &q]() {
return p + q; // here p and q are captured by reference
};
std::cout << “The total of p and q is – ” << total() << std::endl; // Result is – 10
// here we modify update p and q variable value in the outer scope
p = 7;
q = 9;
// here the values of p and q inside the lambda are updated
std::cout << “Total value [after updatation] – ” << total() << std::endl; // Result is – 16
return 0;
}
Explanation of a lambda expression capturing by reference.
- In this example, the lambda expression [&p, &q]() captures the p and q variables by reference, meaning it operates on the original variables.
- When the p and q variables are modified outside the lambda, these changes are directly reflected in the lambda. This is because the lambda expression captures the references to the original variables.
Capturing all variables in a lambda expression (by value or reference).
C++ users can capture all variables in a lambda expression by value or reference from the variable scope surrounding the lambda expression using the following syntax:
- Capture all variables by value in a lambda expression – [=]
- Capture all variables by reference in a lambda expression – [&]
Example of capturing all variables by value in a lambda expression.
#include <iostream>
int main() {
int p = 6;
int q = 5;
// Here we capture all Lambda expressions’ variables by value method
auto total = [=]() {
return p + q; // Here both p and q are captured by value
};
std::cout << “The total of p and q is – ” << total() << std::endl; // Result is – 11
p = 12;
q = 7;
// Here the captured values do not change, as they were captured by the value method
std::cout << “Total value [after update] – ” << total () << std::endl; // Result is – 11
return 0;
}
Explanation of capturing all variables by value in a lambda expression.
- In this example, [=] captures all local lambda expression variables by value. Any modifications made to the p and q parameters in the lambda’s outer scope do not affect the values captured inside the lambda.
Example of capturing all lambda expression variables by reference.
#include <iostream>
int main() {
int p = 5;
int q = 8;
// Here we capture all variables by reference
auto total = [&]() {
return p + q; // Here both p and q are captured by reference method
};
std::cout << “The total of p and q is – ” << total() << std::endl; // Result is – 13
p = 9;
q = 7;
// The captured values are updated because they were captured by reference
std::cout << “Total value [after update] – ” << total() << std::endl; // Result is – 16
return 0;
}
Explanation of capturing all lambda expression variables by reference.
- In this example, [&] captures all lambda expression variables by reference. Any modifications made to the p and q variables in the lambda’s outer scope are displayed inside the lambda.

