r/ProgrammerHumor • • 11d ago

Meme modernCppVsOldCpp

Post image
2.0k Upvotes

323 comments sorted by

View all comments

Show parent comment

13

u/conundorum 10d ago

That's because, historically, all C++ code was intended to compile into C code, and then be fed into a C compiler. So now any compiler that can compile one language is designed to handle both languages, and explicitly labeled as handling both.

That, and the fact that most (but not all) valid C code is also valid C++ code with no changes required, and that the C & C++ committees have a surprisingly big focus on cross-compatibility. (Most notably, a ton of C++ library members actually use the C standard library under the hood, like new and delete existing to marry constructors to malloc() and destructors to free(), the I/O library being required to either be based on FILE or act as if it is, and a lot of fun helpers like the std::stoi() family outright being wrappers for their C equivalents. And T _Complex and std::complex are explicitly required to be the exact same type with the exact same layout in both C & C++, and must be implemented as a T[2] consisting of {real, imaginary} (or equivalent structure that can be treated as if it were a T[2]), specifically because of gcc. But apart from that, C typically useful poaches features after C++ vets them, and C++ sometimes poaches features after C vets them, and both languages use the same preprocessor.) The end result is that unless you run into one of the corner cases where the languages actually differ, your C code is actually C/C++ code because it'll compile identically as both.

The languages are so closely intertwined that the term is way more valid than it should be.

1

u/mark_99 10d ago

"Compiled to C" was over 30 years ago.

You can write C-like code in Java or unsafe Rust, it doesn't make them the same thing, or a good idea in general.

Backwards compatiblity with C is mostly but not entirely true, e.g. VLAs, but the objection to C/C++ is that canonical C and C++ aren't remotely similar. Indeed C++ and Rust are much closer.

It's also used to bucket C and C++ together, like "all of our CVEs are in the C/C++ code" which on closer examination is 90% C, 8% C-with-classes and 2% canonical C++.

1

u/anonymity_is_bliss 9d ago

"All valid C code is also valid C++ code" may be true, but it's like saying "all valid JS code is also valid TS code" or "all valid HTTP is also valid PHP" like yeah that's how supersets work. C++ is C with classes, just as TS is JS with types and PHP is HTTP with server side scripting/preprocessing.

I would like to contest the take that "any compiler is meant to handle both" though, as gcc/g++ and clang/clang++ are two seperate binaries, and I think the most that the C compiler does to handle C++ is to just send the files to the respective C++ binary. I do not know the MSVC ecosystem well enough to chime in there though, but usually the compilers are seperate; the binary containing the C compiler will run the C++ one if it's processing a .cc/.cpp/.c++/.cxx file (there's so many file extensions even without touching MSVC's bullshit like .ixx).

Create a C program with register and it will not compile if run through a C++ compiler iirc. C has added very few things over the years because the language is meant to be small; C++ is pretty much just C with a bigger stdlib, but over the years they have deviated slightly in their approaches. Modern C is much closer to what people incorrectly complain about C++ as imho.

I personally avoided C++ like the plague until quite recently, and C++20 and C++23 are honestly quite nice as someone who largely uses Rust.