What exactly is the “as-if” rule?

后端 未结 3 2152
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 06:13

As the title says,

What exactly is the \"as-if\" rule?

An typical answer one would get is:

<
3条回答
  •  离开以前
    2020-11-21 06:52

    In C11 the rule is never called by that name. However C, just like C++, defines the behaviour in terms of abstract machine. The as-if rule is in C11 5.1.2.3p4 and p6:

    1. In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

    2. [...]

    3. The least requirements on a conforming implementation are:

      • Accesses 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 the result that execution of the program according to the abstract semantics would have produced.
      • The input and output dynamics of interactive devices shall take place as specified in 7.21.3. The intent of these requirements is that unbuffered or line-buffered output appear as soon as possible, to ensure that prompting messages actually appear prior to a program waiting for input.

       

      This is the observable behavior of the program.

提交回复
热议问题