What is the usage of adding -> auto
in []() -> auto { return 4; }
?
For me - it is not different than []() { return 4; }
What is the usage of adding -> auto
in []() -> auto { return 4; }
?
For me - it is not different than []() { return 4; }
It is
auto
by default. The Standard, [expr.prim.lambda]/4, reads:My addition.
So,
-> auto
itself is not useful. However, we can form other return types withauto
, namely:-> auto&
,-> const auto&
,-> auto&&
,-> decltype(auto)
. Standard rules of return type deduction apply. One should bear in mind thatauto
is never deduced to be a reference type, so by default a lambda returns a non-reference type.A few (trivial) examples:
PiotrNycz's addition. As pointed out in comments (credit to @StoryTeller) - the real usage is version with
auto&
andconst auto&
and "The degenerate case is just not something worth bending backwards to disallow."See: