YES
The reference you have already quoted in your posts clearly states that no elements are copied or moved. (This assumes that node
in your code snippet does not refer to map::node_type
).
The same holds for the insert
operation of the map-node (after modifying its key):
If the insertion is successful, pointers and references to the element obtained while it is held in the node handle are invalidated, and pointers and references obtained to that element before it was extracted become valid. (since C++17)
However, accessing the object between extract()
ion and re-insert()
ion has undefined behaviour and its address taken whilst in the extracted state is of limited use. Quoting from the standard:
The extract members invalidate only iterators to the removed element;
pointers and references to the removed element remain valid. However,
accessing the element through such pointers and references while the
element is owned by a node_type is undefined behavior. References and
pointers to an element obtained while it is owned by a node_type are
invalidated if the element is successfully inserted.
Explanation
Essentially, a map<>
is implemented as a tree of nodes, each holding a key
and T
(which are exposed as pair<const Key, T>
to the user). Nodes are allocated (typically) on the heap and the address of your object is related to that of a node. map::extract()
un-links a node from its tree and returns a node handle (an object holding a pointer to a map node), but AFAIK the node itself is not re-allocated, moved, or copied. Upon map::insert(handle)
, the node is re-linked into the tree according to its (new) key. Again, this involves no re-allocation, move, or copy of the node.
Remark
The above is a rough sketch. How things are actually done is likely more complex and also implementation defined. As explained here a node_handle
does allow to alter the key through the member function
Key &node_handle::key() const;
How this is done under the hood is not specified and I speculate that the implementation uses a union or some cast to allow this. Of course, the map has to present to users a pair<const Key,T>
in order to prevent them from changing the key and hence breaking the map, but this is not of any concern for an element extracted from the map.