Lambda expressions — capture list (by value, by reference)
WHAT is a lambda + capture list?
WHY does this exist? (first principles)
A lambda
int n = 10;
auto f = [n](int x){ return x + n; };is compiled to roughly:
struct __Lambda {
int n; // captured BY VALUE -> stored as a member (a COPY)
int operator()(int x) const // const! that's why by-value is read-only by default
{ return x + n; }
};
__Lambda f{ n }; // n copied into the struct at creation timeNow compare by reference:
auto g = [&n](int x){ return x + n; };
// struct { int& n; int operator()(int x) const { return x + n; } };The member is a reference int& — an alias to the original n. So g always sees the current value of n.

HOW to use it — worked examples
The dangling-reference trap
Recall Feynman: explain to a 12-year-old
Imagine you're packing a lunchbox (the lambda) to use later. You can either put an actual sandwich inside (by value) — it's yours, fully portable, but it's the sandwich as it was when you packed it. Or you can tape a note saying "ask Mom for the sandwich in the kitchen" (by reference) — you always get the freshest one, but if Mom goes on vacation (the variable's scope ends), your note points to an empty kitchen and you get nothing (a crash). So: carry it with you → put the real thing in (value). Stay near the kitchen and want it fresh → keep the note (reference).
Active recall
What does the capture list [...] of a lambda specify?
By value [x] vs by reference [&x]: which sees later changes to the original?
[&x] sees the current (live) value; by value [x] keeps the frozen snapshot from creation time.What is a lambda compiled into?
operator(); captures become its member variables.Why can't you modify a by-value captured variable inside a lambda by default?
operator() is const by default; the captured copy is a const member. Use mutable to allow editing the copy.Does mutable editing a by-value capture change the original variable?
What does [=] capture?
What does [&] capture?
What does [=, &y] mean?
y specifically by reference.What is the main danger of capturing by reference?
When the lambda escapes its scope, which capture should you prefer?
What does [this] capture?
this pointer, letting the lambda access its members.Connections
- Function objects (functors) — a lambda is a compiler-generated functor.
- std::function — type-erased wrapper that can hold any lambda.
- References vs pointers in C++ — explains the
int&member behind[&x]. - Object lifetime and scope — the root of the dangling-reference trap.
- const member functions — why by-value captures are read-only by default.
- std::thread and async — classic place where by-reference capture goes wrong.
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, lambda ek chhoti si anonymous function hoti hai jise tum inline likh dete ho. Iska asli jaadu hai capture list [...] — yeh batati hai ki bahar ke (enclosing scope ke) kaunse variables yeh lambda use kar sakti hai, aur kaise. Compiler internally har lambda ko ek hidden struct bana deta hai jiska operator() hota hai, aur captures us struct ke member variables ban jaate hain. Bas yahi ek idea samajh lo, sab clear ho jayega.
By value [n] ka matlab — lambda ke andar n ki ek copy ban jaati hai, creation ke time pe. Soch lo ek photo kheench li. Baad mein bahar wala n change ho jaye, koi farak nahi — tumhari photo waisi hi rahegi (frozen snapshot). By reference [&n] ka matlab — lambda n ka address store karti hai, ek phone line ki tarah. Jab bhi call karoge, original n ki current value milegi.
Ek important baat: by-value capture default mein read-only hoti hai, kyunki operator() const hota hai. Agar copy ko andar modify karna ho to mutable lagao — lekin tab bhi sirf copy badlegi, original nahi. Aur sabse bada trap: by reference tab use mat karo jab lambda apne scope se bahar ja rahi ho (return ho rahi, stored ho rahi, ya thread/async ko di ja rahi). Variable mar jayega, reference dangling ho jayegi, aur program crash (undefined behavior). Aise case mein hamesha by value capture karo taaki data lambda ke andar zinda rahe.
Yaad rakhne ka formula: Value = photo (safe to carry, frozen), Reference = phone line (fresh, but useless agar banda chala gaya). Exams aur real interviews dono mein yeh dangling-reference wala point bahut poocha jaata hai.