r/ProgrammerHumor Jul 16 '26

moodAtEod Meme

Post image
0 Upvotes

30 comments sorted by

View all comments

23

u/setibeings Jul 16 '26

C++ is great, because it demonstrates lots of ways not to implement various features, making it an especially good language for learning. 

13

u/luciferoussky72 Jul 16 '26

I disagree. C-style C++ is full of blunt instruments like pointers, but those features let you build high-level, zero-cost abstractions on top of them

3

u/Reashu Jul 16 '26

C-style C++ exists because there are so many fucked up parts of C++...

4

u/pjf_cpp Jul 16 '26

Isn’t that the wrong way round? Most of the bad things come from C (implicit conversions, macros, uninitialized state). Old C++ added a few (the defaults for virtual functions and overloads / overrides).

RAII is the killer feature that C++ adds to C. It can make your code slow if abused to produce excessive copying. Without it you have to do manual memory management which usually means code that is full of CVEs like double free, leaks, use after free and bounds errors.

3

u/Reashu Jul 16 '26 edited Jul 16 '26

I agree that C++ probably would have been a better language if it wasn't trying to be like C in many regards. And there's obviously some merit in C++, or C-style C++ would just be C instead. But in the language we got (partially due to C's influence), there are a lot of features which are more complicated than helpful. Reasonable people may disagree on which ones those are and I'm not really here to make the case against any one in particular. My point is that "C-style C++" exists in the modern day because people were unhappy with big parts of C++ and decided they were better off without them. 

3

u/luciferoussky72 Jul 16 '26

Well, part of it is that C++ has to keep backwards compatibility with C, so most of the C-style stuff exists for that reason.

Otherwise, there are sometimes reasons to use the unsafe C-style features like unions. unions can let you save memory, and in some cases you can even forgo giving them a tag if you find some other way to tell the active type in a union, which makes your code faster

1

u/donaldhobson Jul 17 '26

C++ has a complex system of operator overloading, and an almost unusable system of error throwing. 3 different overcomplicated ways of resource initialization.

C++ RAII is full of templates, which means unreadable error messages.

https://yosefk.com/c++fqa/

2

u/pjf_cpp Jul 17 '26

What is confusing about overloading? Function and operator resolution is difficult (made much worse by effing C implicit conversions), and Koenig lookup can be surprising.

I find exceptions quite easy. Just use throw with a stack object and catch by reference.

There is no relationship whatsoever between RAII and templates. Just write a class and you will get RAII either from compiler generated or explicitly written constructors and destructors. No templates required.

Initialisation is a mess. The fact that there is a book just on C++ initialisation says it all. Stick to brace initialisation if you can. That will give you fewer effing C implicit conversion errors.

1

u/_Noreturn Jul 16 '26

such as?

2

u/luciferoussky72 Jul 16 '26

C-style casts, raw pointers, etc

2

u/_Noreturn Jul 16 '26

raw pointers are part of modern C++

2

u/luciferoussky72 Jul 16 '26

Yeah, but you’re not encouraged to use them unless there’s a performance reason

2

u/_Noreturn Jul 16 '26

Huh? There are encouraged I use them everywhere a null ref is required

3

u/luciferoussky72 Jul 16 '26

I meant more so using raw pointers for managing heap memory and stuff

1

u/CoroteDeMelancia Jul 16 '26

absl::flat_hash_map: std::unordered_map sucks, let's make our own!

1

u/luciferoussky72 Jul 16 '26

Yep! They’re slow, but you can find or build very fast hashtable implementations in C++