0Pricing
C++ Academy · Lesson

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

  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