其实,c++ 标准对于具体编译器实现的限制还是比较宽松的。c++ 标准在其语义逻辑基础上定义一个“抽象机器”,这个抽象机器保证对 c++ 程序的语义逻辑作出相应的操作。c++ 编译器实现只要在其实现环境上保证其产生的执行程序的行为“看起来好像”与这个抽象机器的行为一致就可以了。这一节指出了实现与虚拟机器的关系,怎么判定一个实现和标准是否符合,副作用和顺序点的概念。
1.9 program execution [intro.execution]
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.i)
在本标准的语义描述中,定义了一个参数化的,非确定的抽象机器。本标准不对符合标准的实现的内部结构设置任何规格。实现尤其不需要复制或模仿这个抽象机器的结构,需要模仿的(仅仅)是抽象机器的可观察的行为。抽象机器在下面阐述。i)
i) 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.
这个条款往往被称为“好像”原则,因为只要实现的结果通过考察程序的可观察行为可被认为好像是遵从了本标准的规格时,实现就可以自由忽略任何标准规格。例如,如果某个具体实现可以推导出表达式中的某部分的值不会被用到,并且这一部分表达式的副作用不会对程序产生的可观察行为造成影响时,这个实现就无需对这部分表达式进行求值。
certain aspects and operations of the abstract machine are described in this international standard as implementation-defined (for example, sizeof(int)). these constitute the parameters of the abstract machine. each implementation shall include documentation describing its characteristics and behavior in these respects. such documentation shall define the instance of the abstract machine that corresponds to that implementation (referred to as the “corresponding instance" below).
本标准将虚拟机器的某些方面和操作描述为实现定义的(如 sizeof(int))。这些就形成了虚拟机器的实现参数。每个实现都应该对它们的具体特性和行为提供文档描述。实现的文档中应当定义对应与该实现的虚拟机器的实例(“对应实例”描述如下)。
certain other aspects and operations of the abstract machine are described in this international standard as unspecified (for example, order of evaluation of arguments to a function). where possible, this international standard defines a set of allowable behaviors. these define the nondeterministic aspects of the abstract machine. an instance of the abstract machine can thus have more than one possible execution sequence for a given program and a given input.
本标准将虚拟机器的其他一些方面和操作描述为未指明的(如函数参数的求值顺序)。本标准在可能的情况下定义一组可接受的行为。由此定义了抽象机器的非确定性的方面。对于一个已知程序和已知数据,虚拟机器的实例将因其非确定方面产生不只一个可能的执行序列。
certain other operations are described in this international standard as undefined (for example, the effect of dereferencing the null pointer). [note: this international standard imposes no requirements on the behavior of programs that contain undefined behavior. ]
其他操作被本标准称为未定义的(如对空指针解进行引用的后果)。【注:本标准对包含未定义行为的程序的行为不作任何规范。】
a conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible execution sequences of the corresponding instance of the abstract machine with the same program and the same input. however, if any such execution sequence 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).
当一个符合标准的实现执行一个完备的程序时,其产生的可观察行为应该与该实现的抽象机器对应实例依据同一程序和同一输出产生的可能执行序列之一相同。尽管如此,如果在这个执行序列中包含未定义行为,本标准仍不会对实现以这个输入数据的这个程序的执行(不包括对第一个未定义操作之前的操作)作任何规范。
the observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library i/o functions.ii)
抽象机器的可观察行为是其对 volatile 数据的读写和对库 i/o 函数的调用的序列。ii)
ii) an implementation can offer additional library i/o functions as an extension. implementation that do so should treat calls to those functions as “observable behavior” as well.
实现可以提供额外的 i/o 库函数作为扩展,实现应该把对这些函数的调用同样看作“可观察行为”。
accessing an object designated by a volatile lvalue (3.10), modifying an object, calling a library i/o function, or calling a function that does any of those operations are all side effects, which are changes in the state of the execution environment. evaluation of an expression might produce side effects. at certain specified points in the execution sequence called sequence points, all side effects of previous evaluations shall be complete and no side effects of subsequent evaluations shall have taken place.iii)