C++26 finally stops compilers from deleting infinite loops

Writing while (true); in C++ used to let compilers delete your loop and execute whatever code sat behind it.

C++26 proposal P2809R3 officially eliminates undefined behavior for trivial infinite loops, as detailed by developer Sandor Dargo. Under C++11 forward-progress rules, compilers assumed every loop eventually finished, performed I/O, or accessed shared data. If a bare while (true); loop did none of those, optimizers like Clang could erase it entirely—causing execution to fall through into completely unrelated functions.

Why it matters: Bare-metal and kernel developers routinely write while (true); to halt a device after a fatal hardware error. When the optimizer deleted that loop, the hardware kept executing instructions in a corrupted state. C11 solved this issue years ago, but C++ left the trap open until now.

Know this: To qualify under C++26, a loop must have a completely empty body (; or {}) and a constant expression that evaluates to true (like for (;;);). Qualified loops now safely yield execution instead of triggering undefined behavior. Because the committee accepted the proposal as a defect report, compiler makers can backport the fix to older C++ modes too.

Your crash-handling code will finally stay right where you put it.

Sources