对main()函数的限制 [英] restrictions on the main() function
问题描述
C ++ 03 3.6.1.3
: 函数main不能在程序中使用(3.2)。 ...
我不知道为什么这个规则存在...有人知道任何系统/主要使用?
我知道术语 used的定义
。我知道有一些简单的解决方法,如调用一个单一的MyMain()从main()和使用MyMain()。问题是关于现实世界的实现,如果不存在限制,则会有问题。谢谢!
除了其他答案:c ++规范保证所有静态初始化都发生在main之前。 / p>
如果代码可以调用main,那么一些静态作用域对象可以调用main,在这种情况下违反基本的保证。
规范不能说静态作用域对象不应调用main(),因为许多对象不是专门写在静态范围总是被实例化。它也不能说构造函数不应该调用main() - 因为它非常难以审计,并证明一个构造函数不是调用一个方法,调用一个方法,有时可能调用main()。
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 aware of any system/implementation where it would be a problem if main were used?
P.S. 1. I know the definition of the term used
. 2. I know there are simple workarounds like calling a single MyMain() from main() and using MyMain() instead. 3. The question is about real-world implementations which would have a problem if the restriction weren't there. Thanks!
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().
这篇关于对main()函数的限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!