https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114421
Xi Ruoyao <xry111 at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |xry111 at gcc dot gnu.org --- Comment #3 from Xi Ruoyao <xry111 at gcc dot gnu.org> --- (In reply to Earle F. Philhower, III from comment #2) > Thanks very much for the fast explanation. For reference of others who > might hit this, I see references to C11 6.8.5 pp 6 online (but of course > it's an ISO document so not freely available). > > "An iteration statement whose controlling expression is not a constant > expression, that performs no input/output operations, does not access > volatile objects, and performs no synchronization or atomic operations in > its body, controlling expression, or (in the case of a for statement) its > expression-3, may be assumed by the implementation to terminate." For this topic C++ is actually more strict than C so referring the C standard may be still misleading. In C++ there is no "the controlling expression is a constant expression" exempt, so even while (true); can be optimized out. AFAIK GCC does not optimize away while (true); in C++, but Clang is actively doing this. There is a WG21 paper to add the constant expression exemption for C++ like C (I cannot remember its Nxxxx ID though).