Generic Lambdas and Closures C++14
Write generic lambdas with auto parameters and reason about closure types.
Generic Lambdas and Closures C++14 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.
Generic Lambdas
C++14 lets you write auto in a lambda s parameter list. The compiler generates a templated operator() — the lambda accepts any type.
auto print = [](const auto& x) {
std::cout << x << "\n";
};
print(42);
print("hello");
print(3.14);How They Work
The lambda becomes a class with a templated operator(). Each unique combination of types instantiates a separate function under the hood.
Use Cases
Generic lambdas shine in algorithms that work with multiple types.
std::vector<int> ints = {1, 2, 3};
std::vector<std::string> strs = {"a", "b"};
auto printAll = [](const auto& container) {
for (const auto& x : container) std::cout << x << " ";
};
printAll(ints);
printAll(strs);Multiple auto Parameters
Each auto is independent — different parameters can resolve to different types.
auto add = [](auto a, auto b) {
return a + b;
};
std::cout << add(1, 2.5); // 3.5
std::cout << add(std::string("Hi"), "!"); // Hi!Closures Are Objects
Each lambda expression creates a closure — an instance of a unique compiler-generated class. The closure stores any captured variables as members.
Unique Type per Lambda
Each lambda expression has a unique type, even if two have identical text. Cannot directly write the type name — use auto or std::function.
Storing Lambdas in std::function
To hold a lambda in a variable whose type is known, wrap it in std::function. Adds slight overhead but enables runtime polymorphism over callables.
#include <functional>
std::function<int(int)> doubler = [](int x) { return x * 2; };Type Erasure Cost
std::function uses type erasure — it can store any compatible callable but adds a heap allocation and a virtual call. Avoid in hot loops.
When auto Is Enough
For short-lived lambdas (passed to algorithms, stored in auto), no type erasure is needed and there is no overhead.
Returning Lambdas
Use auto as the return type to return a lambda. Each call creates a fresh closure.
auto makeAdder(int n) {
return [n](int x) { return x + n; };
}
auto add5 = makeAdder(5);
std::cout << add5(10); // 15Higher-Order Functions
Lambdas turn C++ into a comfortable place for higher-order functions — functions that take or return other functions.
Quick Check
What does auto in a C++14 lambda parameter list mean?
Recap
C++14 generic lambdas use auto parameters for compile-time polymorphism. Each lambda is a unique closure type — use auto to hold them, or std::function when type erasure is required.
Frequently asked questions
Is the “Generic Lambdas and Closures C++14” lesson free?
Yes — the full text of “Generic Lambdas and Closures C++14” 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 “Generic Lambdas and Closures C++14”?
Write generic lambdas with auto parameters and reason about closure types. 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 “Generic Lambdas and Closures C++14” 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
- Function Objects Functors
- Lambda Captures by-value by-reference mutable
- Generic Lambdas and Closures C++14
- std::function Type-erased Callables