What exactly is the quot;as-ifquot; rule?(“好像究竟是什么?规则?)
问题描述
正如标题所说:
究竟什么是as-if"规则?
一个典型的答案是:
允许任何和所有不改变程序可观察行为的代码转换的规则
The rule that allows any and all code transformations that do not change the observable behavior of the program
有时,我们会不断从某些实现中获取行为,这些行为归因于该规则.多次出错.
From time to time, we keep getting behaviors from certain implementations, which are attributed to this rule. Many times wrongly.
那么,这条规则到底是什么?标准并没有明确将这条规则作为一个部分或段落提及,那么究竟什么属于该规则的范围?
So, what exactly is this rule? The standard does not clearly mention this rule as a section or paragraph, so what exactly falls under the purview of this rule?
对我来说,这似乎是一个标准没有详细定义的灰色区域.有人可以详细说明细节,引用标准中的参考资料吗?
To me, it seems like a grey area which is not defined in detail by the standard. Can someone elaborate on the details, citing the references from the standard?
注意:将其标记为 C 和 C++,因为它与两种语言都相关.
推荐答案
什么是as-if"规则?
as-if"规则基本上定义了允许实现在合法 C++ 程序上执行的转换.简而言之,所有不影响程序可观察行为"的转换(具体定义见下文)都是允许的.
What is the "as-if" rule?
The "as-if" rule basically defines what transformations an implementation is allowed to perform on a legal C++ program. In short, all transformations that do not affect a program's "observable behavior" (see below for a precise definition) are allowed.
目标是让实现自由执行优化,只要程序的行为仍然符合 C++ 标准在抽象机器方面指定的语义.
The goal is to give implementations freedom to perform optimizations as long as the behavior of the program remains compliant with the semantics specified by the C++ Standard in terms of an abstract machine.
C++11 标准在第 1.9/1 段中引入了as-if"规则:
The C++11 Standard introduces the "as-if" rule in Paragraph 1.9/1:
本国际标准中的语义描述定义了一个参数化的非确定性抽象机器.本国际标准对一致性实现的结构没有要求.特别是,它们不需要复制或模仿抽象机器的结构.而是符合需要实现来模拟(仅)抽象机的可观察行为,如解释下面.
The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.
另外,一个解释性脚注添加:
Also, an explanatory footnote adds:
这个规定有时被称为as-if"规则,因为实现可以自由地忽略这个规定的任何要求国际标准,只要结果符合要求,只要可以从程序的可观察行为.例如,如果可以,实际实现不需要评估表达式的一部分推断它的值没有被使用,并且没有产生影响程序可观察行为的副作用.
This provision is sometimes called the "as-if" rule, because an implementation is free to disregard any requirement of this International Standard as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable behavior of the program. For instance, an actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no side effects affecting the observable behavior of the program are produced.
<小时>
该规则的具体规定是什么?
第 1.9/5 段进一步规定:
What does the rule mandate exactly?
Paragraph 1.9/5 further specifies:
一个一致的实现执行一个格式良好的程序应该产生相同的可观察行为作为具有相同程序的抽象机的相应实例的可能执行之一和相同的输入.但是,如果任何此类执行包含未定义的操作,则此 International标准对使用该输入执行该程序的实现没有任何要求(甚至关于第一个未定义操作之前的操作).
A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible executions of the corresponding instance of the abstract machine with the same program and the same input. However, if any such execution contains an undefined operation, this International Standard places no requirement on the implementation executing that program with that input (not even with regard to operations preceding the first undefined operation).
值得强调的是,该约束仅适用于执行格式良好的程序",并且执行包含未定义行为的程序的可能结果是不受约束的.第 1.9/4 段也明确说明了这一点:
It is worth stressing that this constraint applies when "executing a well-formed program" only, and that the possible outcomes of executing a program which contains undefined behavior are unconstrained. This is made explicit in Paragraph 1.9/4 as well:
本国际标准中将某些其他操作描述为未定义(例如,效果试图修改 const 对象).[注:本国际标准对包含未定义行为的程序的行为.——尾注]
Certain other operations are described in this International Standard as undefined (for example, the effect of attempting to modify a const object). [ Note: This International Standard imposes no requirements on the behavior of programs that contain undefined behavior. —end note ]
最后,关于可观察行为"的定义,第 1.9/8 段如下:
Finally, concerning the definition of "observable behavior", Paragraph 1.9/8 goes as follows:
对一致性实现的最低要求是:
The least requirements on a conforming implementation are:
——对易失性对象的访问严格按照抽象机的规则进行评估.
— Access to volatile objects are evaluated strictly according to the rules of the abstract machine.
——在程序终止时,写入文件的所有数据应与以下可能结果之一相同根据抽象语义执行程序会产生.
— At program termination, all data written into files shall be identical to one of the possible results that execution of the program according to the abstract semantics would have produced.
——交互设备的输入和输出动态应以提示的方式发生输出实际上是在程序等待输入之前交付的.什么构成交互式设备是实现定义的.
— The input and output dynamics of interactive devices shall take place in such a fashion that prompting output is actually delivered before a program waits for input. What constitutes an interactive device is implementation-defined.
这些统称为程序的可观察行为.[ 注意:更严格每个实现都可以定义抽象语义和实际语义之间的对应关系.——结束注意 ]
These collectively are referred to as the observable behavior of the program. [ Note: More stringent correspondences between abstract and actual semantics may be defined by each implementation. —end note ]
<小时>
是否存在此规则不适用的情况?
据我所知,as-if"规则的唯一例外是复制/移动省略,即使复制构造函数、移动构造函数或析构函数的类有副作用.第 12.8/31 段规定了具体条件:
Are there situations where this rule does not apply?
To the best of my knowledge, the only exception to the "as-if" rule is copy/move elision, which is allowed even though the copy constructor, move constructor, or destructor of a class have side effects. The exact conditions for this are specified in Paragraph 12.8/31:
当满足某些条件时,允许实现省略类的复制/移动构造对象,即使为复制/移动操作选择了构造函数和/或对象的析构函数有副作用.[...]
When certain criteria are met, an implementation is allowed to omit the copy/move construction of a class object, even if the constructor selected for the copy/move operation and/or the destructor for the object have side effects. [...]
这篇关于“好像"究竟是什么?规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“好像"究竟是什么?规则?
基础教程推荐
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01