为什么 void main() 没有标准化为 main 函数的有效签名?

发布于 2025-01-08 16:15:09 字数 499 浏览 1 评论 0原文

为什么在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

老娘不死你永远是小三 2025-01-15 16:15:09

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 a return statement to return a specific exit code to the runtime system. It would be unreasonable to expect all existing code to change to use exit() instead, so int 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 of System.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 use return and an exit code value from main().

離殇 2025-01-15 16:15:09

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 the void keyword defined the two standard definitions for main: int main(void) and int 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 than int main(). You don't even need to explicitly return a value; falling off the end of main is equivalent to return 0; (in C++, and in C starting with C99).

稚然 2025-01-15 16:15:09

您通常想知道程序的退出状态。这就是为什么你有 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.

送舟行 2025-01-15 16:15:09

这是错误的,因为这不是 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 says int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文