为什么在C ++中不允许调用main() [英] Why calling main() is not allowed in C++

查看:106
本文介绍了为什么在C ++中不允许调用main()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++03 3.6.1.3: 在程序中不得使用main函数(3.2). ...

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

我想知道为什么存在此规则...是否有人知道使用main会出现问题的任何系统/实现?

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.我知道术语used的定义. 2.我知道有一些简单的解决方法,例如从main()调用单个MyMain()并改用MyMain(). 3.问题是关于现实世界中的实现,如果没有限制,出现问题.谢谢!

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!

推荐答案

除了其他答案:c ++规范还保证在调用main之前发生了所有静态初始化.

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

如果代码可以调用main,那么某些静态作用域对象可以调用main,在这种情况下,将违反基本保证.

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

规范不能说静态作用域的对象不应该调用main()",因为许多对象并不是专门为始终在静态作用域进行实例化而编写的.也不能说构造函数不应该调用main(),因为很难审计和证明构造函数没有在调用方法,而是在调用方法(有时可能会调用main()).

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().

这篇关于为什么在C ++中不允许调用main()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆