On this page
  1. Lambdas
  2. Overview
  3. Capture Syntax
  4. Capture Safety

Lambdas

Prev: Templates | Up: builtins | Next: nullptr
Rel: Algorithms & Ranges, auto
Ref: cppreference — Lambda Expressions
Tags: #public #prog #cpp #builtins #functional #tier/core


Overview

A C++ lambda expression constructs an anonymous, unnamed function object (closure) inline. Because each lambda has its own unique compile-time type, passing it to templated algorithms (std::sort, std::ranges::filter) incurs zero heap allocation and zero indirect-call overhead.

Capture Syntax

int threshold = 10;

auto by_value = [threshold](int x) { return x > threshold; };
auto by_ref   = [&threshold](int x) { threshold += x; };
auto moved_in = [ptr = std::make_unique<int>(42)]() { return *ptr; }; // init-capture
auto generic  = [](const auto& a, const auto& b) { return a < b; };   // generic lambda

Capture Safety

Linked from

Related notes