Generic Lambdas and Closures C++14
Write generic lambdas with auto parameters and reason about closure types.
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.
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