TRT
Sign in

Advanced · Module 10 · Lesson 4 of 6

Lambdas: Captures, mutable and Algorithms

Capture local state by value or reference, mark stateful lambdas mutable, and pass them to sort, filter and wrapper callables.

Pro — members onlyAbout 11 minutes

By the end you can

  • Write lambdas with value, reference and mixed captures
  • Apply mutable to modify a value capture inside the body
  • Pass lambdas to sort, copy_if and function wrappers

Builds on Casts With Intent: static, dynamic, const and reinterpret.

01 · A question first

A sort needs a custom order, a filter needs a predicate, and a thread pool needs work packaged with its data. All three call sites accept a small function defined on the spot. How does one expression carry code plus the variables it needs?

Make a guess before you read on.

02 · The idea

Members only

Unlock the full lesson

This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.

What's inside

  • Start with the problem
  • How the idea works
  • Why it is correct
  • What it costs
  • Generalized lambda capture and move-only state
  • A generic lambda with init capture example
  • Capture defaults and when to avoid them
  • Pitfalls and variations

03 · Practice

Something wrong with this lesson? Report a problem