4 ms·
> C++11 move semantics and Rust move semantics are not equivalent. C++ requires every movable type to have a null state, and a (perhaps derived) move constructo
by soulbadguy 10y ago
> C++11 move semantics and Rust move semantics are not equivalent. C++ requires every movable type to have a null state, and a (perhaps derived) move constructor that can potentially throw exceptions.
This doesn't sounds true to me. Can you clarify what you mean by moveable type have to have null state ?
> Rust avoids the null state problem by statically omitting the constructor call
You seems to be conflating move semantics and move optimization...
- dbaupp 10y agoThe parent correct: C++ runs destructors on all local variables, even if they have been "moved from", so it must be valid to do so. This translates into a null/"valid invalid"/sentinel state that the destructor can check to avoid truly destroying the underlying resource (which could result in double-freeing memory, or double-closing a file, or some other undefined behaviour). For instance, moving out of a unique_ptr will swap in NULL, and the destructor checks for that. Static omission of destructors in Rust is more than just an optimisation: locals (and other values) never have their destructors run once they've been moved-from, and thus no values need null states, and moving a value doesn't have to run arbitrary code (it can be, and is, just a memcpy).
- Rusky 10y ago> > omitting the constructor call Oops, too late to edit but that was meant to say "destructor."