Question:
You can declare a function to find out the return type by the type of the return expression:
auto foo();
You can declare the return type at the end:
auto bar() -> int;
But what does the following syntax mean?
auto baz() -> auto;
Answer:
Грамматически, синтаксис auto baz() -> auto;
является корректным в соответствии с https://eel.is/c++draft/dcl.decl#5 и https://eel.is/c++draft/dcl.spec.auto#3.
noptr-declarator parameters-and-qualifiers trailing-return-type
The placeholder type can appear with a function declarator in the decl-specifier-seq, type-specifier-seq, conversion-function-id, or trailing-return-type, in any context where such a declarator is valid.
Про значение написано далее в https://eel.is/c++draft/dcl.spec.auto#3:
If the function declarator includes a trailing-return-type ([dcl.fct]), that trailing-return-type specifies the declared return type of the function.
То есть возвращаемый тип определяется -> auto
в нашем случае. И далее в том же пункте:
If the declared return type of the function contains a placeholder type, the return type of the function is deduced from non-discarded return statements, if any, in the body of the function.
Таким образом, разница между 1 и 3 только формальная: в первом случае declared return type
будет placeholder type auto
до параметров, а в третьем — placeholder type auto
после. Но, так как это один и тот же placeholder type auto
, то поведение у них одинаковое, а именно: the return type of the function is deduced from non-discarded return statements, if any, in the body of the function
.