miniblog.

← Back to all posts
57
C++ no longer considers trivial infinite loops to be undefined behaviour! https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2809r3.html Spotted in the release notes for the latest clang.

7 Comments

@wilfredh Seriously, what kind of stance is “If we can detect that your code has an infinite loop anywhere under any circumstances, we reserve the right to erase all your data.“? What is a “trivial” infinite loop anyway? Is it time for us all to review the halting problem? :) Like I said, I’m glad JavaScript is perfect and doesn’t have any quirky behavior like this.
@DavidAnson @wilfredh Not saying this is good ergonomics, but you have it backwards. The old rule was “_because_ we can’t solve the halting problem, but still want to be able to optimize, we need you to ensure for us that your loops do terminate (or do something else easily recognizable)”. The rules of the game were that the compiler was allowed to assume every trivial loop terminates so that it could do optimizations like raising or lowering unrelated code across loops.
@DavidAnson @wilfredh Many of the rules around undefined behavior in C++ are like this: they’re not just arbitrary dumb things, but rather a reasoned choice of prioritizing optimization over safety. That’s not an entirely illogical choice, particularly decades ago. It’s just fallen out of fashion now, for good reason. (The other common sort of UB rule comes from prioritizing performance on some random machine that happened to exist when an early compiler was written and no longer matters.)
@jacobsa @wilfredh That makes sense, thank you for the explanation! That said, I’m pretty sure there were plenty of while(true) loops back in the day and it’s slightly terrifying to think all of those programs represent undefined behavior. :)