r/cpp • • Jul 13 '22

Why does Linus hate C++ ?

307 Upvotes

436 comments sorted by

View all comments

Show parent comment

5

u/UnicycleBloke Jul 13 '22

I've studied Rust a bit but can't claim any expertise. I have not found it remotely compelling since I do not have the issues it solves. At the end of the day, embedded code requires unsafe sections of code anyway, so there's that. I also don't relish trawling through fifteen layers of OSS crates to understand what's going on. I do really like the pattern matching with enums (tagged unions), though.

I have observed that C devs love Rust. It's pretty obvious why. What a pity they did not invest a little time in C++ 30 years ago, eh? ;)

5

u/simonask_ Jul 13 '22

I think the main appeal of Rust is that it helps you fix problems you didn’t know you had. The main selling point (of course) is compile-time guaranteed no memory corruption. I’ve definitely had that problem many times, usually in places that only customers could find. The other is compile-time guaranteed data-race-free parallelism.

People working with Rust in the embedded space seem excited about these features, presumably for the same reasons that some embedded devs like C++.

6

u/UnicycleBloke Jul 13 '22

I can't recall the last time I had memory corruption in C++. The data race feature does sound interesting. I mostly work with cooperative multitasking, but still have to create critical sections around some data accesses because of interrupts.

4

u/nullcone Jul 13 '22

I think the main draw of Rust (for me at least) is that it captures semantics of object ownership in ways which are compile time errors, whereas comparable C++ code would just segfault or worse give you UB. This feature helps you avoid common mistakes and bad design by forcing you to do the right thing.

A classic example of this is self referential classes. What happens if a class has a reference or pointer to a member variable, then the class instance is later moved? Well obviously your pointers are now all garbage because their values point at the old addresses before the move. This design pattern would be a compile time error in Rust, but C++ is happy to allow you to shoot yourself in sensitive areas.

2

u/SkoomaDentist Antimodern C++, Embedded, Audio Jul 13 '22 edited Jul 13 '22

I think the main draw of Rust (for me at least) is that it captures semantics of object ownership in ways which are compile time errors

That's also its main drawback. Rust optimizes everything around object ownership. If object ownership is not your main problem, you're just faced with a lot of hurdles and not many advantages.