Since C++17, associative containers support the extraction of a node and its re-insertion (possibly into another container of the same type). The object returned by extrac
I think the predominant subtlety in the "extraction" system is that the value_type
of a map
is pair
— note the const
!
Modifying a const object causes undefined behaviour, so you need to be very careful not to modify something that's known to be const. While the node is part of any map, the key is const. The "magic" of the extraction machinery (and the reason it took so long to specify) is that while the node is extracted, the key is not const.
This basically requires you to look at the problem really hard and convince yourself that a pair
can sometimes be interpreted as a pair
(and bear in mind that pair
is a template that users are permitted to specialize!). So to avoid any potential for const objects being modified, there must be a clear sequencing of inserted and extracted states for any node.
There is standard wording to help with the specialization issue in [container.node.overview]p4:
If a user-defined specialization of
pair
exists forpair
orpair
, whereKey
is the container’skey_type
andT
is the container’smapped_type
, the behavior of operations involving node handles is undefined.