main() trailing return type

Mar 1, 2023 at 12:54pm
1
2
int main() {
}


with Clang produces the warning message:


<source>:1:5: warning: use a trailing return type for this function [modernize-use-trailing-return-type]
int main() {


Using:

1
2
auto main() -> int {
}


removes the warning.

C++ Core Guidelines F46 specifies int is the return type for main() - but not that a trailing return type is preferred.

Is there a reason why Clang prefers a trailing return type?
Mar 1, 2023 at 1:30pm
> Is there a reason why Clang prefers a trailing return type?

Using a trailing return type for main is just plain stupid.
There is no real type deduction with auto main() -> int
and the return type of main() is never going to be anything other than int.

Stick to the straightforward int main(); blithely ignore the noise that may be generated by the 'almost-always-auto' crowd.
Mar 1, 2023 at 1:34pm
I think the warning comes from Clang-Tidy, not Clang itself.

I have never used Clang-Tidy but I guess you can configure it somehow. You could probably disable the modernize-use-trailing-return-type warnings if you don't like them.
Mar 1, 2023 at 2:05pm
Using a trailing return type for main is just plain stupid.
There is no real type deduction with auto main() -> int
and the return type of main() is never going to be anything other than int.

Stick to the straightforward int main(); blithely ignore the noise that may be generated by the 'almost-always-auto' crowd.


I've never used auto with main() - always int, but just wondered if there was a good reason why Clang was reporting this. Seems not which is just what I thought.
MS VS doesn't generate any 'noise' about int main().

Clang drops a clanger!
Last edited on Mar 1, 2023 at 2:05pm
Mar 1, 2023 at 2:14pm
For templates and lambdas a trailing return type can be useful, if a bit verbose.

Insisting main be so bowdlerized is a bit head-up-their-fundament geeky.

cppreference's page on main:

https://en.cppreference.com/w/cpp/language/main_function

I guess the C++ standard allows an implementation to allow the trailing return type.

I honestly see little to no advantage of using a trailing return type with main.

If the standard committee ever formalized a trailing return type as part of the standard I still wouldn't use it. I like auto as a return type when it can reduce typing.
Mar 1, 2023 at 2:22pm
The mention about main in the C++ Core Guidelines is very specific, to tell the "compiler extension" devotees who insist void main is legit to shut the hell up.

F.46: int is the return type for main()
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-main
Mar 1, 2023 at 2:26pm
seeplus wrote:
just wondered if there was a good reason why Clang was reporting this

This warning doesn't seem to be enabled by default.

https://godbolt.org/z/44xYY58Yd <-- Remove -checks=* and the warning goes away.

Given that you (or your IDE/build system) have enabled this warning then it makes sense that it reports the warning consistently for all functions.

The Clang-Tidy documentation says:
This transformation is purely stylistic.
https://clang.llvm.org/extra/clang-tidy/checks/modernize/use-trailing-return-type.html
Last edited on Mar 1, 2023 at 2:31pm
Mar 1, 2023 at 2:53pm
George P wrote:
If the standard committee ever formalized a trailing return type as part of the standard ...

I'm pretty sure the standard already allows a trailing return type for the main function as long as it's int.

The C++ Standard says:
An implementation shall allow both
– a function of () returning int and
– a function of (int, pointer to pointer to char) returning int
as the type of main
https://eel.is/c++draft/basic.start.main#2

It doesn't say how you should specify the return type int.

auto main() -> int {} means the return type is int. It's just a roundabout way of writing it.
Last edited on Mar 1, 2023 at 3:17pm
Mar 1, 2023 at 4:31pm
The standard may allow it, but it doesn't specify it in explicit terms except as some "implementation allowed" after-thought.

It is not a formal part of the standard at this time, period.
Mar 1, 2023 at 5:58pm
Nah, the standard sounds pretty clear here. main is still returning int, it's just written a different way.

But anyway I agree with JLBorges and the premise of this warning is dumb.

If this forum moved faster maybe there'd be more interesting stuff to talk about.
Mar 1, 2023 at 6:53pm
If this forum moved faster maybe there'd be more interesting stuff to talk about.

Must admit, I'm curious... where are C++ students going to get people to do their homework for them get help on programming, these days?
Mar 1, 2023 at 7:24pm
George P wrote:
The standard may allow it, but it doesn't specify it in explicit terms except as some "implementation allowed" after-thought.

It is not a formal part of the standard at this time, period.

I meant that we can count on it being allowed. I.e. implementations has to allow it.

I think your interpretation makes sense for the old wording that was used before C++14.

The current wording, however, doesn't mention any specific syntax. If one keeps complaining about auto main() -> int not being explicitly mentioned one should do the same for int main() because it's no longer explicitly mentioned either.

MikeyBoy wrote:
where are C++ students going to get people to do their homework for them get help on programming, these days?

Reddit has a pretty active C++ community:
https://www.reddit.com/r/cpp/
https://www.reddit.com/r/cpp_questions/

At least it had a couple of years ago. I don't hang out there any more. Too many interesting topics to respond and too much time wasted. That's why I came back here. ;)
Last edited on Mar 1, 2023 at 8:05pm
Mar 1, 2023 at 11:23pm
I'm curious... where are C++ students going

ChatGPT, of course!
Mar 3, 2023 at 10:52am
where are C++ students going to get people to do their homework for them get help on programming, these days?


Is C++ still being taught much in colleges/universities? I've looked at a selection of Universities web sites and I only found one that mentioned C++ for a second year course. The vast majority covered Python/Java/Javascript
Topic archived. No new replies allowed.