The short answer
Every lambda expression in C++ has its own type, and you cannot write that type’s name. The compiler creates a class for each lambda, called a closure type. Calling the lambda runs that class’s function call operator. Because the name is out of reach, you usually store a lambda with auto, or with std::function when you need a single declared type.
Two lambdas with the same code have different types
This example uses std::is_same_v, which needs C++17.
#include <iostream>
#include <type_traits>
int main() {
auto a = [](int x) { return x + 1; };
auto b = [](int x) { return x + 1; };
std::cout << std::boolalpha;
std::cout << std::is_same_v<decltype(a), decltype(b)> << "\n"; // false
}
The two lambdas look the same, but each lambda expression in the source code gets its own closure type. Writing the same code twice still produces two different types.
Storing a lambda with auto
auto is the usual choice for a lambda stored in a local variable:
#include <iostream>
int main() {
auto add = [](int a, int b) { return a + b; };
std::cout << add(2, 3) << "\n"; // 5
}
The variable takes the closure type, so you never need to know the name.
Storing a lambda with std::function
std::function is a wrapper that can hold any callable with a matching signature, including lambdas and function pointers. All of them share one declared type:
#include <functional>
#include <iostream>
int main() {
std::function<int(int, int)> op = [](int a, int b) { return a * b; };
std::cout << op(3, 4) << "\n"; // 12
}
This helps when a variable, a member or a container has to hold different lambdas. The wrapper can add some overhead compared with auto, so it is not the default for a single local lambda.
Getting the type with decltype
decltype gives you the closure type of a variable, which is useful when another declaration needs that type:
#include <iostream>
int main() {
auto add = [](int a, int b) { return a + b; };
using AddType = decltype(add);
AddType add_copy = add; // a copy of the same closure type
std::cout << add_copy(4, 5) << "\n"; // 9
}
Converting a lambda without captures to a function pointer
A lambda with an empty capture list can be converted to an ordinary function pointer, as long as the parameters and return type match. A lambda that captures variables cannot:
#include <iostream>
int main() {
int (*times_ten)(int) = [](int x) { return x * 10; };
std::cout << times_ten(4) << "\n"; // 40
}
Passing different lambdas to a template
Can you put a function as a parameter? Yes, and a template is one way to do it. Each lambda has its own type, so a template called with different lambdas creates a separate version for each one:
#include <iostream>
template <typename Func>
void apply_to_five(Func f) {
std::cout << f(5) << "\n";
}
int main() {
apply_to_five([](int x) { return x + 1; }); // prints 6
apply_to_five([](int x) { return x * x; }); // prints 25
}
C++20: default construction and assignment
Since C++20, a lambda with no captures can be default constructed and assigned to. Before C++20, neither was allowed. Lambdas that capture variables still cannot be default constructed.
Quick check
- Every lambda expression has its own unnamed closure type.
- Two identical lambdas still have different types.
- Use
autoto store a lambda in a local variable. - Use
std::functionwhen one declared type has to hold several lambdas or callables. - A lambda with no captures converts to a function pointer with a matching signature.