Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why main() in C++ cannot be inlined?

Tags:

c++

main

inline

I was reading the C++ FAQs and I noticed one sentence.

main() cannot be inline.

Why is this?

like image 602
jon Avatar asked Aug 08 '11 10:08

jon


People also ask

What functions Cannot be inlined?

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.

Can main be inline?

Show activity on this post. I was reading the C++ FAQs and I noticed one sentence. main() cannot be inline.

How do you check if a function is inlined or not?

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.

When can a function be inlined?

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.


2 Answers

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.

like image 165
sepp2k Avatar answered Oct 08 '22 00:10

sepp2k


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 inlined main(). [..] [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.

like image 43
Lightness Races in Orbit Avatar answered Oct 08 '22 00:10

Lightness Races in Orbit