TRT
Sign in
mediumAdvancedconcept

How do lambda captures work, and what is the difference between value, reference, and init capture?

Covers capture modes, the mutable keyword, lifetime implications, and generalized lambda capture for move-only types.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstLambdas: Captures, mutable and AlgorithmsPro — members onlyCapture local state by value or reference, mark stateful lambdas mutable, and pass them to sort, filter and wrapper callables.

A strong answer

A strong candidate frames How do lambda captures work, and what is the difference between value, reference, and init capture? around the exact memory layout, compiler semantics, and hardware execution model covered in Lambdas: Captures, mutable and Algorithms. Covers capture modes, the mutable keyword, lifetime implications, and generalized lambda capture for move-only types.

Sorting four integers in custom order, keeping only even values from a list, and running a small operation with caller supplied logic all need a function defined at the call site. A named function far away adds indirection and cannot see locals without extra parameters. The original lambda example file showed the right ideas but placed statements outside any function and called a helper before it was declared, so it never compiled. The corrected form below wraps every demonstration in a single entry point and declares helpers before use.

A lambda expression defines an unnamed function object where it is needed. It combines a capture list, a parameter list, an optional return type, and a body. The capture list decides which surrounding variables enter the new object and whether they enter by copy or by alias.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: Write lambdas with value, reference and mixed captures.
  • Mastery of core invariant: Apply mutable to modify a value capture inside the body.
  • Mastery of core invariant: Pass lambdas to sort, copy_if and function wrappers.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01Why does a lambda need mutable to modify a value capture?
  2. 02What lifetime risks does reference capture introduce?

Not started

Sign in to save your learning progress.

Sign in to save