Is the result of the following expression well-defined? What is it?
hash_map[object.key()] = std::move(object);
I'm not sure whether the effects of std::move
part would occur before or after the object.key()
part, hence my question.
std::move is used to indicate that an object t may be "moved from", i.e. allowing the efficient transfer of resources from t to another object. In particular, std::move produces an xvalue expression that identifies its argument t . It is exactly equivalent to a static_cast to an rvalue reference type.
std::move is actually just a request to move and if the type of the object has not a move constructor/assign-operator defined or generated the move operation will fall back to a copy.
We know that std::move does not actually move anything. It just cast an lvalue reference (&) to rvalue reference (&&).
Use an assignment operator operator= that returns a reference to the class type and takes one parameter that's passed by const reference—for example ClassName& operator=(const ClassName& x); . Use the copy constructor.
It is well defined because it doesn't matter what comes first in this code: You can rewrite it to the following equivalent
hash_map[object.key()] = static_cast<objecttype&&>(object);
What can we say about the code:
object.key()
should be executed before assignment to the mapstd::move(object)
should be executed before assignment to the mapThen there will be the assignment to the map which will accept xvalued object
with whatever changes key
function did.
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