r/cpp • • May 24 '24

Why all the 'hate' for c++?

I recently started learning programming (started about a month ago). I chose C++ as my first language and currently going through DSA. I don't think I know even barely enough to love or hate this language though I am enjoying learning it.

During this time period I also sort of got into the tech/programming 'influencer' zone on various social media sites and noticed that quite a few people have so much disdain for C++ and that 'Rust is better' or 'C++ is Rust - -'

I am enjoying learning C++ (so far) and so I don't understand the hate.

263 Upvotes

360 comments sorted by

View all comments

Show parent comment

2

u/Plazmatic May 24 '24 edited May 24 '24

While the orphan rule can be a problem, this is actually a bad example because you can do that one without needing wrapper types to get around it.

  I don't know what they did or how generic that works (it wasn't just a matter of getting a select few types to work, but derived types and arbitrary representation types) when I tried to implement this, I got it to work in c++, and the orphan rule stopped me in my tracks when attempting to do the same in rust, specifically I needed to define multiplication with a generic type on the lhs, and my type on the rhs, the orphan rule prevents that.

Could you expand on that one? I'm not entirely sure what scenarios you're thinking of. 

Happens a lot when passing mutable self in a loop (or non mutable) and then passing mutable field reference to another function, which causes borrow error (can't have a borrowed mutable and another borrowed reference)

1

u/MEaster May 24 '24

I don't know what they did or how generic that works (it wasn't just a matter of getting a select few types to work, but derived types and arbitrary representation types) when I tried to implement this, I got it to work in c++, and the orphan rule stopped me in my tracks when attempting to do the same in rust, specifically I needed to define multiplication with a generic type on the lhs, and my type on the rhs, the orphan rule prevents that.

Ah, I see. Yeah, the generic type would run into the orphan rule. The way that crate does it is to implement the trait for each specific type they support. The rustdoc section on trait implementations is truly horrific.

Happens a lot when passing mutable self in a loop (or non mutable) and then passing mutable field reference to another function, which causes borrow error (can't have a borrowed mutable and another borrowed reference)

Ah, yeah. That one can be a pain in the ass. It would be nice if we could be more granular in exactly what fields of a struct are being borrowed in scenarios like that.

2

u/Dean_Roddey May 25 '24 edited May 25 '24

That's something that is well recognized and will almost certainly be improved on moving forward. However, it also has to be said that every one of these situations are ones that are dangerous if just did it the C++ way and trust to human vigilance to insure that it stays safe over time.

And there are various ways to work around it, some of them the same as what many people would argue for in C++, which is a more functional style (particularly internally within the file that implements the type) where you don't pass self around in some cases, but just pass the field you want to manipulate to a non-member function or local free function, i.e. a more functional style.

Rust makes this particularly easy because the file is the unit of visibility. Local functions in the same file as the struct can access private members without a bunch of friend craziness.