I've got following code:
std::list some_data;
...
std::list new_data = std::move(some_data);
some_data.clear();
...
The question is whether some_data.clear()
is necessary? (for the record, some_data
will be reused in the future)
A: You should use std::move if you want to call functions that support move semantics with an argument which is not an rvalue (temporary expression).
Note that moving the vector around doesn't change the vector, as the position of the vector doesn't affect the magnitude or the direction. But if you stretch or turn the vector by moving just its head or its tail, the magnitude or direction will change.
You must not use a variable after calling std::move() on it. Since you have casted your variable to an rvalue, functions that receive rvalues may act destructively on your variable, so using the variable's contents afterward may result in undefined behaviour.
In general, it is perfectly safe to assign to an object that has been an argument to std::move .
Yes, it's necessary.
Only the std smart pointers are guaranteed to be in a default constructed state after being moved from.
Containers are in an valid, but unspecified state. This means you can only call member functions without preconditions, e.g. clear
, that put the object in a fully known state.
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