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. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
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+.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Lambda Expressions Capture Mutable Generic” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Lambda Expressions Capture Mutable Generic”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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