到底是什么"如假设"规则? [英] What exactly is the "as-if" rule?

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

问题描述

正如标题所说,

到底是什么为假设治呢?

What exactly is the "as-if" rule?

这是典型的回答人会得到的是:

An typical answer one would get is:

这允许任何及所有code转换不改变程序的可观察行为规则

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 a 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-如果的规则基本上界定了转型的实现,则允许在一个合法的C ++程序执行。总之,不影响程序的所有转变的观察到的行为的(见下文为precise定义)是允许的。

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标准引入了 AS-如果的在规则段1.9 / 1:

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:

此规定有时被称为为假设规则后,因为一个实现可以自由地忽略这个任何要求
  国际标准,只要结果是,如果要求已服从,只要可以从所确定的
  该方案可观察的行为。例如,一个实际的实施可以不需要计算一个前pression的一部分,如果它可以
  推断出它的值不使用,并且没有副作用影响该程序的可观察行为产生

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:

一个符合标准的实现 执行一个结构良好的程序的应产生相同的观察行为
  作为抽象机的相应实例的具有相同的程序的可能的执行的一个
  和相同的输入
。但是,如果任何这样执行包含未定义操作,该国际
  规范的地方没有要求上执行该程序与输入实现(甚至没有
  关于操作preceding第一个未定义操作)。

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-如果的规则复制/移动省音,这是允许的,即使拷贝构造函数,移动构造函数或析构函数的类有副作用。造成这种情况的确切条件在指定款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天全站免登陆