gpt4 book ai didi

c++ - 遗漏是未定义的行为吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:21:21 25 4
gpt4 key购买 nike

标准说:

1.3.24                                                          [defns.undefined]undefined behaviorbehavior for which this International Standard imposes no requirements[ Note: Undefined behavior may be expected when this International Standard omits any explicit definition of behavior or when a program uses an erroneous construct or erroneous data. 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 (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message). Many erroneous program constructs do not engender undefined behavior; they are required to be diagnosed.— end note ]

显然,该标准无法涵盖行为的所有可能方面。所以看起来如果标准没有涵盖某些东西,它“可能”是未定义的行为。这到底是什么意思?

这意味着如果标准没有涵盖某些内容,就不能对其施加要求。但是,标准怎么能在没有明确说明的情况下说某些事情是未定义的行为呢?从字面上看,任何事情都可能在程序中发生,除非标准如此规定,否则假定它不是未定义的行为。这是否意味着非标准程序在默认情况下是未定义的行为?


一位回答者说笔记是非规范的。这在 Are notes and examples in the core language specification of the C++ Standard non-normative? 中得到了回答。其中说:

Notes and examples integrated in the text of a document shall only be used for giving additional information intended to assist the understanding or use of the document. They shall not contain requirements ("shall"; see 3.3.1 and Table H.1) or any information considered indispensable for the use of the document, e.g. instructions (imperative; see Table H.1), recommendations ("should"; see 3.3.2 and Table H.2) or permission ("may"; see Table H.3). Notes may be written as a statement of fact.

在我看来,被省略的东西并不是严格意义上的未定义行为。例如,如果注释中提到某事,是否是严格未定义的行为?

最佳答案

However, how can the standard say something is undefined behavior without explicitly saying so?

因为这就是 undefined 的意思。会发生什么还没有定义。该标准定义了一个有效程序的期望值,它不会尝试列出所有可能的无效程序并说“这是未定义的,这也是未定义的,这也是未定义的”。

该标准没有具体说明如果您在程序运行时放火烧计算机会发生什么情况。这并不意味着它定义明确。它显然是未定义的。

Literally anything can happen in a program and it is assumed that it is not undefined behavior unless said so by the standard.

我不确定你在这里想说什么,但听起来倒过来了 180°。

Does this mean a non-standard program is undefined behavior by default?

什么是“非标准程序”?

关于c++ - 遗漏是未定义的行为吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26526426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com