‹ BackHN Continuity

Thread

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

173 points · 290 comments · ibobev

  1. Aurornis · · focus · HN ↗
    I never would have guessed that the unreachable() function would get executed in that example. Probably not something you’d encounter in practice, though I have seen some weird things happen with layers of #ifdef
    1. mathisfun123 · · focus · HN ↗
      > Probably not something you’d encounter in practice

      it's actually probably the most common footgun you'll encounter in practice: non-void functions with no return statements just keep executing past their end. ask me how i know.

      compile with -Wreturn-type if you want to avoid such things...

      1. drdexebtjl · · focus · HN ↗
        C on the other hand puts an implicit `return 0` at the end, but only on the main function for some reason. Very weird.
        1. mpyne · · focus · HN ↗
          C++ also special-cases the `main` function. Probably because `main` is the interface to the OS so it gets special language treatment.
          1. mitxela · · focus · HN ↗
            In C++ you're not allowed to call main yourself, so that the compiler can call the global constructors at the start of main.
            1. pjmlp · · focus · HN ↗
              Which also exist as C extensions, in some compilers.
Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.