I am in the process of making a custom iterator, but I am puzzled on
what makes the compiler not recognise the pointer
or reference
typedefs in the case of a templated iterator implementation.
The following code compiles fine:
#include <iterator>
struct It : std::iterator<std::random_access_iterator_tag, int>
{
int *v;
pointer operator-> () { return v; }
reference operator* () { return *v; }
};
but once I templatize my custom iterator I get errors:
#include <iterator>
template <class T>
struct It2 : std::iterator<std::random_access_iterator_tag, T>
{
T *v;
pointer operator-> () { return v; }
reference operator* () { return *v; }
};
error: 'pointer' does not name a type
note: (perhaps 'typename std::iterator<std::random_access_iterator_tag, T>::pointer' was intended)
error: 'reference' does not name a type
...
1> Why can't the compiler see the pointer
and reference
definitions contained in the std::iterator
?
According to the note
, it seems that I shouldn't have bothered using the std::iterator struct, but instead I should have manually copied the typedefs.
But that seems too error prone in case the iterator or iterator_traits get an extra typedef in the future.
2. How do you think I should deal with the definition of these traits (pointer
, reference
etc.) ?