Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/content/docs/cpp/language/basic_concepts/as_if.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ _Observable behavior_ of a program includes the following:

Because the compiler is (usually) unable to analyze the code of an external library to determine whether it does or does not perform I/O or volatile access, third-party library calls also aren't affected by optimization. However, standard library calls may be replaced by other calls, eliminated, or added to the program during optimization. Statically-linked third-party library code may be subject to link-time optimization.

Programs with undefined behavior often change observable behavior when recompiled with different optimization settings. For example, if a test for signed integer overflow relies on the result of that overflow, e.g. `if (n + 1 < n) abort();`, [it is removed entirely by some compilers](#https://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html) because <DocLink dest="/cpp/language/expressions/operator_arithmetic" section="overflows">signed overflow is undefined behavior</DocLink> and the optimizer is free to assume it never happens and the test is redundant.
Programs with undefined behavior often change observable behavior when recompiled with different optimization settings. For example, if a test for signed integer overflow relies on the result of that overflow, e.g. `if (n + 1 < n) abort();`, [it is removed entirely by some compilers](https://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html) because <DocLink dest="/cpp/language/expressions/operator_arithmetic" section="overflows">signed overflow is undefined behavior</DocLink> and the optimizer is free to assume it never happens and the test is redundant.

<DocLink dest="/cpp/language/initialization/copy_elision">Copy elision</DocLink> is an exception from the as-if rule: the compiler may remove calls to move- and copy-constructors and the matching calls to the destructors of temporary objects even if those calls have observable side effects.

Expand Down