P3091R6 — Better lookups for `map` , `unordered_map`, and `flat_map`
Pablo Halpern
LWG
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 the addition (in C++29) of a `lookup` member function that returns `optional<T&>` and leverages the observers and monadic operations of `optional` , such as `value_or` , to simplify lookups for `map` , `unordered_map` , and `flat_map` . This proposal’s usefulness would also be enhanced by the *maybe* facilities proposed in [[P1255]](http://wg21.link/P1255).