1

In the following image I'm using the same type of iterators which means the same implementation of operator++ so how does the compiler know if it should get the next value or the previous one...?

enter image description here

  • The `operator++` overload is different for regular and reverse iterators. Just like you can overload `++` to do completely different things for two different classes. Try to implement an overload of `++` for some class that does what you would expect `--` to do, instead. Once you complete this task, the answer to your question should be very obvious to you. – Sam Varshavchik Jun 28 '20 at 21:35
  • @SamVarshavchik but the iterator in the same, there is no regular and reverse iterator, all are vector::iterator –  Jun 28 '20 at 21:36
  • If you actually tried it, you'd notice that it won't compile :-) – rustyx Jun 28 '20 at 21:38
  • This is a mistake in your textbook. [rbegin() and rend() do NOT return `iterator`s](https://en.cppreference.com/w/cpp/container/vector/rbegin). They return a `reverse_iterator`, a completely different class. Whichever textbook you got it from, throw it away and get a better textbook. – Sam Varshavchik Jun 28 '20 at 21:38
  • @SamVarshavchik should I tell my professor :-) –  Jun 28 '20 at 21:39
  • It's funny that it's one of the best universities in the field of CS in the world LOL –  Jun 28 '20 at 21:40
  • @SamVarshavchik do you work in the field of Cyber security testing? –  Jun 28 '20 at 21:44
  • C++ is hard to get right on paper. There are tons of bugs in even the best C++ resources. Sometimes the inaccuracies are caused by the author wanting to simplify the code in order to focus the attention on a specific aspect. Like if they used `auto`, that would solve the issue but might arguably make the code harder to understand. Note: there is a curated list of C++ resources available here: [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Anything outside it should be taken with a grain of salt. – rustyx Jul 01 '20 at 12:35

1 Answers1

1

First, the picture you have contains an error. As you can see here the reverse iterator type of vector is std::reverse_iterator<iterator> which uses the template std::reverse_iterator<T>. So begin and rbegin have not the same return value (and I also do not think they are convertible to each other).

But actually that wouldn't even be a problem, you could implement

struct iter {
    bool reverse;
    pointer ptr;
 
    iter& operator++() {
        if(reverse) --ptr;
        else ++ptr;
        return *this;
     }
};

But type-seperating them is really preferable.

n314159
  • 4,990
  • 1
  • 5
  • 20
  • As a note, reverse_iterator generally _is_ convertible to iterator, but not the other way. For example: std::vector v; auto rit {v.rbegin() }; auto it {rit}; compiled without complaint. Makes sense as reverse_iterator has the base() member which returns the corresponding iterator. – SoronelHaetir Jun 28 '20 at 23:01
  • That does only compile because you use auto and hence `it` is just a copy of `rit` and also a reverse iterator. Take `std::vector:: iterator it{rit};` and it will not compile. There are good reasons to not let iterators and reverse iterators mingle implicitly, e.g. `std::distance` would be weird, since it had to be the same in both directions which is counterintuitive. – n314159 Jun 29 '20 at 00:11