‹ BackHN Continuity

Thread

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

173 points · 290 comments · ibobev

  1. omoikane · · focus · HN ↗
    > The loop must be a trivially empty iteration statement -- meaning its body is literally empty

    This seems to say that the loop body can not be "continue". Indeed, I just tried -std=c++26 with ";" and got an infinite loop as promised, but "continue" restores the undefined behavior:

    - &quot;while(true);&quot; -&gt; <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;T65o51crx" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;T65o51crx

    - &quot;while(true) continue;&quot; -&gt; <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;Pj9raEcnP" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;Pj9raEcnP

    This is unfortunate since I know of one style guide that prefers &quot;continue&quot; over single semicolons. I guess all those code will be doing &quot;while(true) {}&quot; from now on.

    <a href="https:&#x2F;&#x2F;google.github.io&#x2F;styleguide&#x2F;cppguide.html#Formatting_Looping_Branching:~:text=Empty%20loop%20bodies%20should%20use%20either%20an%20empty%20pair%20of%20braces%20or%20continue%20with%20no%20braces%2C%20rather%20than%20a%20single%20semicolon" rel="nofollow">https:&#x2F;&#x2F;google.github.io&#x2F;styleguide&#x2F;cppguide.html#Formatting...

    1. nullc · · focus · HN ↗
      Nice underhanded coding technique &#x2F; bugdoor material once the &quot;infinite loops are not UB&quot; notion is widespread enough. :P
      1. IshKebab · · focus · HN ↗
        Yeah they haven&#x27;t learnt their lesson at all. Fixing subtle UB by making it even more subtle! Mental.
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.