ProAnswers.org

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

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

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.

Usually main() is called from systems init() function. Thus, it is needed that there can be exactly one definition for main().

Now, if we can inline the main() function and include in a header file then, for every translation unit there will be different definition for main(). Which is not allowed. You can declare main() in a namespace and inline it. But not the global main().

The standard says :

[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. ]