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:

  1. The lambda add is defined as [](int a, int b) { return a + b; }, taking two integers and returning their sum.
  2. The lambda is assigned to the variable add using auto.
  3. The lambda is invoked with arguments 10 and 20, 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:

  1. The variable factor is declared in the main function.
  2. The lambda multiply captures factor by value using the capture list [factor].
  3. When the lambda is called with 5, it multiplies it by the captured value of factor 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:

  1. The variable count is captured by value in the lambda.
  2. The mutable keyword allows the lambda to modify the captured value.
  3. Each call to increment modifies its local copy of count, leaving the original variable unchanged.

Key Points about Lambda Expressions

  1. Lambdas provide a concise way to define inline functions.
  2. They can capture variables by value or reference, enabling them to access surrounding variables.
  3. The mutable keyword allows modification of captured-by-value variables.
  4. Lambdas are widely used in modern C++ for callbacks, functional programming, and algorithmic operations.
  5. They support features like default arguments, trailing return types, and template parameters.