P3091R4 — Better lookups for `map` , `unordered_map`, and `flat_map`
Pablo Halpern
LEWGLWG
Revisions R6Jul 2026R4Jul 2025
The most convenient way to look up an element of a `map`-like container is to use the index operator, i.e., `theMap[key]`. This operator cannot be used, however, when 1) the container is `const`, 2) the mapped type is not default constructible, 3) the default-constructed value is inappropriate for the context, or 4) the container should not be modified. These limitations often force the user to resort to the `find` member function, which returns an iterator that points to a `pair` and typically leads to more complex code having at least one `if` statement and/or duplicate lookup operations. Taking inspiration from other languages, especially Python, this paper proposes a `get` member function that returns `optional<T&>` and leverages the observers and monadic operations of `optional` to simplify lookups for `map`, `unordered_map`, and `flat_map`. Note that this proposal is built on `optional<T&>`, which is described in [[P2988R12](https://wg21.link/p2988r12)] and is expected to be voted into the C++ Working Draft in Sofia. It’s usefulness is also enhanced by the `maybe` facilities described in [[P1255](http://wg21.link/P1255)].