Understanding the as-if rule, "the program was executed as written"

C++Compiler Optimization

C++ Problem Overview


I am trying to understand the as-if rule. According to cppreference:

> The as-if rule
> Allows any and all code transformations that do not change the observable behavior of the program

> Explanation
> The C++ compiler is permitted to perform any changes to the program as long as the following remains true: [...]

It is hard for me to understand the second tip of the Explanation section:

> 2) At program termination, data written to files is exactly as if the program was executed as written.

I just don't understand what "the program was executed as written" means.

C++ Solutions


Solution 1 - C++

On Monday your boss comes into your office and says "I need file A on my desk by Thursday and file B on my desk on Friday". He first describes the things that he wants in file A and how he thinks you should do those and then describes the things he wants in file B.

In the mind of your boss, you will first do the things for file A, place that file on his desk on Thursday, then get to work on file B and finish that on Friday. But you realize that it would make more sense to start work on file B earlier - before file A even. There's no reason your boss has to know - all he cares about is receiving A on Thursday and B on Friday. You also realize that the way he suggested can be improved, so you take a slightly different approach to producing the required information.

In this analogy, the boss is some C++ code and you are the compiler. It is legal for the compiler to rearrange operations (work on the files in another order) as long as the observable behavior (putting files on the desk of the boss) is the same. Similarly, the compiler is free to do any transformations (using a different approach than the one described by the boss) on the code that preserve the observable behavior.

In particular, "as if the program was executed as written" means "as if you did the work exactly as your boss instructed you to" (even if you did something different).

Solution 2 - C++

An important feature of the cited rule is that it specifies a minimal set of requirements for an implementation to be conforming, but in no way implies that those requirements will be sufficient for any particular application, nor that some applications won't need implementations to offer stronger guarantees. Suppose the procedure for performing and recording the results of a single test would be:

  1. Do the experiment.
  2. Write down the measured result.
  3. Unlock the safe
  4. Put the paper with the measurements in the safe.
  5. Lock the safe.

If one is given three tests to perform, one could perform the above five steps in order for each test, but either of the following sequences of steps might also be acceptable:

  1. Do experiment #1
  2. Write down the measured result on paper #1
  3. Do experiment #2
  4. Write down the measured result on paper #2
  5. Do experiment #3
  6. Write down the measured result on paper #3
  7. Unlock the safe
  8. Put paper #1 in the safe
  9. Put paper #2 in the safe
  10. Put paper #3 in the safe
  11. Lock the safe

or--to avoid having to keep track of three papers at once:

  1. Do experiment #1
  2. Write down the measured result on paper #1
  3. Unlock the safe
  4. Put paper #1 in the safe
  5. Do experiment #2
  6. Write down the measured result on paper #2
  7. Put paper #2 in the safe
  8. Do experiment #3
  9. Write down the measured result on paper #3
  10. Put paper #3 in the safe
  11. Lock the safe

If everything goes as expected, all three approaches would be equivalent. If, however, the second experiment could go awry and trash any papers that are lying on the desk, using the second approach would risk losing the results of the first experiment--an outcome that would not have occurred had the fully-detailed procedure been followed. Worse, if the third experiment goes really awry and trashes everything that isn't locked in the safe, the third approach would risk losing everything that was in the safe, even content unrelated to the experiments.

In some cases the second or third approach may be appropriate. In some, it wouldn't. Judging whether those approaches are appropriate would require knowledge of the risks posed by the experiments, the contents of the safe, and many other factors.

The authors of the Standard can't know everything necessary to judge what guarantees will be needed by what applications. Instead, they rely upon the producers and users of various implementations to recognize what guarantees will be needed to safely and effectively accomplish whatever needs to be done.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpoohRuiView Question on Stackoverflow
Solution 1 - C++Max LanghofView Answer on Stackoverflow
Solution 2 - C++supercatView Answer on Stackoverflow