Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a placeholder in a trailing-return-type override an initial placeholder?

g++ appears to accept any combination of auto and decltype(auto) as initial and trailing return types:

int a;
auto f() { return (a); }                             // int
auto g() -> auto { return (a); }                     // int
auto h() -> decltype(auto) { return (a); }           // int&
decltype(auto) i() { return (a); }                   // int&
decltype(auto) j() -> auto { return (a); }           // int
decltype(auto) k() -> decltype(auto) { return (a); } // int&

However, clang rejects j and k, saying: error: function with trailing return type must specify return type 'auto', not 'decltype(auto)' (demonstration).

Which compiler is correct? Which rule (auto or decltype(auto)) should be used in each case? And does it make any sense to use a placeholder type in a trailing-return-type?

like image 652
ecatmur Avatar asked Jun 17 '14 09:06

ecatmur


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 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.

What is return type in C++?

In C++, a function which is defined as having a return type of void , or is a constructor or destructor, must not return a value. If a function is defined as having a return type other than void , it should return a value.


1 Answers

auto is required when introducing a trailing-return-type.

§8.3.5 [dcl.fct] /2

In a declaration T D where D has the form

D1 ( parameter-declaration-clause ) cv-qualifier-seqoptref-qualifieroptexception-specificationoptattribute-specifier-seqopttrailing-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. [...]

See also Core Issue 1852 for the apparent contradiction with [dcl.spec.auto]/1.

like image 98
Xeo Avatar answered Sep 20 '22 13:09

Xeo