为什么 void main() 没有标准化为 main 函数的有效签名?
为什么在 C++ 中将入口点的返回类型设置为 void
总是不被鼓励,并且后来被标准删除并被现代编译器禁止?为什么它被认为是不好的做法?
现在,据我了解,C# 和 Java 都允许入口点的返回类型为 void,即
static void main(String[] args) /* Java */
static void Main(string[] args) /* C# */
C# 和 Java 程序员并不认为这是不好的做法,事实上他们经常使用它。
其他语言(只是打算,我怀疑 C++ 至少会在这十年内取得成功)可能是 C++ 的继承者,例如 D 编程语言或 Vala 也允许 void main( )
。正如您所看到的,我怀疑 C++ 社区将其从标准中删除,因为它太晦涩或不受欢迎。
所以我的问题是,为什么 C++ 社区删除了 void main()
?有什么问题吗?
Why has setting the entry point's return type to void
in C++ always been discouraged, and was later removed by the standard and is prohibited by modern compilers? Why is it considered bad practice?
Now, as I understand C# and Java both allow the entry point's return type to be void
i.e
static void main(String[] args) /* Java */
static void Main(string[] args) /* C# */
And C# and Java programmers do not consider it bad practice, they use it often in fact.
Other languages which are (only intended to be, I doubt C++ will be succeeded in this decade, at least) possible successors of C++ like the D Programming Language or Vala also allow a void main()
. So as you can see, I doubt the C++ community removed it from the standard because it was too obscure or unpopular.
So my question is, Why did the C++ Community Remove void main()
? What was wrong with it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C++ 标准委员会可能选择要求
int main()
,因为大量现有代码期望使用return
语句将特定的退出代码返回到运行时系统。期望所有现有代码都改为使用exit()
是不合理的,因此标准中提出了int main()
的要求。像 Java 这样的语言在设计时并没有任何需要保持兼容的现有代码体。因此,设计者可以选择
void main()
并要求使用System.exit()
来实现非零退出代码。因此,为 C++ 标准选择
void main()
的“错误”在于,它会破坏预期使用return
和 exit 的现有代码来自main()
的代码值。The C++ standards committee likely chose to require
int main()
because of the large body of existing code that expected to use areturn
statement to return a specific exit code to the runtime system. It would be unreasonable to expect all existing code to change to useexit()
instead, soint main()
was made a requirement in the standard.A language such as Java, when it was designed, did not have any body of existing code that it needed to remain compatible with. Therefore, the designers could choose
void main()
and require the use ofSystem.exit()
for non-zero exit codes.So, the thing that would be "wrong" with choosing
void main()
for the C++ standard would be that it would break existing code that expected to usereturn
and an exit code value frommain()
.C++从未允许
void main()
,尽管某些编译器可能允许它作为扩展或仅仅因为它们不诊断它。类似地,C 从来不允许
void main()
除了作为扩展;引入void
关键字的同一 1989 年标准定义了main
的两个标准定义:int main(void)
和int main( int argc, char *argv[])
。其他语言允许这样做,因为它们是其他语言。
能够编写
void main()
而不是int main()
并没有什么特别的优势。您甚至不需要显式返回值;从main
末尾脱落相当于return 0;
(在 C++ 中,以及在从 C99 开始的 C 中)。C++ has never permitted
void main()
, though some compilers might permit it either as an extension or just because they don't diagnose it.Similarly C has never permitted
void main()
other than as an extension; the same 1989 standard that introduced thevoid
keyword defined the two standard definitions formain
:int main(void)
andint main(int argc, char *argv[])
.Other languages permit it because, well, they're other languages.
There is no particular advantage in being able to write
void main()
rather thanint main()
. You don't even need to explicitly return a value; falling off the end ofmain
is equivalent toreturn 0;
(in C++, and in C starting with C99).您通常想知道程序的退出状态。这就是为什么你有
int main()
的原因——你返回退出状态。You generally want to know the exit status of your program. That's the reason why you have the
int main()
-- you return your exit status.这是错误的,因为这不是 C++ 标准指定的合法
main
。没有人关心其他语言指定的内容。对于 C++ 程序,只有 C++ 标准相关,它表示int
。It's wrong because this is not what the C++ Standard specifies as a legal
main
. Nobody cares about what the other languages specify. For C++ programs, only the C++ Standard is relevant, and it saysint
.