Note: I\'ve seen similar questions, but none of the answers are precise enough, so I\'m asking this myself.
This is indeed not a very well defined thing in the standard, but I would interpret "depends on" as meaning "the behavior under the rules of the abstract machine is affected".
This behavior consists of the sequence of reads and writes to volatile variables and the calls to library I/O functions (which includes at least the I/O functions of the standard library like printf
, but may also include any number of additional functions in any given implementation, e.g. WinAPI functions). See 1.9/9 for the exact wording.
So the behavior is undefined if execution of the destructor or lack thereof affects this behavior. In your example, whether the destructor is executed or not affects the value of x
, but that store is dead anyway, since the next constructor call overwrites it, so the compiler could actually optimize it away (and chances are, it will). But more importantly, the call to rand()
affects the internal state of the RNG, which influences the values returned by rand()
in the other object's constructor and destructor, so it does affect the final value of x
. It's "random" (pseudo-random) either way, but it would be a different value. Then you print x
, turning that modification into observable behavior, thus making the program undefined.
If you never did anything observable with x
or the RNG state, the observable behavior would be unchanged independent of whether the destructor is called or not, so it wouldn't be undefined.