Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should main with trailing return type be avoided? [closed]

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?

like image 892
101010 Avatar asked Jun 27 '14 15:06

101010


People also ask

Why use trailing return type?

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.

Can return type be auto?

In C++14, you can just use auto as a return type.


3 Answers

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.

like image 111
Nikos Athanasiou Avatar answered Sep 19 '22 11:09

Nikos Athanasiou


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.

like image 34
jalf Avatar answered Sep 21 '22 11:09

jalf


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 by m * n. Putting the use of decltype() in front of multiply() would be invalid because m and n 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.
  • There is no type deduction going on.
  • The return type (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:

  1. It confuses those developers who are not familiar with the new syntax.

  2. Not all tools support this new language feature.

  3. As discussed above, using this feature is unnecessary with int main().

I rest my case.

like image 31
Ali Avatar answered Sep 21 '22 11:09

Ali