Why calling main() is not allowed in C++

后端 未结 3 629
离开以前
离开以前 2020-11-30 15:05

C++03 3.6.1.3: The function main shall not be used (3.2) within a program. ...

I wonder why this rule exists... Is anyone awa

3条回答
  •  猫巷女王i
    2020-11-30 15:29

    I'd imagine this preserves an implementation's freedom to prefix main with code to construct globals and statics, accept any parameters identifying the environment and command line arguments and map them to the argc/argv/env conventions of C++, construct an appropriate stack and exception framework for the application to execute etc. Consider that not all environments may allow an executable image to have any other symbol designated as initialisation code to be run before main().

    Similarly, cleanup code may be appended to main(), along with a call to the OS with some mapping from the 0/non-zero convention of C and C++ to the actual success/failure values used by that specific OS.

    Consequently, calling main from elsewhere could attempt a second reinitialisation of the application framework or force an unintended exit to the OS - sounds catastrophic to me.

提交回复
热议问题