Trailing Return Type

This new feature going to help specify the return type after the function arguments. Notice, in a new syntax for declaring functions, one in which the return type comes after the function name and arguments list instead of before them:

Consider the following example with traditional syntax code a function:

int f1(double, int);

And here is the way using new syntax:

auto f2(double, int) -> int;

The new syntax may look like a step backwards in readability for the usual function declarations, but it does make it possible to use decltype to specify template function
return types:

template < typename T1, typename T2 >
auto Foo(T1 t1, T2 t2) -> decltype(T1*T2)
{
  ...
}

The problem that’s addressed here is that T1 and T2 are not in scope before the compiler
reads the eff parameter list, so any use of decltype has to come after the parameter list.
The new syntax makes that possible.

2 Responses to Trailing Return Type

  1. Caro says:

    Relatively unimportant, but your second code example should have a trailing ‘>’ instead of ‘)’. Good description however!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s