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?