加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 空表达式是否评估为NOP?

发布时间:2020-12-16 09:42:24 所属栏目:百科 来源:网络整理
导读:想知道空表达式是否评估为NOP或者它是否依赖于编译器. // Trivial exampleint main(){ ;;} 解决方法 它依赖于编译器,但可观察的行为必须是没有任何反应.在实践中,我确信大多数编译器都会省略任何代码都没有空表达式. A conforming implementation executing
想知道空表达式是否评估为NOP或者它是否依赖于编译器.

// Trivial example
int main()
{
    ;;
}

解决方法

它依赖于编译器,但可观察的行为必须是没有任何反应.在实践中,我确信大多数编译器都会省略任何代码都没有空表达式.

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");

但是,这是有条件支持的,其行为是实现定义的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读