Inference of the return type of auto return functions without a return type of return in C ++ 11 or 14 is performed as if you were doing
auto retval =
and auto always infers the type of value.
Lambdas are the only auto return functions in C ++ 11. In C ++ 14, it was extended to other functions (by similar rules).
In C ++ 11, you can fix this with the return type ->decltype(var) or ->int const& on your lambda.
In C ++ 14, you can simply ->decltype(auto) , which changes the rules for outputting the type of the return type from
auto retval =
more like
decltype() retval =
which in your case means you are not dropping & or const .
Please note that the returned const& parameters are extremely dangerous, since r values ββcan be overridden in const& implicitly, and the reference extension of the life cycle does not go through a function call. So:
auto&& x = some_function();
is safe even if some_function() returns a temporary value, while f is supposed to behave the way you want f :
auto&& x = f( some_function() );
generates an x dangling reference to a temporary object returned by some_function() .
This is surprisingly surprising, as is the fact that the for(:) loops implicitly use the auto&& parameters behind the scene and clear the time intervals between initializing the range expression and performing the iteration.
This answer is not yet complete, since I have not added a link to the standard.
source share