Topic: trailing-return-type
Author: Gene Bushuyev <publicfilter@gbresearch.com>
Date: Mon, 27 Sep 2010 17:18:00 CST Raw View
Why does 7.1.6.4/1 requires trailing-return-type even though in
certain contexts it can be deduced from the return statement? -- "The
auto type-specifier signifies that the type of a variable being
declared shall be deduced from its initializer or that a function
declarator shall include a trailing-return-type."
For example,
template<typename T, typename U>
auto myFunc(T&& t, U&& u) -> decltype (forward<T>(t) + forward<U>(u))
{ return forward<T>(t) + forward<U>(u); };
decltype(...) duplicates exactly the return statement, which can be
burdensome and error-prone, and quite unnecessary. It would be better
if the standard allowed to omit trailing-return-type where compiler
can deduce it, e.g.
template<typename T, typename U>
auto myFunc(T&& t, U&& u)
{ return forward<T>(t) + forward<U>(u); };
In fact, there is already a precedent in the standard, in 5.1.2/4
lambda function is allowed to omit trailing-return-type: "If a lambda-
expression does not include a trailing-return-type, it is as if the
trailing-return-type denotes the following type:
=97 if the compound-statement if of the form
{ return attribute-specifier-opt expression ; }
the type of the returned expression after lvalue-to-rvalue conversion
(4.1), array-to-pointer conversion (4.2), and function-to-pointer
conversion (4.3);
=97 otherwise, void."
--
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]