In order to read properties of an iterator, the STL class std::iterator_traits shall be utilized, not the raw iterator type.
Correct: using Category = std::iterator_traits<Iterator>::iterator_categoryIncorrect: using Category = Iterator::iterator_category;
Let's say we want to implement a template function called iterator_distance(), equivalent of std::distance(), which returns the number of steps between two iterators:
- If the iterator category is random access we simply subtract the difference between the iterator a and b.
- Otherwise, we have to calculate the number of steps from iterator a to iterator b.
Using iterator_category tag and difference_type, the distance function is implemented ...