Trailing Return Types and auto
How do trailing return types relate to the auto keyword in C++?
Trailing return types and auto are closely related when defining functions. When you use a trailing return type, you must use auto in place of the actual return type before the function name. For example:
auto Add(int x, int y) -> int {
return x + y;
}Here, auto essentially acts as a placeholder, indicating that the actual return type will be specified after the parameter list using the -> syntax.
This is different from using auto without a trailing return type:
auto Subtract(int x, int y) {
return x - y;
}In this case, auto tells the compiler to deduce the return type from the return statement. The function will return an int, but that type isn't explicitly specified in the function signature.
So in summary:
autowith a trailing return type: theautois a placeholder and the actual type is specified after the->autowithout a trailing return type:autotells the compiler to deduce the return type
Trailing Return Types
An alternative syntax for defining function templates, which allows the return type to be based on their parameter types