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?
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.
In C++14, you can just use auto as a return type.
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.
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.
auto is required when introducing a trailing-return-type.
§8.3.5 [dcl.fct] /2
In a declaration
T DwhereDhas the form
D1 (parameter-declaration-clause)cv-qualifier-seqoptref-qualifieroptexception-specificationoptattribute-specifier-seqopttrailing-return-typeand the type of the contained declarator-id in the declaration
T D1is “derived-declarator-type-listT”,
Tshall be the single type-specifier auto. [...]
See also Core Issue 1852 for the apparent contradiction with [dcl.spec.auto]/1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With