Thus, according to the C ++ standard, the compiler eliminates mutable int c?
No. volatile -qualified objects are used to read or write to hardware, and there are side effects of assigning a volatile object.
Therefore, in accordance with the restrictions on optimization, placed in the so-called “,”, in accordance with the implementations, it is not allowed to optimize c , the “As is” rule is officially introduced in paragraph 1.9 / 1 of the C ++ 11 standard:
The semantic descriptions in this International Standard define a parametrized non-deterministic abstract machine. There are no requirements for the structure of the corresponding implementations in this International Standard. In particular, they do not need to copy or emulate the structure of an abstract machine. Rather, appropriate implementations are needed to emulate (only) the observed behavior of an abstract machine, as explained below
And the concept of “observable behavior” is defined in paragraph 1.9 / 8:
Least requirements for the corresponding implementation:
- Access to unstable objects is evaluated strictly in accordance with the rules of an abstract machine .
- At the end of the program, all the data written to the files should be identical to one of the possible results that the program execution in accordance with abstract semantics.
- The dynamics of the input and output of interactive devices should take place in such a way as to induce the output is actually delivered before the program waits for input. What is an interactive device is determined by the implementation.
These aggregates are called observable program behavior . [...]
Since access to volatile objects must be evaluated strictly in accordance with the rules of an abstract machine, compilers are not allowed to optimize off c and the corresponding assignment operation.