3 ms·
Destructive move would be incompatible with c++ as it works today. If you construct an object it’s valid. Since move doesn’t do delete() (how could it?) it lea
by gumby 7y ago
Destructive move would be incompatible with c++ as it works today. If you construct an object it’s valid. Since move doesn’t do delete() (how could it?) it leaves behind a block of memory that the program considers an X and so must be valid.
- olliej 7y agoThe compiler can just say "this value is dead, you can't use it". That's what rust does - rust isn't literally freeing/clearing memory when a struct is moved.
- gumby 7y agoRust can do this for an arbitrary element of a dynamically allocated array?
- olliej 7y agoRust can only do that in unsafe blocks. The C++ version would need the existing (unchangeable) move semantics.
- roca 7y agoNo. You can't move an element out of an array in Rust. What Ollie said is true for local variables, and collections generally support some kind of "move element(s) out of the collection" API (e.g. Vec::pop(), T::into_iter()).
- gumby 7y agoSo rust can do it in a subset of cases, such that it can make a stronger assertion; C++ can do it in the general case, and is therefore unable to make the stronger assertion. Neither is correct vs the other, rather they reflect different choice points in the language possibility space.
- roca 7y agoYou can do a C++ style "swap object with a placeholder" move in Rust, easily, with std::mem::replace(). There's no advantage for C++ here.