ISO/IEC JTC1 SC22 WG21 N4240

Date: 2014-10-11

Thomas Köppe <>

Improved insertion interface for unique-key maps (Revision 2)

Revision history


  1. Background
  2. Summary
  3. Impact on the standard
  4. Technical specifications
  5. Notes
  6. Questions for the WG


The existing interface of unique-keyed map containers (std::map, std::unordered_map) is slightly underspecified, which makes certain container mutations more complicated to write and error-prone than necessary. This paper describes new member function templates to fill this gap.

The justification and rationale for the new interface are given in N3873. The initial reaction to N3873 in Issaquah was that the existing map interfaces should be fixed rather than adding new interfaces. We explored this idea in N4006 in Rapperswil and decided that the original proposal was preferable (with some name changes). This paper only summarises the proposed extension without repeating the original discussion. We only restate the motivating code snippet here for motivation:

std::map<std::string, std::unique_ptr<Foo>> m; m["foo"] = nullptr; auto ptr = std::make_unique_ptr<Foo>; auto res = m.emplace("foo", std::move(ptr)); assert(ptr);    // ??? (may or may not fire)


To each of the unique-keyed map container templates std::map and std::unordered_map, we propose to add the following new, specialised algorithms:

The utility of these two algorithms lies in the fact that they make mutations possible that would be verbose to spell out, error-prone, surprising and hard to teach with the existing interface. Moreover, the algorithms can perform their actions as efficiently as possible, since they are able to take advantage of the internal structure of the container, thus filling a gap where users might previously have felt that they “could do it better by hand”. Briefly:

Finally, since both new algorithms separate parameters into key and mapped type components, they are somewhat more intuitive than the generic container mutators that are expressed in terms of value_type (which is a std::pair). This point is often confusing or annoying to users and hard to teach.

Impact on the standard

This is purely an extension of the standard library. Four new function templates have to be added to [map.special], and also to a new section “specialised algorithms” [unord.maps.special] for unordered maps. There is no interference with existing code.

Technical specifications

Add the following to [maps.special], and to a new subsection under [unord.maps], named “unordered_map specialised algorithms [unord.maps.special]”:

template <typename ...Args> pair<iterator, bool> try_emplace(const key_type & k, Args &&... args) template <typename ...Args> pair<iterator, bool> try_emplace(const_iterator hint, const key_type & k, Args &&... args)

Effects: If the key k already exists in the map, there is no effect, no dynamic allocations are performed and no exceptions are thrown. Otherwise, inserts the element constructed from the arguments as value_type(k, forward<Args>(args)...) into the map. The bool part of the return value is true if and only if the insertion took place, and the iterator part points to the element of the map whose key is equivalent to k. The hint iterator provides an insertion hint.

Complexity: The same as emplace and emplace_hint, respectively.

template <typename M> pair<iterator, bool> insert_or_assign(const key_type & k, M && obj) template <typename M> pair<iterator, bool> insert_or_assign(const_iterator hint, const key_type & k, M && obj)

Effects. If the key comparing equal to k does not exist in the map, inserts the element constructed as value_type(k, forward<M>(obj)). If the key already exists, the mapped part of the value is assigned forward<M>(obj). The bool part of the return value is true if and only if the insertion took place, and the iterator part points to the inserted or updated element. The hint iterator provides an insertion hint.

Complexity: The same as emplace and emplace_hint, respectively.


The original names in N3873 were “emplace_stable” and “emplace_or_update”, and both took only a single second parameter M && obj. In Rapperswil, the names try_emplace and emplace_or_assign were proposed, and the question arose whether the signatures could be variadic.

Making try_emplace variadic seems to pose no further obstacle, and this is how it appears in this paper. As for emplace_or_assign, we agreed that a variadic signature would not fit well with assignment (we never have variadic assignment operations in the standard), so we retained the single-parameter form here. However, with a single argument the function feels less like an “emplace” and more like an “insert”, which is why we are tentatively proposing the name insert_or_update here. Naturally, this is up to debate.

In Rapperswil there were also questions as to whether there should be overloads in which the key parameter is taken by mutable (rvalue) reference. This was deemed a less common use case so that we are not pursuing it here, but the proposed design is amenable to future extensions should a desire for further generality arise. This also includes considerations like templated key parameters and support for transparent comparators; transparent comparators are currently only considered for look-up functions, not for mutators.

Questions for the WG

Questions in poll form.