Search code examples
c++iteratortype-traitsstl-algorithm

Shouldn't iterator_traits<vector<bool>::iterator>::iterator_category be input_iterator_tag?


I recently learned that ForwardIterators require operator * to return by reference, which means that iterators that return proxies, such as std::vector<bool>, cannot be ForwardIterators ([forward.iterators]).

However, I tested std::iterator_traits<std::vector<bool>::iterator>::iterator_category in different compilers (G++, Clang, MSVC) and it was always std::random_access_iterator_tag. Is that consistent with the above requirement?


Solution

  • No, it is not. That's part of the big issue with vector<bool>.

    The standard contradicts itself. It says that vector's iterators are random access, but defines vector<bool> in such a way that its iterators don't fulfill the requirements of RandomAccessIterator.

    This ancient paper by Herb Sutter explains this and other downsides: http://www.gotw.ca/publications/N1185.pdf