c++iteratorreverse-iterator

Can I use reverse iterator as ForwardIt?


Based on this question. std::rotate is defined the following way:

template< class ForwardIt >
constexpr ForwardIt rotate( ForwardIt first, ForwardIt n_first, ForwardIt last );

Looking at the name ForwardIt, it expects forward iterator.

Question: assuming that my initial collection supports forward iterator (e.g. vector), can I use reverse iterators here? Why/why not?

Looking at, for example, http://www.cplusplus.com/reference/iterator/reverse_iterator/, I don't understand if what is returned be considered a forward iterator. Do I have to guarantee that reverse iterator satisfies forward iterator's properties? In this case, how can I check that it's true? vector::rbegin() documentation doesn't mention if it's the case.


Solution

  • The requirements for reverse_iterator are given in reverse.iter.requirements:

    The template parameter Iterator shall either meet the requirements of a Cpp17BidirectionalIterator ([bidirectional.iterators]) or model bidirectional_­iterator ([iterator.concept.bidir]).

    and the requirements for iterator.concept.bidir:

    The bidirectional_­iterator concept adds the ability to move an iterator backward as well as forward.

    So yes, a reverse_iterator can be used as a forward_iterator.