Now itโs obvious that these two types are the same,
No, it is not. The type of each lambda expression is a unique, separate type.
From [expr.prim.lambda] / 3:
The type of lambda expression (which is also the type of the closure object) is a unique, unnamed type of non-unit class called the closure type, whose properties are described below.
Therefore, returning the type of the return value for f fails and does not result in std::function<int()> . The latter is an unrelated type of library, which is somehow not magically a โcommon typeโ of any type of closure.
Of course, each of the unique closure types can be converted to std::function<int()> , so if you provide a return type, everything works:
auto f = []() -> std::function<int()> { return 1 ? []() { return 1; } : []() { return 2; }; };
Or, as a simple function:
std::function<int()> f() { return 1 ? []() { return 1; } : []() { return 2; }; }
source share