How to use C++ Lambdas with Capture Clauses?
Last Updated :
09 May, 2024
In C++, lambda expressions provide a convenient way to create one-liner functions. One of the key features of lambdas is their ability to capture variables from their surrounding scope by using capture clauses. This allows lambdas to access and modify variables from the outer scope, even after the lambda itself has been created. In this article, we will learn about what are lambdas with capture clauses in C++.
C++ Lambdas with Capture Clauses
Capture clauses are specified within the square brackets after the lambda expression's parameter list. They also define how the variables from the surrounding scope are being captured by the lambda.
Following is the syntax for lambda expressions with capture clauses:
Syntax
[capture-list] (parameters) -> return-type {
// lambda body
}
There are mainly two types of capture clauses in lambda expressions :
- Capturing a Variable by Value : A copy of a variable is captured at the time when the lambda is being created. Any particular changes made within the lambda expression affects this copy only , by leaving the original variable as it is.
- Capturing a Variable by Reference: Instead of just a copy, a direct reference to the variable can be captured. The modifications made within the lambda will be reflected in the original variable. To capture a variable by reference you can use the &dereferencing operator.
C++ Program to Capture a Variable by Value using Lamda
In the following example , we will capture a variable x by value in the lambda expression. Later the value of the variable is modified but it is not reflected inside the lambda function because the variable was captured using the value.
C++
// C++ program to capture a value by value in lambda
// expression
#include <iostream>
using namespace std;
int main()
{
int x = 50;
// capture x by value
auto lambda
= [x]() { cout << "Inside lambda: " << x << endl; };
// modify the value of x outside lambda expression
x = 100;
// Call the lambda function to print its captured value
// of x
lambda();
// Print the original value of x
cout << "Outside lambda: " << x << endl;
return 0;
}
OutputInside lambda: 50
Outside lambda: 100
Time Complexity: O(1)
Auxiliary Space: O(1)
C++ Program to Capture a Variable by Reference using Lambda
In the following example , we will capture a variable x by reference in the lambda expression. Later the value of the variable is modified and the modified value will be used in the lambda function as the variable was passed by reference.
C++
// C++ program to capture a value by reference in lambda
// expression
#include <iostream>
using namespace std;
int main()
{
int x = 50;
// capture x by reference
auto lambda = [&x]() {
cout << "Inside lambda: " << x << endl;
};
// modify the value of x outside lambda expression
x = 100;
// Call the lambda function to print its captured value
// of x
lambda();
// Print the original value of x
cout << "Outside lambda: " << x << endl;
return 0;
}
OutputInside lambda: 100
Outside lambda: 100
Similar Reads
How to Capture std::vector in Lambda Function? In C++, lambda functions allow users to define an inline function anywhere in the program. They are also able to capture the objects from outside its definition. In this article, we will look at how to capture a std::vector object in lambda functions in C++. Capture std::vector in Lambda FunctionTo
2 min read
How to Capture Pairs in Lambda Expression in C++? In C++, lambda expressions provide an easy way to define anonymous functions inline. They also allow these inline functions to capture some variables from the surrounding scope. In this article, we will learn how to capture pairs in lambda functions in C++ Capture Pairs in Lambda Expressions in C++W
2 min read
When to Use Lambda Expressions Instead of Functions in C++? In C++, both lambda expressions and functions are used to define operations that can be invoked somewhere else in the code. However, there are some cases where using lambda expressions can be more beneficial than using functions. In this article, we will learn when to use lambda expressions instead
4 min read
Recursive lambda expressions in C++ A recursive lambda expression is the process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. Using a recursive algorithm, certain problems can be solved quite easily. Examples of such problems are Towers of Ha
8 min read
How to Create a Thread in C++? A thread is a basic element of multithreading which represents the smallest sequence of instructions that can be executed independently by the CPU. In this article, we will discuss how to create a thread in C++. How to Create a Thread in C++?In C++, the std::thread is a class template that is used t
2 min read