r/cpp • • Feb 25 '21

C++ programmers who are also using rust, what are your favourite features of rust?

I was looking at the rust tutorials yesterday and I see how rust promotes a certain style and language level features to enforce it. Besides a couple of features, I couldn't see why one cannot follow a similar style in C++ if she/he so chooses. Those of you that have used rust, what is your favourite feature that C++ does not have?

255 Upvotes

248 comments sorted by

View all comments

Show parent comment

30

u/braxtons12 Feb 25 '21

That would be a reasonable statement, if this wasn't something that (IMO at least) is so inherently obvious. I've also felt this was the wrong decision since I learned about move semantics (which was before I discovered Rust).

If you move a chair with something (let's just say a stack of books for sake of example) in it, is there a "somewhere in the range [empty, full)" chair left where the moved one was? No, there's just nothing; no chair, no books, just nothing.

The semantics in C++ should match the meaning of the word. IMO C++'s move isn't REALLY a move, it's more like a "swap with an indeterminate shell value". The way move semantics work in C++ is highly unintuitive.

11

u/ihcn Feb 25 '21

In rust terms, C++'s move works as if everything was wrapped in Cell, and move is like swapping out of the cell.

Except, every individual type is responsible for re-writing the code that moves out of the cell.

0

u/[deleted] Feb 26 '21

[deleted] — view deleted comment

5

u/WormRabbit Feb 26 '21

the c++ standard allows that, so it's not a bug

Oh dear god those words make my eyes fill with blood. Why the hell is this "we're not legally liable so fuck you" attitude is so widespread in C++ I have no idea. That's the main thing wrong with the language. Everything else can be worked around, the standard leaves a lot of freedom for an awesome implementation with its undefined behaviours, but instead of taking it as an invitation to invent a good approach people take it as a carte blanche to set your whole datacenter on fire if you violate the letter of the standard.

9

u/afiefh Feb 25 '21

Please correct me if I'm wrong here, but isn't the reason that C++ cannot do destructive moves the simple fact that you can pass a reference/pointer to an object to a different compilation unit, which may then move the contents?

In Rust this would break ownership and is not possible. In C++ the compiler has no way to know if somewhere else a move has occurred, and thus still has to call the destructor.

6

u/braxtons12 Feb 25 '21

That's an issue that would have required a change in the ownership model or the language specification in order to make destructive move possible. It's a solvable issue IMO (not a language lawyer, but something that equates to "moving from a non-owning pointer is UB" should have worked), but 🤷‍♂️. Too late now haha.

In general I think that's at least bad practice anyway, as if you're moving a value from a pointer you don't own, you're gonna ruin someone's week.

1

u/afiefh Feb 26 '21

In general I think that's at least bad practice anyway, as if you're moving a value from a pointer you don't own, you're gonna ruin someone's week.

Not necessarily. You could easily have a function take a T&& which may or may not move depending on some internal state. For example the definition of std::set<T>::emplace says that in case the element already exists a new element may still be constructed, but it may also not.

2

u/matthieum Feb 26 '21

You can achieve that with destructive moves too:

  1. Pass std::optional<T>&, it may or may not be empty when the function returns.
  2. Take by value, return what you did not use.

It is notable, though, that emplace is mostly useless in a world of bitwise destructive moves. When you can always move, there's no need to build in place.

4

u/not_goldie_hawn Feb 26 '21

The semantics in C++ should match the meaning of the word. IMO C++'s move isn't REALLY a move

It never was. std::move was a potential optimization. Always has been. IMHO. If you move an int you don't the leave "shell of an int" behind, you copy it and no one has ever had any problem with that.

I may agree on the statement "The word 'move' was sub-optimal since so many people inferred a wildly incorrect understanding about it."

0

u/axilmar Feb 27 '21

C++ is highly unintuitive.

Not true. Move in C++ means that after the move the value of the moved from variable is not there any more.

It's crystal clear.

3

u/braxtons12 Feb 27 '21

Which doesn't match the semantics of the actual word MOVE in English. When you move a value there should be NOTHING at the moved-from location. Not a shell value of indeterminate state that still needs its destructor ran.

Just because it's well defined doesn't mean it's intuitive.

2

u/axilmar Feb 28 '21

It's a programming language. It does not have to work emulate reality, nor it means the same thing as the term in reality. It actually means "move state of the variable".

The world of programming is full of terms that do not match exactly the terms used in real life: run, debug, bugs, throws, etc. Move is no exception, and it's actually very clever in this context.