Anonymous Functions in C++
Anonymous functions in C++, also known as lambda expressions, are unnamed functions that can be defined inline and used immediately. Introduced in C++11, these functions are concise and allow for capturing variables from their surrounding scope. Anonymous functions are particularly useful for tasks like callbacks, functional programming, and passing logic to algorithms.
Syntax
</>
Copy
[capture_list](parameter_list) -> return_type {
// Function body
};
- capture_list
- Specifies variables from the surrounding scope to be captured. Use
[]
for no capture. - parameter_list
- Defines the parameters of the lambda, similar to regular functions.
- return_type
- Specifies the return type. If omitted, the compiler deduces it automatically.
- Function body
- The code executed when the lambda is called.
Examples
Example 1: Basic Anonymous Function
This example demonstrates how to define and use an anonymous function to calculate the square of a number.
</>
Copy
#include <iostream>
using namespace std;
int main() {
auto square = [](int x) {
return x * x;
};
cout << "Square of 5: " << square(5) << endl;
return 0;
}
Output:
Square of 5: 25
Explanation:
- The lambda
square
is defined as[](int x) { return x * x; }
, which takes one integer and returns its square. - The lambda is assigned to the variable
square
. - The lambda is called with the argument
5
, and the result is printed.
Example 2: Capturing Variables by Value
This example demonstrates how to capture variables from the surrounding scope by value.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int base = 10;
auto addBase = [base](int x) {
return base + x;
};
cout << "Base + 5: " << addBase(5) << endl;
return 0;
}
Output:
Base + 5: 15
Explanation:
- The variable
base
is declared in the main function. - The lambda
addBase
capturesbase
by value using the capture list[base]
. - When the lambda is called with
5
, it adds the captured value ofbase
to5
.
Example 3: Capturing Variables by Reference
This example shows how to capture variables from the surrounding scope by reference.
</>
Copy
#include <iostream>
using namespace std;
int main() {
int count = 0;
auto increment = [&count]() {
++count;
};
increment();
increment();
cout << "Count: " << count << endl;
return 0;
}
Output:
Count: 2
Explanation:
- The variable
count
is declared in the main function. - The lambda
increment
capturescount
by reference using[&count]
. - Each call to
increment
modifies the originalcount
variable, incrementing its value.
Key Points to Remember about Anonymous Functions
- Anonymous functions (lambdas) allow for concise and inline function definitions.
- They can capture variables from their surrounding scope by value, reference, or both.
- The
mutable
keyword allows modifying captured-by-value variables. - They are useful in functional programming, callbacks, and algorithms like
std::for_each
. - Lambdas support trailing return types and can be assigned to
std::function
for flexibility.