Search code examples
c++stlcontainersc++23

Is flat_map an STL container?


In the current draft of C++23s flat_map design, the type flat_map::reference is defined as pair<const key_type&, mapped_type&>, i.e. it is not a reference to flat_map::value_type = pair<key_type, mapped_type>. (This seems to be mandatory, since the keys and values are stored not as pairs, but in two separate containers.) Thus, the iterators must be some proxy-class.

This makes me think: Is a flat_map actually even an STL container, similar to std::vector<bool> isn't one?


Solution

  • The standard defines what a "container" is in [container.reqmts]. Among these requirements is:

    typename X::reference

    • Result: T&

    So yes, std::flat_map is not a container.

    But it never claims to be; it is a "container adapter":

    A flat_­map is a container adaptor...

    Note that like std::stack/queue/priority_queue, std::flat_set/map take container types as template parameters.

    It should also be noted that, while they are not containers, they are ranges as defined by the C++20 range library's concepts. The C++20 ranges concepts allow for proxy ranges where the reference type need not be a value_type&.