Remix.run Logo
Aurornis 11 hours ago

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

saghm 9 hours ago | parent | next [-]

That's kind of what you get with UB; the compiler doesn't need to do what you expect.

mathisfun123 9 hours ago | parent | prev [-]

> 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...

mitxela 2 hours ago | parent | next [-]

In C. In C++ it's an error to not return, as it should be.

mathisfun123 33 minutes ago | parent [-]

.... The blog post literally demonstrates that it is not. Feel free to repro on your own machine.

kouosi 9 hours ago | parent | prev | next [-]

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

Isn't -Wreturn-type enabled by default in both gcc and clang atleast for c++?

mathisfun123 8 hours ago | parent [-]

probably - i guess then i meant -Werror=return-type

drdexebtjl 6 hours ago | parent | prev [-]

C on the other hand puts an implicit `return 0` at the end, but only on the main function for some reason. Very weird.

mpyne 3 hours ago | parent [-]

C++ also special-cases the `main` function. Probably because `main` is the interface to the OS so it gets special language treatment.

mitxela an hour ago | parent [-]

In C++ you're not allowed to call main yourself, so that the compiler can call the global constructors at the start of main.