Reputation: 6071
On the cppreference page of reverse_iterator I find the following remark
std::reverse_iterator
does not work with iterators that return a reference to a member object (so-called "stashing iterators"). An example of stashing iterator isstd::filesystem::path::iterator
.
Is that claim correct? And, if yes, why is that?
To me the restriction makes no sense, as I would assume that the reverse iterator basically swaps the operator ++
and operator --
(and stores the underlying iterator off by one).
EDIT: Apparently the question can be mis-understood:
I understand that we need the decrement operation once to implement a reverse iterator. The question is why this is not implemented during construction of the reverse_iterator
. Then the problem with the stashing iterator is avoided. But apparently this is not how it is done, and the decrement is done every time the iterator is dereferenced. Why?
Upvotes: 5
Views: 194
Reputation: 137310
and stores the underlying iterator off by one
That's the reason. You have to conjure a not-off-by-one iterator on dereference, and if destruction of the conjured iterator invalidates the reference obtained from it (as in the case of stashing iterators), then nasal demons.
Upvotes: 4