37

I am new to C++ so please bear with me. I am trying to understand STL iterator_traits. In the book "The C++ Standard Library" the structure iterator_traits is defined as follows:

template <class T>
struct iterator_traits {
  typedef typename T::value_type value_type;
  typedef typename T::difference_type difference_type;
  typedef typename T::iterator_category iterator_category;
  typedef typename T::pointer pointer;
  typedef typename T::reference reference;
};

So it seems to me that it is re-exposing the subtypes that T already exposes. Moving ahead further, the book gives an example of how to use it, which is something like the following

template <class MyIterator>
void do_something(MyIterator start, MyIterator end) {
    typedef typename iterator_traits<MyIterator>::value_type value_type; 
    value_type v = *start;
    .....
}

My question is why do I need this iterator_traits structure here, if the idea was to obtain the value_type, couldn't I have obtained it from MyIterator directly ? My confusion seems to arise from my (surely incorrect) understanding that the information of the subtypes have to be sourced from the template <class T> used to instantiate the iterator_trait. So if you could explain, and preferably with an example why and where would I need iterator_traits that would be very helpful.

Evg
  • 25,259
  • 5
  • 41
  • 83
san
  • 4,144
  • 6
  • 32
  • 50
  • 4
    How much **new** are you ? Newbies generally don't play with `template`s. – iammilind Jul 19 '11 at 03:38
  • @iammilind Well I had done some C and Python before, so the class stuff was somewhat familiar. Templates not so, so this is where I am learning new things. – san Jul 19 '11 at 03:57
  • 17
    Well, if you use C++ full-time for 10 years, you'll still be learning new things about templates in 2021 :-) – James McNellis Jul 19 '11 at 04:15
  • 3
    Hi! I'm from 2021; I've been using C++ full-time for 10 years; and I can confirm that I'm still learning new things about templates. – Chip Hogg Sep 08 '21 at 18:00

1 Answers1

32

Pointers into an array can be used as random access iterators.

There needs to be some consistent way to get these types both for pointers (which obviously can't have the types declared as nested types, since only classes can have nested types) and for class-type iterators. The traits class template provides this consistent way.

The iterator_traits class template is specialized for pointers like so:

template <typename T>
struct iterator_traits<T*>
{
    typedef std::random_access_iterator_tag iterator_category;
    typedef T                               value_type;
    typedef T*                              pointer;
    typedef T&                              reference;
    typedef std::ptrdiff_t                  difference_type;
};
James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 8
    Ah! so the point is to make `do_something` work with pointers as well as class based iterators. Thanks. – san Jul 19 '11 at 04:02
  • Yep. Traits are useful for other reasons too (they are quite handy when using the [curiously recurring template pattern](http://stackoverflow.com/questions/5534759/), for example), but this is the specific reason that we have `iterator_traits`. – James McNellis Jul 19 '11 at 04:14
  • Also, if you use third-party iterator-like class that doesn't follow the protocol one can adapt it by specializing `std::iterator_traits`. – alfC Dec 04 '18 at 15:29