|
|
|
|
|
by nayuki
723 days ago
|
|
Your reasoning is incorrect. Here is how I reason about it. Division by zero is undefined behavior. The compiler can assume that it will not happen. If the divisor is not zero, then the calculation has no side effects. The compiler may reorder the division above the print, because it would have no observable difference in behavior. This could be useful because division has a high latency, so it pays to start the operation as soon as the operand values are known. If the divisor is zero, the UB says that there is no requirement on how it's compiled, so reordering the division above the print is legal. |
|
If you replace 'if(div)' with an opaque function call, that doesn't change anything as the function might never exit the program, never return, long jump or return via an exception.