Trivia impact: rare
Two Identical Lambdas With Different Types
the same-looking expression creates two closure types
auto first = [] {};
auto second = [] {};
std::cout << std::is_same<decltype(first), decltype(second)>::value << "\n";
Does this compile? What does it print?
Why¶
[expr.prim.lambda.closure] gives every lambda-expression a unique, unnamed closure type.
These are two separate lambda-expressions, so the standard does not merge their closure
types merely because their captures, parameters, and bodies look identical. decltype
can name either type after its variable has been declared, but no ordinary class name does.
Where it shows up¶
Templates deduce and instantiate separately for each closure type, and macro expansions can
silently produce more than one such type. Keeping one lambda object (or an alias based on
its decltype) matters when a generic API needs one stable callable type.
Takeaway: every lambda-expression introduces its own closure type, even an empty copy.
Try it: g++ -std=c++17 main.cpp -o demo && ./demo