1

When looking up the type of std::set's member type iterator I was expecting a bidirectional iterator to its value_type const. However, according to cppreference.com it has the following member types:

Member type Definition
... ...
iterator Constant LegacyBidirectionalIterator to value_type
const_iterator LegacyBidirectionalIterator to const value_type
... ...

In practice both are likely to be the same type, but what is the definitional difference between these formulations?

LegacyForwardIterator (which is a base of LegacyBidirectionalIterator) seems to differentiate between LegacyForwardIterator and mutable LegacyForwardIterator implying the the former is somehow not mutable, i.e. const. However std::forward_list references also LegacyForwardIterator without mention of the word mutable:

Member type Definition
iterator LegacyForwardIterator to value_type

Is cppreference incomplete or wrong on this account or what is going on? What is the difference between "Constant LegacyBidirectionalIterator to value_type" and "LegacyBidirectionalIterator to const value_type"?

bitmask
  • 32,434
  • 14
  • 99
  • 159
  • Sparked by trying to answer [Why range based for loop doesn't work with iterator, which points to container?](https://stackoverflow.com/q/74925546/430766). – bitmask Dec 27 '22 at 06:05

1 Answers1

0

what is the definitional difference between these formulations?

From my point of view, there's little difference. The sayings are to be consistent with the descriptions of member types of other containers. i.e. iterator refers to xxxIterator to value_type, and const_iterator refers to xxxIterator to const value_type(it's already implicitly constant). e.g. [std::map].(https://en.cppreference.com/w/cpp/container/map#Member_types), std::vector

The extra Constant in std::set::iterator comes from associative.reqmts#general-6

For associative containers where the value type is the same as the key type, both iterator and const_­iterator are constant iterators. It is unspecified whether or not iterator and const_­iterator are the same type.

Since it's mentioned these two member types are not necessarily to be the same, it's totally fine for cppref to make such literally different wordings (although it's usually the same, also mentioned by std::set::begin)

As to the word mutable, an iterator is either constant or mutable. It's used only when really necessary, so it doesn't mean an iterator without a leading mutable is constant.

Nimrod
  • 2,908
  • 9
  • 20