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

試著忘記壹切 提交于 2020-05-15 02:42:04

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/61639551/where-are-the-statement-of-or-the-foundations-for-the-as-if-rule-in-the-c-st

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!