In the code example below the main
function is written with the C++11 trailing return type notation:
auto main() -> int {
//...
return 0;
}
Question:
Are there any reasons that main
with trailing return type should be avoided and the classical notation should be preferred?
It's plain stupid.
There's no gain, no need or reason to write something like this.
To be pedantic you add the
auto
and->
symbol for no reason.A trailing return type is typically used to deduce the return type after the function arguments have been introduced. Here you already know the return type.
Can you imagine (the looks of) your code base if all your functions used this notation without the need to do so ? You'd practically keep at the front all the storage, likage specifications, static etc and leave the return type at the end, to mingle with exception specifications, const specifiers and friends ?
People you don't need to convince me. I'm not against trailing return types; I'm against the "nouveau riche" mentality of using features where there's no need to do it and concerned about C++ becoming a huge blob of styles and collapsing under its own weight.
Lighthearted shifts of the norm are signs of instabillity and lack of communication. A feature like Python's PEP8 would be a good thing to have and trained eyes should be discarded with caution.
First, let's see why you would want to use trailing return types in general.
Kerrek SB's comment to your previous question:
From Dietmar Kühl's answer (that you have linked in your previous question so you must have read it):
I consider both Kerrek SB and Dietmar Kühl C++ experts and find their guidelines good. Now let's see how the above guidelines apply to
int main()
. Some observations:int main()
is not a function template.int
) won't change in the foreseeable future; we can safely commit to this type.Yes:
It confuses those developers who are not familiar with the new syntax.
Not all tools support this new language feature.
As discussed above, using this feature is unnecessary with
int main()
.I rest my case.
It's perfectly valid and works just fine.
The only issue to concern is that it is new. It may confuse or surprise readers of your code who are only familiar with C++98.
But it works, so feel free to write your
main
this way if you feel like it.