Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Lambda expressions in C++

Reference for C++ lambda expressions: capture lists, mutable, generic and template lambdas, init captures, constexpr and recursion, with runnable examples.

A lambda expression creates an unnamed function object right where you need it. The compiler generates a class with an operator() whose body is the lambda body, and a data member for every captured variable. Because each lambda has its own unique type, you store one in a variable with auto or pass it to a template or to std::function.

Lambdas are the standard way to give a short piece of behavior to an algorithm, a thread, a callback or a comparator. The capture list [...] decides which variables of the enclosing function the body can use and whether they are copied or referenced.

C++
auto add = [](int a, int b) { return a + b; };
int total = add(2, 3);   // 5

Try it

C++ Captures

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Generic lambdas, algorithms and recursion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
[captures] <template-params>(params) specifiers noexcept -> ret { body }

Everything except [captures] and { body } is optional.

Part Description
[x] / [&x] Capture x by copy / by reference.
[=] / [&] Capture everything used by copy / by reference. Prefer naming the captures.
[this] / [*this] Capture the enclosing object by pointer / copy it (C++17). [=] no longer captures this implicitly in C++20.
[x = expr] Init capture (C++14): a new variable, can also move: [p = std::move(ptr)].
[...args] Capture a parameter pack ([...args = std::move(args)] in C++20).
mutable Lets the body modify the by-copy captures. Otherwise operator() is const.
constexpr / consteval / static Lambda can run at compile time (implicit when allowed); static call operator since C++23, valid only without captures.
-> ret Explicit return type. Otherwise deduced from the return statements, which must agree.
<typename T> Template parameter list (C++20), also with requires.

Usage notes

  • Each lambda has a distinct, unnamed closure type. Two lambdas with identical bodies have different types. Use auto, a template parameter, or std::function / std::move_only_function to hold one.
  • Capture lifetime. A by-reference capture dangles if the lambda outlives the variable. Return or store a lambda only if it captures by value (or by move).
  • Captureless lambdas convert to function pointers: int (*f)(int) = [](int x) { return x + 1; }; and +[](int x){...} forces the conversion.
  • Move-only captures are possible through init captures, and then the lambda is move-only too.
  • mutable changes the copy only, not the original variable, and not between separate copies of the lambda.
  • Generic lambdas (auto parameters) are templates; the body is instantiated for each argument type.
  • Recursion requires a way to name the lambda: pass it to itself, use std::function, or use C++23 explicit this (this auto&& self), as above.
  • Default arguments are allowed since C++14: [](int x, int y = 2) {...}.
  • Unevaluated contexts and default constructibility: captureless lambdas are default-constructible and assignable since C++20, so they can be used as comparator template arguments (std::set<int, decltype(cmp)>).