Lambda Expressions Capture Mutable Generic
Write lambdas with captures, mutable semantics, and generic auto parameters.
Lambda Expressions Capture Mutable Generic is a free C++ Academy lesson on CoddyKit — lesson 3 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.
What is a Lambda?
A lambda is an inline anonymous function object. Use it where you would otherwise write a tiny named function or a struct with operator().
auto square = [](int x) { return x * x; };
std::cout << square(5); // 25Syntax Anatomy
A lambda has four parts:
[capture]— what variables from the surrounding scope to use(parameters)— like a function parameter list-> return_type— optional, often deduced{ body }
Empty Capture
[] means "do not capture anything." Lambda accesses only globals and statics.
auto add = [](int a, int b) { return a + b; };Capture by Value
List variables in the capture clause to copy them into the lambda.
int n = 5;
auto times = [n](int x) { return x * n; };
n = 10;
std::cout << times(3); // 15 (captured n was 5)Capture by Reference
Prefix with & to capture by reference. Changes inside the lambda affect the outer variable.
int counter = 0;
auto inc = [&counter]() { ++counter; };
inc(); inc(); inc();
std::cout << counter; // 3Default Captures: [=] and [&]
Capture everything used by value with [=], or everything by reference with [&]. Use with care — be explicit when you can.
auto f = [=]() { return a + b; }; // copies a, b
auto g = [&]() { return a + b; }; // references a, bMutable Lambdas
By default, value captures are const inside the lambda. Add mutable to allow modifying them — the changes affect only the lambda copy.
int n = 5;
auto f = [n]() mutable {
n += 1; // OK because of mutable
return n;
};
f(); f(); // returns 6, then 7
std::cout << n; // still 5 in outer scopeReturn Type Deduction
The compiler deduces the return type from the body. Make it explicit when needed.
auto half = [](int x) -> double { return x / 2.0; };Generic Lambdas (C++14)
Use auto in the parameter list to get a generic lambda that works on any type.
auto print = [](const auto& x) { std::cout << x << "\n"; };
print(42); // int
print("hello"); // const char*
print(3.14); // doubleLambdas as Function Arguments
STL algorithms accept lambdas as predicates and transformations.
std::vector<int> v = {5, 2, 8, 1};
std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });Lambdas Are Function Objects
Under the hood, the compiler generates a class with operator(). You can store lambdas in std::function or auto.
Quick Check
Inside a lambda [n](int x) { return x * n; }, can you assign to n without making the lambda mutable?
Recap
Lambdas are anonymous, inline function objects. Capture by value or reference, mark mutable to modify value captures, and use auto parameters for generic lambdas in C++14+.
Frequently asked questions
Is the “Lambda Expressions Capture Mutable Generic” lesson free?
Yes — the full text of “Lambda Expressions Capture Mutable Generic” 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 Expressions Capture Mutable Generic”?
Write lambdas with captures, mutable semantics, and generic auto parameters. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Lambda Expressions Capture Mutable Generic” 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
- auto decltype and Type Deduction
- Range-based for nullptr and enum class
- Lambda Expressions Capture Mutable Generic
- Structured Bindings and if-init C++17