What is an as-is rule?
The “as is” rule basically determines which transformations whose implementation is allowed to be performed on a legitimate C ++ program. In short, all conversions that do not affect the "observable behavior" of the program (see below for a precise definition) are allowed.
The goal is to give implementations the freedom to perform optimizations, while the behavior of the program remains compatible with the semantics defined by the C ++ standard, in terms of an abstract machine.
Where does standard introduce this rule?
The C ++ 11 standard introduces an “as is” rule in clause 1.9 / 1:
The semantic descriptions in this International Standard define a parameterized 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.
In addition, an explanatory footnote adds:
This provision is sometimes called the "as-if" rule , because the implementation is free to ignore any requirements of this International Standard as long as the result is as if the requirement were met, as far as it can be determined from the observed behavior of the program. For example, the actual implementation should not evaluate part of the expression if it can infer that its value is not used and that no side effects affecting the observed behavior of the program are produced.
What specifically sets the rule?
Paragraph 1.9 / 5 further states:
The corresponding implementation , executing a well-formed program, should provide the same observable behavior as one of the possible executions of the corresponding instance of an abstract machine with the same program and the same input . However, if any such execution contains an undefined operation, this International Standard Seats does not require the execution of this program with this input (not even for operations preceding the first undefined operation).
It should be emphasized that this restriction applies when “only a correctly formed program is executed” and that the possible results of executing a program containing undefined behavior are not limited. This is also clearly indicated in paragraph 1.9 / 4:
Some other operations are described in this International Standard as undefined (for example, the effect of trying to modify a const object). [Note: This International Standard does not establish any requirements for the behavior of programs containing undefined behavior . -end note]
Finally, with regard to the definition of “observed behavior”, paragraph 1.9 / 8 is as follows:
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 . [Note: stricter Correspondence between abstract and actual semantics can be defined by each implementation. end note]
Are there situations where this rule does not apply?
As far as I know, the only exception to the as-if rule is copy / move elision, which is allowed, although the copy constructor, move constructor, or class destructor have side effects. The exact conditions for this are specified in paragraph 12.8 / 31:
When certain criteria are met, the implementation allows you to omit the copy / move construct of the object class , even if the constructor selected for the copy / move operation and / or the destructor for the object has side effects . [...]