I was reading the C++ FAQs and I noticed one sentence.
main() cannot be inline.
Why is this?
The only situation in which a function cannot be inlined is if there is no definition for the function in the compilation unit. Even that will not prevent link-time inlining by a link-time optimizer.
Show activity on this post. I was reading the C++ FAQs and I noticed one sentence. main() cannot be inline.
If you need to make sure that function is inlined and OK to go with proprietary extension in MS VC++, check out the __forceinline declarator. The compiler will either inline the function or, if it falls into the list of documented special cases, you will get a warning - so you will know the inlining status.
Inline function may increase efficiency if it is small. 2) If a function contains static variables. 3) If a function is recursive. 4) If a function return type is other than void, and the return statement doesn't exist in function body.
In C++ it is not legal to call the main function in your code, so there'd be no way it could ever be inlined.
Because the standard says so:
[2003: 3.6.1/3]
: The function main shall not be used (3.2) within a program. The linkage (3.5) of main is implementation-defined. A program that declares main to be inline or static is ill-formed. The name main is not otherwise reserved. [Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces. ]
And why does it say so? Because it's trying to leave as much about the implementation of main
to the individual .. well, implementation .. as is possible, and doesn't want to limit implementations by requiring that inline
be valid here when it arguably has no practical benefit.
My friend on the committee confirmed this:
There's no reason why an
inline
main()
wouldn't work, per se. [..] I could have a C++ interpreter that can invoke inlinedmain()
. [..] [But]inline
/static
main()
are forbidden in order to hopefully avoid confusion. I find it hard to imagine that the rationale would be anything additional to what's already been said in [this Q&A].
BTW, don't confuse the inline
hint keyword with actually inlining functions. You can mark a function inline
and it may not be physically inlined.
So, even if it were true that main
"cannot be inlined" (and strictly speaking it is not true, though inlining main
would be rather awkward and pointless as explained in other answers), it could theoretically still support the inline
hint keyword just fine.
It doesn't for the reason stated above, and in litb's answer: it would complicate matters for no real benefit.
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