Can different optimization levels lead to functionally different code?

The portion of the C++ standard that applies is ยง1.9 “Program execution”. It reads, in part:

conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below. …

A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible execution sequences of the corresponding instance of the abstract machine with the same program and the same input. …

The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions. …

So, yes, code may behave differently at different optimization levels, but (assuming that all levels produce a conforming compiler), but they cannot behave observably differently.

EDIT: Allow me to correct my conclusion: Yes, code may behave differently at different optimization levels as long as each behavior is observably identical to one of the behaviors of the standard’s abstract machine.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)