允许对易失性对象进行优化

use*_*550 4 c optimization volatile language-lawyer

根据ISO / IEC 9899:201x5.1.2.3节,程序执行4段:

在抽象机中,所有表达式均按语义指定的方式求值。如果实际实现可以推断出未使用表达式的值并且没有产生所需的副作用包括由调用函数或访问易失性对象引起的副作用),则无需评估表达式的一部分

What exactly is the allowed optimization here regarding the volatile object? can someone give an example of a volatile access that CAN be optimized away?

Since volatiles access are an observable behaviour (described in paragraph 6) it seems that no optimization can take please regarding volatiles, so, I'm curious to know what optimization is allowed in section 4.

Bre*_*dan 7

重新格式化:

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).
Run Code Online (Sandbox Code Playgroud)

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

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).
Run Code Online (Sandbox Code Playgroud)

简化以关注易变部分:

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