The standard says that iterators and references remain valid during an insert. It doesn’t say anything about reverse iterators. 🙂
The reverse_iterator
returned by rbegin()
internally holds the value of end()
. After a push_back()
this value will obviously not be the same as it was before. I don’t think the standard says what it should be. Obvious alternatives include the previous last element of the list, or that it stays at the end if that is a fixed value (like a sentinel node).
Technical details: The value returned by rend()
cannot point before begin()
, because that is not valid. So it was decided that rend()
should contain the value of begin()
and all other reverse iterators be shifted one position further. The operator*
compensates for this and accesses the correct element anyway.
First paragraph of 24.5.1 Reverse iterators says:
Class template
reverse_iterator
is an iterator adaptor that iterates from the end of the sequence defined
by its underlying iterator to the beginning of that sequence. The fundamental relation between a reverse
iterator and its corresponding iterator i is established by the identity:
&*(reverse_iterator(i)) == &*(i - 1)
.