Title
Replacement of keys in associative containers
Status
new
Section
[associative.reqmts.general][unord.req.general]
Submitter
Jens Maurer

Created on 2022-04-19.00:00:00 last changed 30 months ago

Messages

Date: 2022-05-17.11:58:16

Proposed resolution:

This wording is relative to N4910.

  1. Modify [associative.reqmts.general] as indicated:

    -5- For set and multiset the value type is the same as the key type. For map and multimap it is equal to pair<const Key, T>. Ending the lifetime of the key subobject of a container element by means other than invoking a member function of the container results in undefined behavior.

  2. Modify [unord.req.general] as indicated:

    -7- For unordered_set and unordered_multiset the value type is the same as the key type. For unordered_map and unordered_multimap it is pair<const Key, T>. Ending the lifetime of the key subobject of a container element by means other than invoking a member function of the container results in undefined behavior.

Date: 2022-05-15.00:00:00

[ 2022-05-17; Reflector poll ]

Set priority to 3 after reflector poll. One vote for NAD.

Date: 2022-04-19.00:00:00

Keys for elements of associative containers are presented as const subobjects, preventing their modification by user code according to [dcl.type.cv] p4.

However, that does not prevent those keys to be transparently replaced, for example via

std::map<int, int> map;
map.emplace(1, 2);
using KT = std::map<int, int>::key_type;
auto it = map.begin();
it->first.~KT();
new (const_cast<int*>(&it->first)) KT(3);

This, of course, breaks the ordering of the keys, and should be undefined behavior.

Related issue: CWG 2514.

History
Date User Action Args
2022-05-17 11:58:16adminsetmessages: + msg12464
2022-04-24 08:08:25adminsetmessages: + msg12423
2022-04-19 00:00:00admincreate