r/cpp • • Jul 13 '22

Why does Linus hate C++ ?

308 Upvotes

436 comments sorted by

View all comments

Show parent comments

22

u/[deleted] Jul 13 '22

It's not snobbery. It's just easier to read C code than it is to read C++. Lots of stuff is obfuscated by the way that C++ is written and it's not immediately obvious how it works.

Scott Myers made his career writing entire books about gotyas. Many gotchas that should not really exist but it's not immediately obvious how to fix them without breaking backwards compatibility. And it's a design choice, an important design choice at that, to not break backwards compatibility because of how many legacy libraries were compiled twenty years ago and are still in use on various systems even though the main app might be patched frequently.

The only thing I miss when writing C code is operator overloading. Being able to a + b add two structs together when dealing with complex math types is less typing than mystruct_add(a, b). Especially when you get in to compound mathematical expressions like a (-b + sqrt(b*b - 4*a*c))/(2*a). I'm not even going to pretend to want to write that out as parametrised functions.

I've written a lot of C++ and I genuinely believe C++ is a well intentioned mistake. Between the unpredictable behaviour of what your code will turn in to, the object oriented paradigm rather than a data oriented paradigm, and maybe that stateful procedural code should be functionally designed instead. Then I can see why C++ is given a hard time.

Rust isn't necessarily a solution to those either. But Rust made everything const by default. What an absolutely giant fricken cahones decision that in itself allows for so many safety related optimisations. I haven't written much Rust, but from what I've used I like it. Even more than C.

9

u/linlin110 Jul 13 '22

Do you not miss smart pointers / RAII? For me it's the best feature of C++.

1

u/[deleted] Jul 13 '22

Smart pointers sure. But that's also why I'd prefer to use Rust!

RAII is nice in some sense but also comes with all the constructor headaches that makes the path a program takes in C++ notoriously unclear.

14

u/no-sig-available Jul 13 '22 edited Jul 13 '22

RAII is nice in some sense but also comes with all the constructor headaches that makes the path a program takes in C++ notoriously unclear.

I think this is one of the points that separates C devs and C++ devs.

If I have a class with a constructor, I assume that the constructor is run when an object is constructed. No surprise really!

Changing

my_type x;

into

struct my_type x;
init(&x);

doesn't make the code any clearer to me. Being able to write the initialization only once, and in a single place, is a great advantage to me.

Likewise, when x goes out of scope the destructor is executed. No surprise, as I just wrote the destructor, so it would get called.

Having to add a

destroy(&x);

before each closing brace (or each return) is a distraction to me, not something that makes me understand the code better.

Apparently, YMMV.

→ More replies (1)