Menu
Home Explore People Places Arts History Plants & Animals Science Life & Culture Technology
On this page
As-if rule
In programming language standards, the interpretive principle that allows compilers to apply any optimizing transformation to a program, as long as it doesn’t change the observable behavior of the program as specified in the standard

For the programming language C++, the standard allows compilers to apply any optimizing transformation to a program during compiling, provided that such optimization makes no change in the "observable behavior" of the program, as specified in the standard. This mostly means that any actions the program performs on its environment occur in the specified order. This rule is commonly referred to as the as-if rule.

The rule has three main exceptions in which behavior-changing optimizations are permitted. The first is that programs exhibiting undefined behavior are exempt; since the observable behavior is not well-defined anyway, any transformation is valid. The other two exceptions concern the copying of objects, and are called copy elision and the return value optimization.

The effect of the as-if rule depends on the specific compiler implementation. As an example, in the Microsoft Visual C++ compiler, it causes omission of certain optimizations such as instruction reordering around calls to library functions, since such calls may cause input/output actions or accesses to memory locations marked volatile, and changes in the order of those change observable behavior.

The as-if rule is not specific to C++. Many other programming languages, including C, Go, OCaml, Rust, etc. have a similar rule, to permit optimization (notably inlining).

We don't have any images related to As-if rule yet.
We don't have any YouTube videos related to As-if rule yet.
We don't have any PDF documents related to As-if rule yet.
We don't have any Books related to As-if rule yet.
We don't have any archived web articles related to As-if rule yet.

References

  1. Godbolt, Matt (September 29, 2017). What Has My Compiler Done for Me Lately? Unbolting the Compiler's Lid. CppCon2017. https://cppcon2017.sched.com/event/BguG/unbolting-the-compilers-lid-what-has-my-compiler-done-for-me-lately

  2. As-if rule on Cppreference, retrieved in April 2018 http://en.cppreference.com/w/cpp/language/as_if

  3. Brais, Hadi (May 2015). Compilers: What Every Programmer Should Know About Compiler Optimizations, Part 2. MSDN Magazine (Report). Microsoft. https://msdn.microsoft.com/en-us/magazine/dn973015.aspx