Search code examples
c++language-lawyeras-if

Where are the statement of or the foundations for the "as if" rule in the C++ Standard?


After a little google search (for instance, site:eel.is "as if rule") I couldn't find a proper place where the so called "as if" rule is clearly stated in the C++ standard. All I could find is that in those places within the standard where it is invoked, the intro.execution reference is given.

But intro.execution doen't seem to clearly reference any general form of this rule. I'm probably missing something subtle here, but can you point me to the place, or places, where a precise normative understanding of the rule is conveyed? Maybe the whole intro.execution is indeed intended to convey what we call as the "as if" rule in the wild? I confess that I've just skimmed through it.

I'm using this website to navigate through the working draft of the standard.

The definition of the "as if" rule can be found, for instance in the cppreference site, as something to the effect that compilers are allowed to perform transformations on programs as long as some constraints are met, for optimizations reasons, for instance.


Solution

  • The definition for "as if" rule is given in the footnote to 4.1.1/1. See http://eel.is/c++draft/intro.abstract#footnote-4:

    This provision is sometimes called the “as-if” rule, because an implementation is free to disregard any requirement of this document as long as the result is as if the requirement had been obeyed, as far as can be determined from the observable behavior of the program. For instance, an actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no side effects affecting the observable behavior of the program are produced.