[RFC] Deprecate "implicit int" for main() in C++
Jonathan Wakely
jwakely@redhat.com
Wed Apr 25 15:31:00 GMT 2018
On 25/04/18 14:53 +0100, Andrew Haley wrote:
>On 04/25/2018 01:23 PM, Jonathan Wakely wrote:
>
>> We enabled -Wreturn-type by default in GCC 8, so code using the
>> extension will get warnings even without -Wall now. Users might want
>> to use -Werror=return-type to ensure they aren't bitten by the new
>> optimizations that assume control never reaches the end of a
>> non-void function.
>
>But ISO C++ allows control to reach the end of main(), and
>automagically returns 0. I guess you didn't mean that, but your reply
>was confusing.
>
>N4659, Section 6.6.1 Para 5:
>
> If control flows off the end of the compound-statement of main, the
> effect is equivalent to a return with operand 0 (see also 18.3).
Yes, I should have said "never reaches the end of a non-void function
other than main".
-Wreturn-type doesn't warn about flowing off the end of main, because
it's well-defined. There's definitely scope for confusion, because
-Wreturn-type gives the main function special treatment in two ways:
* Unlike normal functions, flowing off the end of main doesn't warn,
because the standard defines what happens there.
* Unlike normal functions, G++ allows omitting the return type of
main.
This is a non-standard extension to C++ (implicit int return types
are allowed by C89 but not by any version of C++).
What I'm proposing for deprecation is the non-standard extension that
allows:
main() { return 0; }
More concretely, deprecating it for a few releases would allow us to
apply the attached patch at some point in the future, so that instead
of:
rt.c:1:6: warning: ISO C++ forbids declaration of âmainâ with no type [-Wreturn-type]
main() { return 0; }
^
We'd get:
rt.c:1:6: error: ISO C++ forbids declaration of 'main' with no type [-fpermissive]
main() { return 0; }
^
More information about the Gcc
mailing list