logoalt Hacker News

cmovqyesterday at 5:47 PM2 repliesview on HN

Note that compilers are more than happy to delete 'while (true)' if the loop doesn't have side-effects.


Replies

mpyneyesterday at 9:52 PM

I think this is no longer true. C++26 implemented a change to make trivial loops like these defined behavior (and therefore will loop endlessly as you'd expect). And this example was always defined behavior in C.

Both languages continue to have examples of slightly more complicated loops that can be assumed to terminate in the absence of side effects, but `while(true)` isn't one of those any longer.

mort96yesterday at 6:32 PM

Of course. I wasn't talking about empty loops.

But also, I wouldn't rely on a compiler to remove empty 'while (true)' loops.