它依赖于编译器,但可观察的行为必须是没有任何反应.在实践中,我确信大多数编译器都会省略任何代码都没有空表达式.
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.
并且可观察的行为由以下定义:
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.
这实际上是实现的唯一要求.它通常被称为“as-if”规则 – 只要可观察行为符合预期,编译器就可以做任何喜欢的事情.
对于它的价值,这些空表达式称为空语句:
An expression statement with the expression missing is called a null statement.
如果你真的想要一个NOP,你可以尝试:
asm("nop");
但是,这是有条件支持的,其行为是实现定义的.