Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this trailing return type legal in C++11?

The following:

auto (*f())() -> int;

gives an error in C++11 mode with Clang saying:

error: 'auto' return without trailing return type; deduced return types are a C++14 extension

but compiles in C++14 mode. GCC compiles without complaining in both modes with -Wall -Wextra -pedantic.

n3337 7.1.6.4/2 says:

The auto type-specifier may appear with a function declarator with a trailing-return-type (8.3.5) in any context where such a declarator is valid.

8.3.5p2 talks about function declarators but I'm too inexperienced to figure it out. Can anyone explain if it's legal in C++11?

like image 319
John Cena Avatar asked May 27 '16 23:05

John Cena


People also ask

Why use trailing return type?

The trailing return type feature removes a C++ limitation where the return type of a function template cannot be generalized if the return type depends on the types of the function arguments.

Can return type be auto?

In C++14, you can just use auto as a return type.

What is return type in CS?

In computer programming, the return type (or result type) defines and constrains the data type of the value returned from a subroutine or method. In many programming languages (especially statically-typed programming languages such as C, C++, Java) the return type must be explicitly specified when declaring a function.

What does decltype auto do?

Use auto and decltype to declare a function template whose return type depends on the types of its template arguments. Or, use auto and decltype to declare a function template that wraps a call to another function, and then returns the return type of the wrapped function.


1 Answers

This is partly covered by CWG 1725:

The treatment of a declaration like the following is not clear:

auto (*f())() -> int; // #1

8.3.5 [dcl.fct] paragraph 2 appears to require determining the type of the nested declarator

auto (*f()); // #2

which, because it does not have a trailing-return-type, would be ill-formed by (C++11) 7.1.6.4 [dcl.spec.auto]. (In C++14, an auto return type without a trailing-return-type is, of course, permitted.)

Rationale (September, 2013): The intent of the C++11 wording is that the requirement for a trailing return type applies only at the top level of the declarator to which auto applies, not to each possible recursive stage in the declarator processing. [..]

Furthermore, according to [dcl.fct]/2,

In a declaration T D where D has the form

     D1 ( parameter-declaration-clause ) [...] trailing-return-type

and the type of the contained declarator-id in the declaration T D1 is “derived-declarator-type-list T, T shall be the single type-specifier auto.

As mentioned in the DR, T D1 is auto (*f()), which is of type "function of () returning pointer to auto" (i.e. matches the requirement). Hence your code is valid in both C++11 and 14, and f's type is "function of () returning pointer to function of () returning int".

like image 181
Columbo Avatar answered Sep 28 '22 13:09

Columbo