C++ 中未定义行为段落中的 [Note] 是什么意思?标准是什么意思?

发布于 2024-10-04 07:29:18 字数 490 浏览 3 评论 0原文

作为用户 Tony 指出 C++标准第1.3.12段中有一个[注意]

允许的未定义行为的范围从完全忽略具有不可预测结果的情况,到在翻译或程序执行期间以环境特有的记录方式进行行为......

这是否与 UB 的定义相矛盾,说 .. .这个国际标准没有提出任何要求?我的意思是他们说“没有要求”,然后在同一段中说“允许的 UB”。

这条注释应该如何解读?它确实以某种方式限制了 UB 吗?

As user Tony points out there's a [Note] in paragraph 1.3.12 of C++ standard saying

permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment...

Doesn't this contradict the definition of UB saying that ...this International Standard imposes no requirements? I mean they say "no requirements" and then say "permissible UB" - right in the same paragraph.

How should this note be interpreted? Does it indeed limit UB in any way?

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

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

发布评论

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

评论(3

把人绕傻吧 2024-10-11 07:29:18

ISO/IEC 指令第 3 部分的 §6.5.1 中:

注释和示例集成在
只能使用标准文本
用于提供附加信息
旨在帮助理解
或使用该标准,并且不得
包含其所适用的条款
必须符合才能
能够声称遵守
标准。

因此,它完全是非规范性的(不具有约束力),仅用于可能的澄清。

From §6.5.1 of Part 3 of the ISO/IEC Directives:

Notes and examples integrated in the
text of a standard shall only be used
for giving additional information
intended to assist the understanding
or use of the standard and shall not
contain provisions to which it is
necessary to conform in order to be
able to claim compliance with the
standard.

So it's entirely non-normative (non-binding) and meant only for possible clarification.

佞臣 2024-10-11 07:29:18

由于注释不是规范性的,因此它不会以任何方式限制 UB。这只是一个澄清,即实现可以使用一些正式导致 UB 作为记录扩展的构造,尽管任何依赖于此类细节的程序本质上都不能安全地移植到其他环境。

As notes are not normative it doesn't limit UB in any way. It's just a clarification that an implementation could use some constructs that formally cause UB as a documented extension, although any program that relies on such a detail is, of course, inherently not safely portable to other environments.

金橙橙 2024-10-11 07:29:18

本注释解释了如果实现遇到没有定义行为的代码,它可能会做什么。 “允许”一词并不意味着限制,而是给出了一些常见行为的示例。

有趣的是,编译器几乎总是必须编译一些东西!考虑一下这段代码:

void f() { 1 / 0; }

翻译器在遇到这种情况时的行为没有明确定义,但它不能只是做任何它喜欢的事情!事实上如果是编译器的话还是需要编译这个编译单元。那是因为包含此函数的程序的行为仍然可以很好地定义!编译器无法知道该函数是否被调用。事实上,这个问题出现在函数是“main()”的地方,并且控制肯定会流经零除法,结果是编译器甚至不允许拒绝该程序。原因是:程序仍然是格式良好的,并且需要符合标准的编译器来接受所有格式良好的程序(并拒绝所有格式错误的程序并发出诊断错误消息,除非另有说明)。

这不容易变得格式错误,因为很难准确地指定需要如何智能的编译器来检测何时必须发生除以零的情况。

有趣的是,该标准声称“不强加任何要求”实际上非常接近错误。支持单独编译的编译系统的特点是,它无法检测一段没有明确定义行为的代码是否确实被执行,因此编译器实际上需要无论如何都要编译一些东西,因为它无法推断程序是否具有未定义的行为。

This note is explaining what an implementation might do if it encounters code for which there is no defined behaviour. The word "permissible" is not intended to be a restriction, rather some examples of common behaviours are given.

It is interesting to note that a compiler almost always HAS to compile something! Consider this fragment of code:

void f() { 1 / 0; }

the behaviour of the translator on encountering this is not well defined, but it can't just do anything it likes! In fact if it is a compiler it is still required to compile this compilation unit. That is because the behaviour of a program containing this function could still be well defined! The compiler cannot know if the function is called. In fact this question arose where the function was "main()" and control was certain to flow through the zero division, and the upshot is that the compiler is not allowed to reject even that program. The reason is: the program is still well formed, and a conforming compiler is required to accept all well formed programs (and reject all ill-formed ones and issue a diagnostic error message, unless otherwise specified).

This can't easily be made ill-formed because it is hard to specify exactly how smart compilers can be required to be in detecting when a division by zero must take place.

So interestingly, the claim of the Standard that it "Imposes no requirements" is in fact very close to being wrong. It is characteristics of a compilation system supporting separate compilation that it cannot detect if a piece of code for which there is no well defined behaviour is, in fact, executed, and so the compiler is in fact required to compile something anyhow, because it cannot deduce if the program has undefined behaviour.

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