I was documenting a piece of code, writing that we need the chosen container to be a ReversibleContainer.
Reading in details the description for std::vector and std::basic_string:
- For
std::vectorand under its member types section (iterator), we read respectively:
std::vector(for T other thanbool) meets the requirements ofContainer,AllocatorAwareContainer(since C++11),SequenceContainer,ContiguousContainer(since C++17) andReversibleContainer.
LegacyRandomAccessIteratorandLegacyContiguousIteratortovalue_type
- For
std::basic_stringand under its member types section, we read:
std::basic_stringsatisfies the requirements ofAllocatorAwareContainer(except that customized construct/destroy are not used for construction/destruction of elements),SequenceContainerandContiguousContainer(since C++17).
LegacyRandomAccessIteratorandLegacyContiguousIteratortovalue_type
It seems to me std::basic_string meets all the requirements to be a ReversibleContainerthe same way std::vector does, yet is not described as such.
Not my question but I also do not understand why it is not described as a Container the same way std::vector is.
Any idea why it was like this?
The only thing I see in the reversible container requirements that isn't reflected in the specification of
std::basic_stringis that therbegin/rend/crbegin/crendmember functions lack the constant-time complexity requirement ([string.iterators]).std::basic_string's member functions have no such complexity guarantee.I am not sure whether this is intentional, since a
std::basic_stringhas a constant time-complexity requirement on itssizemember function ([string.capacity]). So astd::basic_stringimplementation should also always be able to obtainrbeginin constant time. @JanSchultke has opened a LWG issue to propose clarifying thatstd::basic_string's iterator functions have constant time complexity.Otherwise it seems to me that they are reversible containers and the proposed resolution for the LWG issue mentioned above would clarify that
std::basic_stringought to be a reversible container.