2 ms·
> Rule 4: When we move out of a variable, we can't use it anymore. > C++ doesn't have rule 4, in C++ the compiler lets us accidentally use the variable after w
by codedokode 3y ago
> Rule 4: When we move out of a variable, we can't use it anymore.
> C++ doesn't have rule 4, in C++ the compiler lets us accidentally use the variable after we move out of it.
Why is it so? Moving is not a legacy operation, why did they allow to use moved-out variable?
- gary_0 3y agoC++ move semantics was added to the existing standard library and class syntax in a way that avoided breaking old code. The language added "move constructor" conventions and the like, rather than making more drastic internal changes. So moving from a std::string variable means it's still a valid object, only its internal state is now owned by someone else, and the std::string is required to now be in an "empty" (but still valid) state. Most of the "moving" is actually handled by extra code in std::string rather than the core language having rules for how the underlying bytes are moved around, because the core language wasn't changed that much.