Using std :: move (object) and object.method () in the same assignment expression.

Is the result of the following expression correct? What is it?

hash_map[object.key()] = std::move(object);

I'm not sure if the effect of the std::move part will occur before or after the object.key() , so my question is.

+6
source share
1 answer

It is well defined because it doesn't matter what comes first in this code: you can rewrite it to the next equivalent

 hash_map[object.key()] = static_cast<objecttype&&>(object); 

What can we say about the code:

  • object.key() must be executed before being assigned to the map
  • std::move(object) must be executed before being assigned to the map

Then a mapping will be assigned that will accept an xvalued object with any key changes.

+5
source

Source: https://habr.com/ru/post/989914/


All Articles