首页 > 解决方案 > 允许对易失性对象进行优化

问题描述

来自ISO/IEC 9899:201x5.1.2.3 节程序执行4段:

在抽象机中,所有表达式都按照语义的规定进行评估。如果一个实际的实现可以推断出它的值没有被使用并且没有产生所需的副作用(包括调用函数或访问易失性对象引起的任何副作用),则它不需要评估表达式的一部分。

关于 volatile 对象,这里允许的优化究竟是什么?有人可以举一个可以优化的易失性访问的例子吗?

由于 volatiles 访问是一种可观察的行为(在第6段中描述),似乎没有关于 volatiles 的优化可以采取,所以,我很想知道第4部分允许哪些优化。

标签: coptimizationlanguage-lawyervolatile

解决方案


稍微重新格式化:

An actual implementation need not evaluate part of an expression if:

  a) it can deduce that its value is not used; and

  b) it can deduce that that no needed side effects are produced (including any
     caused by calling a function or accessing a volatile object).

在不改变含义的情况下反转逻辑:

An actual implementation must evaluate part of an expression if:

  a) it can't deduce that its value is not used; or

  b) it can't deduce that that no needed side effects are produced (including
      any caused by calling a function or accessing a volatile object).

简化以关注易失性部分:

An actual implementation must evaluate part of an expression if needed
side effects are produced (including accessing a volatile object).

推荐阅读