I have this very simple code:
constexpr int main()
{
return 0;
}
I understand from C++11 standard N3485 that it is illegal:
The function main shall not be used within a program. The linkage (3.5) of main is implementation-defined. A program that defines main as deleted or that declares main to be inline, static, or constexpr is illformed. The name main is not otherwise reserved.
When I run it using gcc 4.7.2. it outputs the following error:
prog.cpp:1:20: error: cannot declare ‘::main’ to be inline
Meanwhile, if I remove the return 0 from the function body, it reports the same error without even giving a warning about missing return statement from main.
Is this a bug of gcc 4.7.2? Why constexpr is reported as inline? Does the second phenomenon mean that it is OK to not put return statement in main or is it by default return 0 even I do not put return statement into it (I know this is bad practice)?
Thank you.
main() is the only value-returning function which is allowed to have the return statement omitted. Flowing off the end of main() without returning anything is equivalent to returning 0 (while for other functions it is undefined behavior, see 6.6.3/2).
Per paragraph 3.6.1/5 of the C++11 Standard:
A
returnstatement inmainhas the effect of leaving themainfunction (destroying any objects with automatic storage duration) and callingstd::exitwith the return value as the argument. If control reaches the end ofmainwithout encountering areturnstatement, the effect is that of executingreturn 0;
Concerning the inline message, constexpr functions are implicitly inline. Per paragraph 7.1.5/2 of the C++11 Standard (courtesy of Luc Danton):
[...].
constexprfunctions andconstexprconstructors are implicitlyinline(7.1.2).
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