by bellowsgulch · 3mo ago · 0 comments
Like almost without fail. I can’t even think of how you’d end up in a scenario like this because a compiler would complain before letting you use an undefined implicit declaration.
I’m not going to defend all of the ones C(++) has but stuff like deref’ing null or integer overflow or out of bounds access has its roots in sanity, it just looks insane today. There are tools for many/most of these and if you’re not using them you’re doing it wrong.
That's not possible for 'standard C' though, only for the separate language that's called "the common subset of C and C++". E.g. it works more or less by accident for some specific C code that predates C99, but even that is built with C++ semantics (which has many subtle and some not-so-subtle differences to C semantics).
Instead of trying to build C in C++ mode, turn all the warnings to 11 (and also include warnings that aren't in -Wall -Wextra, like -Wsign-conversion).
The C++ compatibility with C, is "As Close as Possible to C, but no Closer", meaning supporting C should not break the stronger type checking or the improved type system C++ has over C.
As such, besides C89 original differences, compatibility has only been added for C features that don't clash with C++ approach to the same problem, and while the C++ standard library keeps up with ISO C, it also does so taking into consideration C++ features for the implementation of said library functions.
"Sibling Rivalry: C vs C++"
https://www.stroustrup.com/sibling_rivalry.pdf
"C and C++: Siblings"
https://www.stroustrup.com/siblings_short.pdf
"C and C++, a case for compatibility"
https://www.stroustrup.com/compat_short.pdf
Two examples of the library updates,
"C++17 should refer to C11 instead of C99"
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p00...
"C++26 should refer to C23 not C17"
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2025/p33...
Best of luck, =3
One silly side effect of compiling C in C++ mode is that it forces you to cast from void pointers. E.g. the famous example of:
bla_t* bla = (bla_t*)malloc(sizeof(bla_t));
That extra cast is just a silly and completely pointless C++-ism. It's only the tip of the iceberg though (another difference was zero-initialization with `{}` vs `{0}`, but that's harmonized now in C23 so that `{}` is also valid in C, but as soon as you want to use C99 designated init, the 'harmonization' ends).One critical missing piece of C compilation mode was usually that the compiler didn't warn when using a wrong enum type, but that's also fixed by now (assuming `-Wall -Wextra`, but that should always be enabled anyway, otherwise you're just flying blind), e.g.: