简体   繁体   中英

Reason for restrictions of `reverse_iterator`

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 is std::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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM