“好像"到底是什么?规则? [英] What exactly is the "as-if" rule?

查看:30
本文介绍了“好像"到底是什么?规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说:

究竟什么是as-if"?规则?

人们会得到的典型答案是:

A typical answer one would get is:

允许任何和所有不改变程序可观察行为的代码转换的规则

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:

符合要求的实现执行格式良好的程序应产生相同的可观察行为作为具有相同程序的抽象机的相应实例的可能执行之一和相同的输入.但是,如果任何此类执行包含未定义的操作,则此国际标准对使用该输入执行该程序的实现没有要求(甚至不要求关于第一个未定义操作之前的操作).

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:

——对 volatile 对象的访问严格按照抽象机的规则进行评估.

— 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. [...]

这篇关于“好像"到底是什么?规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆