Why are recursive main() calls not allowed in C++? [duplicate]

纵然是瞬间 提交于 2019-11-30 03:53:20

问题


Possible Duplicates:
restrictions on the main() function
Is it legal to recurse into main() in C++?

I read in C++ Primer that main is not allowed to be called recursively, and in some related questions here on SO it is indeed confirmed that it is illegal.

But why is it illegal? As long as you avoid a stack overflow, what's the problem with calling main within itself?


回答1:


Well, the standard states:

3.6.1.3 "The function main shall not be used within a program."

5.2.2.9 "Recursive calls are permitted, except to the function named main"

I guess it is beause main() is a special function used as the entry point to the program. I'd say keep it special, don't take it down the level of a normal function, because it is not.




回答2:


I believe that the wording in 3.6.1/3 forbids this, saying it shall not be used in a program:

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 illformed. The name main is not otherwise reserved. [Example: member functions, classes, and enumerations can be called main, as can entities in other namespaces. ]

Then in 3.2/2

An object or nonoverloaded function is used if its name appears in a potentially evaluated expression.

This clearly indicates that used includes possible calls (which would be recursive) to main.



来源:https://stackoverflow.com/questions/5326491/why-are-recursive-main-calls-not-allowed-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!