I have a problem when I'm reading SGI STL's implementation of list iterator.
template<class T>
struct __list_node {
void *prev;
void *next;
T data;
};
template<class T, class Ref, class Ptr>
struct __list_iterator {
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, Ref, Ptr> self;
...
typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef __list_node<T>* link_type;
...
link_type node;
...
reference operator*() const { return (*node).data; }
pointer operator-> const { return &(operator*()); }
self& operator++() { ... }
self operator++(int) { ... }
...
...
};
So, why are there three template arguments?
What if only class T
exists? Something likes below.
template<class T>
struct __list_iterator {
typedef __list_iterator<T> iterator;
typedef __list_iterator<T> self;
...
typedef T value_type;
typedef T* pointer;
typedef T& reference;
...
...
};
I wonder if three arguments is a must for some specific class T
or some reason I can't figure out.
Hope somebody could help me. Thanks a lot!