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

后端 未结 3 628
离开以前
离开以前 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条回答
  •  生来不讨喜
    2020-11-30 15:33

    In addition to the other answers: The c++ spec guarantees that all static initialization has happened before main is called.

    If code could call main then some static scoped object could call main, in which case a fundamental guarantee is violated.

    The spec can't say "static scoped objects should not call main()" because many objects are not written specifically to be instantiated at static scope always. It also can't say that constructors should not call main() - because its very difficult to audit and prove that a constructor isn't calling a method, calling a method, that might sometimes, call main().

提交回复
热议问题