Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 831
Tarc Avatar asked May 06 '20 15:05

Tarc


2 Answers

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.

like image 83
SergeyA Avatar answered Oct 06 '22 15:10

SergeyA


From a different Draft Standard, it would appear that the section you refer to in your link actually is the "as-if" rule (see the footnote #5):

1.9 Program execution [intro.execution]

The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below.5.
...
5) This provision is sometimes called the “as-if” rule, because an implementation is free to disregard any requirement of this International Standard 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.

PS: One thing I found 'interesting' in this quoted document is the index entry for "as-if rule":

program execution, 8–11
    abstract machine, 8
    as-if rule, see as-if rule
like image 28
Adrian Mole Avatar answered Oct 06 '22 16:10

Adrian Mole