New adopted paper: N3638, Return Type Deduction for Normal Functions -- Jason Merrill
Note: This paper was adopted into draft C++14 on Saturday at the Bristol UK ISO C++ meeting.
A new WG21 paper is available. A copy is linked below, and the paper will also appear in the next normal WG21 mailing. If you are not a committee member, please use the comments section below or the std-proposals forum for public discussion.
Document number: N3638
Date: 2013-04-17
Return type deduction for normal functions
by Jason Merrill
Excerpt:
Any C++ user introduced to the C++11 features of
auto
, lambdas, and trailing return types immediately wonders why they can't just writeauto
on their function declaration and have the return type deduced. This functionality was proposed previously in N2954, but dropped from C++11 due to time constraints, as the drafting didn't address various questions and concerns that the Core WG had. I have now implemented this functionality in GCC, and propose to add it to C++14. I discuss some of the less obvious aspects of the semantics below.This proposal also resolves core DRs 975 (lambda return type deduction from multiple return statements), 1048 (inconsistency between
auto
and lambda return type deduction), and 1588 (deducing cv-qualifiedauto
).
Note that this paper also allows lambdas (not just functions) with multiple return statements to have their return type deduced. Examples in the paper include multiple returns, recursion, and more:
auto iterate(int len) // return type is deduced as int
{
for (int i = 0; i < len; ++i)
if (search (i))
return i;
return -1;
}
auto sum(int i) {
if (i == 1)
return i; // return type deduced to int
else
return sum(i-1)+i; // ok to call it recursively now
}
template <class T> auto f(T t) { return t; } // return type deduced at instantiation time
[]()->auto& { return f(); } // return a reference