‹ BackHN Continuity

Thread

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

173 points · 290 comments · ibobev

  1. account42 · · focus · HN ↗
    Unfortunate. There isn't ever a good reason to have an infinite loop so concerned compilers could have just diagnosed this as a warning.
    1. echoangle · · focus · HN ↗
      The article mentions a use case for that:

      > What I found is that this is common in embedded and kernel code as a halt-on-error pattern. When a fatal error occurs and there’s no operating system to exit to, you simply stop:

      1. pdonis · · focus · HN ↗
        If this is a genuine use case, I wonder why the language can't just introduce a built-in function for it. For example, std::get_stuck_here(). Then the compiler would know not to optimize this away. The implementation under the hood could still be an infinite loop, but the compiler would not have to guess why it's there.
        1. sigbottle · · focus · HN ↗
          __asm__ __volatile("hlt"); when doing quick and hacky debugging could work
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.