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?
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.
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.
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.
First, let's see why you would want to use trailing return types in general.
Kerrek SB's comment to your previous question:
Trailing return types are a specialized language feature that's mostly useful for generic library writers (that is, writers of generic libraries, not generic personalities who happen to be writing libraries), similar to decltype. Incidentally, both language features also have some limited use in obscure or long lambda expressions, but they shouldn't be used a lot on "normal" user code.
From Dietmar Kühl's answer (that you have linked in your previous question so you must have read it):
The significance of trailing return types is primarily for function template where it is now possible to use parameters to the function together with
decltype()
to determine the return type. For example:template <typename M, typename N> auto multiply(M const& m, N const& n) -> decltype(m * n);
This declares the function
multiply()
to return the type produced bym * n
. Putting the use ofdecltype()
in front ofmultiply()
would be invalid becausem
andn
are not, yet, declared.
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.Are there any reasons that main with trailing return type should be avoided and the classical notation should be preferred?
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.
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