为什么,如果 c++标准说语法不正确,g++ 确实如此吗?允许吗?

发布于 2024-10-27 14:49:16 字数 359 浏览 6 评论 0原文

我刚刚读到一条评论,内容类似于:

“你永远不应该使用 void main(),你应该始终使用 int main()。”

现在我知道使用 int main() 的原因(以便您可以检查返回是否成功等等),但我不知道使用 void main()是非法的。我做了一些调查,我发现不使用 void main() 的唯一原因是因为“标准是这样说的”。

我的问题是: 如果 C++ 标准规定 main 必须返回一个值,为什么 g++ 允许程序员使用 void main() 作为有效语法?它不应该返回错误/警告吗,因为它违反了标准的规定?

I just read a comment that said something along the likes of:

"You should never use void main() you should always use int main()."

Now I know the reasons for using int main() (so that you can check for success on return and whatnot) but I didn't know that using void main() was illegal. I did some investigating and the only reason I could find not use void main() is because the "standard says so".

My question is:
Why, if the C++ standard says that main must return a value, does g++ allow programmers to use void main() as valid syntax? Shouldn't it return an error / warning because it goes against what the standard says?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

回忆追雨的时光 2024-11-03 14:49:16

这仅意味着您的编译器的特定版本可能允许它,但更高版本(可能更符合标准)可能不允许它。因此,最好从一开始就编写符合标准的代码!

That only means that a particular version of your compiler may allow it, but the later versions (which is likely to be more Standard conformant) may not allow it. So better write Standard Conformant code from the beginning!

勿忘心安 2024-11-03 14:49:16

按照标准,main确实需要返回int。但许多编译器允许 void 返回类型,因为在标准之前的 C++ 中它是允许的,并且很长一段时间以来,许多代码都是使用 void 返回类型编写的。

还值得一提的是,C++ 明确允许省略 void:

int main() {
}

will return 0 的 return 语句。但这仅适用于 main。

According to the standard, main is indeed required to return int. But many compilers allow a return type of void since in pre-standard C++ it was allowed, and for a long time much code was written with a return type of void.

It is also worth to mention that C++ explicitly allows obission of the return statement for void:

int main() {
}

will return 0. But that is only allowed for main.

不离久伴 2024-11-03 14:49:16

您可以使用以下构建命令强制编译器符合标准:

-ansi -pedantic -Wall

如果您不编写跨平台代码,那么 -c99 可能是更好的选择。并非所有编译器都支持这一点。

You can force the compiler to be standards compliment by using the following the build commands:

-ansi -pedantic -Wall

If you are not coding cross-platform code then -c99 might be a better choice. Not all compilers support that.

仅此而已 2024-11-03 14:49:16

GNU 项目有一个对其哲学的体面总结

在大多数情况下,遵循已发布的标准对用户来说很方便,这意味着他们的程序或脚本将更加可移植地运行。 ...

但是我们并不严格遵循这些规范,并且有一些特定的点我们决定不遵循它们,以便使 GNU 系统更好地为用户服务。

例如,标准 C 规定几乎所有对 C 的扩展都是禁止的。多么愚蠢啊! GCC 实现了许多扩展,其中一些扩展后来被采纳为标准的一部分。如果您希望这些构造按照标准“要求”给出错误消息,则必须指定 --pedantic,它的实现只是为了让我们可以说“GCC 是 100% 的实现”标准,”并不是因为有任何理由实际使用它。

POSIX.2 指定 dfdu 必须默认以 512 字节为单位输出大小。用户想要的是1k单位,所以我们默认就是这样做的。如果您想要 POSIX“要求”的荒谬行为,则必须设置环境变量 POSIXLY_CORRECT(最初命名为 POSIX_ME_HARDER)。 ...

特别是,不要仅仅因为标准说“禁止”或“弃用”而拒绝新功能或删除旧功能。

有时,当扩展引起像这样的混乱时,GCC 会删除它们。我相信这个扩展的存在是为了允许具有不正确 main 声明的旧代码进行编译,而不一定是鼓励人们编写 void main()。类似于允许 POSIX 之前的函数声明的扩展。此外,虽然 int main(int argc, const char** argv) 是 C 认可的 main 声明,但 C++ 标准也认可 int main(),以及 POSIX 制裁 int main(int argc, const char** argv, const char** envp)。很可能还有其他我还没有遇到的声明。

The GNU Project has a decent summary of their philosophy:

In most cases, following published standards is convenient for users—it means that their programs or scripts will work more portably. ...

But we do not follow either of these specifications rigidly, and there are specific points on which we decided not to follow them, so as to make the GNU system better for users.

For instance, Standard C says that nearly all extensions to C are prohibited. How silly! GCC implements many extensions, some of which were later adopted as part of the standard. If you want these constructs to give an error message as "required" by the standard, you must specify --pedantic, which was implemented only so that we can say "GCC is a 100% implementation of the standard," not because there is any reason to actually use it.

POSIX.2 specifies that df and du must output sizes by default in units of 512 bytes. What users want is units of 1k, so that is what we do by default. If you want the ridiculous behavior "required" by POSIX, you must set the environment variable POSIXLY_CORRECT (which was originally going to be named POSIX_ME_HARDER). ...

In particular, don’t reject a new feature, or remove an old one, merely because a standard says it is "forbidden" or "deprecated."

Sometimes, GCC has removed extensions when they caused confusion like this one. I believe this extension existed to allow old code with an incorrect main declaration to compile, not necessarily to encourage people writing void main(). Similar to the extension that allowed pre-POSIX function declarations. Besides, while int main(int argc, const char** argv) is the C-approved declaration for main, the C++ standard also sanctions int main(), and POSIX sanctions int main(int argc, const char** argv, const char** envp). There may well be other declarations that I haven't run into yet.

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