After std::move
is called on an object, why doesn't the language cause a compilation error if the object is used after?
Is it because it is not possible for compiler to detect this condition?
Nothing. It'll be treated as any other object after that. This means that the destructor will still be called.
std::move itself does "nothing" - it has zero side effects. It just signals to the compiler that the programmer doesn't care what happens to that object any more. i.e. it gives permission to other parts of the software to move from the object, but it doesn't require that it be moved.
The general principle in C++ language design is "trust the programmer". Some issues I can think of with rejecting any use of the object after it has been an argument to std::move
.
std::move
in the general case would be equivalent to solving the halting problem. (In other words, it can't be done.) You would have to come up with some rules that describe what you mean by "after" in a way that can be statically determined.std::move
. (A particular class might cause an assertion, but that would be a very odd design.)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With