In some cases, you must use the final return type. In particular, the lambda return type, if specified, must be specified through the final return type. In addition, if the return type uses the decltype type decltype which requires the argument names to be in scope, you must use the final return type (however, you can usually use declval<T> to get around this last problem).
The final return type has other minor advantages. For example, consider defining a non-inline member function using the traditional function syntax:
struct my_awesome_type { typedef std::vector<int> integer_sequence; integer_sequence get_integers() const; }; my_awesome_type::integer_sequence my_awesome_type::get_integers() const {
Member type definitions are not in scope until the class name appears before ::get_integers , so we must repeat the class qualification twice. If we use a finite return type, we do not need to repeat the type name:
auto my_awesome_type::get_integers() const -> integer_sequence {
This is not such a big problem in this example, but if you have long class names or member functions of class templates that are not defined by built-in, then this can go a long way in readability.
In his "Fresh Paint" session in C ++ Now 2012, Alisdair Meredith noted that if you use consecutive finite return types, the names of all your functions are aligned neatly:
auto foo() -> int; auto bar() -> really_long_typedef_name;
I used finite return types everywhere in CxxReflect , so if you are looking for an example of how the code looks consistent using them, you can take a look there (for example, the type class ).
James McNellis Jun 26 '12 at 20:28 2012-06-26 20:28
source share