Reputation: 23654
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.
Upvotes: 2
Views: 194
Reputation: 126522
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
return
statement inmain
has the effect of leaving themain
function (destroying any objects with automatic storage duration) and callingstd::exit
with the return value as the argument. If control reaches the end ofmain
without encountering areturn
statement, 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):
[...].
constexpr
functions andconstexpr
constructors are implicitlyinline
(7.1.2).
Upvotes: 9