C++26: Trivial infinite loops are no longer undefined behaviour

Let’s start with a question! Is this program well-defined? 1 2 3 4 int main() { while (true) ; } If you said yes, you’d be wrong — at least before C++26. A while (true); loop with no side effects used to be undefined behaviour. Compilers were free to assume it terminates, and some — Clang in particular — would optimize it away entirely, with spectacular consequences: 1 2 3 4 5 6 7 8 9 10 11 // https://godbolt.org/z/WYMxxeW1T #include <iostream> int main() { while (true) ; } void unreachable() { std::cout << "Hello world!" << std::endl; } In…
155 points by ibobev 34 days ago www.sandordargo.com 213 comments

Summary

C++26 changes the status of infinite loops without side effects, which were previously considered undefined behavior. This update allows developers to write such loops confidently, knowing that compilers will treat them correctly instead of optimizing them away.

Comments (213 of ) ⌛

Loading more comments... ⌛
Scroll down to load more comments
No comments available.
Type to search HN and engineering blog articles
Press Escape to close  |  ↑↓ to navigate  |  Enter to open