0Pricing
C++ Academy · Lesson

Lambda Captures by-value by-reference mutable

Choose capture clauses thoughtfully and understand lambda state.

Lambda Captures by-value by-reference mutable is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Captures Recap

A lambda s capture clause lists variables from the surrounding scope it can use. The capture mode controls how each variable is accessed.

Capture by Value

Names listed without & are copied at lambda creation. The lambda owns its copy.

int n = 5;
auto f = [n]() { return n * 2; };
n = 10;
std::cout << f();   // 10 (captured n was 5)

Capture by Reference

Names with & are stored as references to the outer variable. Changes propagate both ways.

int counter = 0;
auto inc = [&counter]() { ++counter; };
inc(); inc();
std::cout << counter;   // 2

Default Capture: =

[=] captures by value everything used in the lambda body. Convenient but can be surprising — it captures only the variables referenced.

int a = 1, b = 2, c = 3;
auto f = [=]() { return a + c; };   // captures a and c (not b)

Default Capture: &

[&] captures by reference everything used. Be careful — references to locals dangle if the lambda outlives the scope.

int n = 0;
auto bad = [&]() { return n; };
return bad;        // n dies, lambda dangles

Mixing Modes

Combine default and specific captures. List individual variables to override the default.

int a = 1, b = 2;
auto f = [=, &b]() {     // a by value, b by reference
    return a + b;
};

Mutable Lambdas

Value-captured variables are const inside the lambda. Add mutable to let the lambda modify its captured copy.

int n = 5;
auto inc = [n]() mutable {
    ++n;
    return n;
};
inc(); inc();
std::cout << n;   // 5 (outer unchanged)

Init Captures (C++14)

Capture an expression result with a new name. Enables capturing by move and computed initial values.

auto ptr = std::make_unique<int>(42);
auto f = [p = std::move(ptr)]() {
    return *p;
};
// ptr is now empty; p owns the int inside the lambda

Capturing this

Inside a member function, [this] captures the this pointer — all members are accessible inside the lambda.

Capturing *this (C++17)

[*this] copies the entire object into the lambda. Useful when the lambda outlives the original object.

Pitfall: Capture by Reference and Async

Never capture local variables by reference into a lambda that runs asynchronously — the locals likely die before the lambda fires.

// BUG
int local = 42;
std::async([&local]() { use(local); });   // local dies, lambda dangles

Quick Check

Which capture mode is correct for letting a lambda hold ownership of a std::unique_ptr from the outer scope?

Recap

Capture by value copies, capture by reference aliases. Use defaults [=] or [&] carefully. mutable permits modifying value-captured copies. Init captures (C++14) enable capturing by move.

Frequently asked questions

Is the “Lambda Captures by-value by-reference mutable” lesson free?

Yes — the full text of “Lambda Captures by-value by-reference mutable” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Lambda Captures by-value by-reference mutable”?

Choose capture clauses thoughtfully and understand lambda state. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Lambda Captures by-value by-reference mutable” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Function Objects Functors
  2. Lambda Captures by-value by-reference mutable
  3. Generic Lambdas and Closures C++14
  4. std::function Type-erased Callables
← Back to C++ Academy