C++ Notes Help

Lambdas

They are inline functions which can be used for short snippets of code that are not going to be reused and therefore do not require a name.

void main() { [] () {cout<<"Hello"<<endl;} (); => Hello }

Syntax

[capture](parameters) -> return_type { body };

Parameters and return type

[](int a, int b) { return a + b; }

Return type is usually inferred.

  • But required if:

    • Multiple return paths

    • Confusing types

[](int x) -> double { if (x > 0) return x * 1.5; return 0.0; };

Usage

void main() { int sum = [] (int a, int b) {return a+b;} (10, 20); cout<<sum; // 30 }
void main() { // Assigning the function reference to a pointer auto f = [] () {cout << "Hello World" << endl;}; f(); // Hello World }

Capture List

The capture list decides what outside variables the lambda can see and how.

1. Capture nothing

[]() { cout << "No outside access"; };

2. Capture by value

int x = 10; auto f = [x]() { cout << x; };
int main() { int a = 10; auto f = [a] () {cout << a << endl;}; // statement 1 f(); => 10 a++; f(); => 10 }

The value of a is replaced at the definition of the lambda expression, hence it does not access the latest/updated value of a, but the original copied value of a.

Moreover, Here we cannot modify the value of captured variables inside the lambda expression.

Capture by value variables are read-only.

3. Capture by reference

int x = 10; auto f = [&x]() { x++; };
  • Refers to the original variable

int main() { int a = 10; auto f = [&a] () {cout << ++a << endl;}; f(); => 11 ++a; f(); => 13 }

4. Capture everything

[=] // all by value [&] // all by reference

5. Mixed capture

[x, &y]()

Lambdas vs function pointers

int (*fp)(int, int); auto lambda = [](int a, int b) { return a + b; };

Lambdas without captures ✅ can be converted to function pointers.

int (*fp)(int, int) = [](int a, int b) { return a + b; };

Internal Implementation

A lambda is an unnamed function object.

auto add = [](int a, int b) { return a + b; };
  • It creates:

    • A compiler-generated class

    • With an overloaded operator()

// So, this add(2, 3); // is really: add.operator()(2, 3);
Last modified: 08 February 2026