Lambda Expressions in C++
Lambda expressions in C++ are anonymous functions that can be defined inline.
Introduced in C++11, they allow you to write concise and flexible code for tasks like callbacks, functional programming, and algorithms.
A lambda expression can capture variables from its surrounding scope and has a concise syntax compared to regular functions.
Syntax
</>
Copy
[capture_list](parameter_list) -> return_type {
// Function body
};
- capture_list
- Specifies the variables to capture from the surrounding scope. Can be empty (
[]
) if no variables are captured. - parameter_list
- Defines the input parameters for the lambda, similar to regular functions.
- return_type
- Specifies the return type of the lambda. If omitted, the compiler deduces it automatically.
- Function body
- The code executed when the lambda is called.
Examples
Example 1: Simple Lambda Expression
This example demonstrates a simple lambda that adds two numbers.
</>
Copy
#include <iostream>
using namespace std;
int main() {
auto add = [](int a, int b) {
return a + b;
};
cout << "Sum: " << add(10, 20) << endl;
return 0;
}
Output:
Sum: 30
Explanation:
- The lambda
add
is defined as[](int a, int b) { return a + b; }
, taking two integers and returning their sum. - The lambda is assigned to the variable
add
usingauto
. - The lambda is invoked with arguments
10
and20
, and the result is printed.
Example 2: Lambda with Capture List
This example shows how to capture variables from the surrounding scope in a lambda.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int factor = 10;
auto multiply = [factor](int num) {
return num * factor;
};
cout << "Result: " << multiply(5) << endl;
return 0;
}
Output:
Result: 50
Explanation:
- The variable
factor
is declared in the main function. - The lambda
multiply
capturesfactor
by value using the capture list[factor]
. - When the lambda is called with
5
, it multiplies it by the captured value offactor
and returns the result.
Example 3: Lambda with Mutable Capture
This example demonstrates how to modify a captured variable using the mutable
keyword.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int count = 0;
auto increment = [count]() mutable {
return ++count;
};
cout << "First call: " << increment() << endl;
cout << "Second call: " << increment() << endl;
cout << "Original count: " << count << endl;
return 0;
}
Output:
First call: 1
Second call: 2
Original count: 0
Explanation:
- The variable
count
is captured by value in the lambda. - The
mutable
keyword allows the lambda to modify the captured value. - Each call to
increment
modifies its local copy ofcount
, leaving the original variable unchanged.
Key Points about Lambda Expressions
- Lambdas provide a concise way to define inline functions.
- They can capture variables by value or reference, enabling them to access surrounding variables.
- The
mutable
keyword allows modification of captured-by-value variables. - Lambdas are widely used in modern C++ for callbacks, functional programming, and algorithmic operations.
- They support features like default arguments, trailing return types, and template parameters.