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

21 days ago by CodyIT to c/cpp

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 int main() { while (true) ; } void unreachable() { std::cout << "Hello world!" << std::endl; } In…
lambalicious 2 points 21 days ago

One more lie to be added to (LTS / non-"latest commit") C++...

Move doesn't move, forward doesn't forward, (variadic list of failures...), infinite loops don't.

path: 0 26009270, hotness: undefined, score: 2, children: 0
RovingDiary 1 point 12 days ago

A great read. It's wacky how the compiler can simply fallthrough to next linked statement. Anways glad to see these cases being defined now.

path: 0 26157944, hotness: undefined, score: 1, children: 0
cpp
cpp

@programming.dev

login for more options
2380
415
47

The center for all discussion and news regarding C++.

Rules

  • Respect instance rules.
  • Don't be a jerk.
  • Please keep all posts related to C++.

go to feed...