Iterator category

In code:

//I know that to get this effect (being able to use it with std algorithms) I can inherit like I did in line below:

    class Iterator //: public std::iterator<std::bidirectional_iterator_tag,T>

    {
    private:
        T** itData_;
    public:
        //BUT I WOULD LIKE TO BE ABLE TO DO IT BY HAND AS WELL
        typedef std::bidirectional_iterator_tag iterator_category;
        typedef T* value_type;//SHOULD IT BE T AS value_type or T*?
        typedef std::ptrdiff_t difference_type;
        typedef T** pointer;//SHOULD IT BE T* AS pointer or T**?
        typedef T*& reference;//SHOULD IT BE T& AS reference or T*&?
};

      

Basically, I am asking if I have a variable of type T ** in the iterator class, is it a correct assumption that the value type for this iterator would be T *, etc., as I described in the comments in the code, in the following to the relevant lines.
Thanks.

+2


a source to share


2 answers


Definition in the standard (section 24.3.2):

template<class Category, class T, class Distance = ptrdiff_t,
         class Pointer = T*, class Reference = T&>
struct iterator {
    typedef T value_type;
    typedef Distance difference_type;
    typedef Pointer pointer;
    typedef Reference reference;
    typedef Category iterator_category;
};

      

As you can see, the defaults are:



typedef T value_type;
typedef ptrdiff_t difference_type;
typedef T* pointer;
typedef T& reference;

      

It is assumed that the iteration is over a container of type items T

. If your container contains type elements T*

instead, the typedefs in your question will be correct.

+3


a source


You must define them the way you want. pointer

and reference

are (the same) as the return types of the dereferencing operators that you define for your iterator class (i.e. operator->()

and operator*()

respectively), so what you want those operators to return may be how you want to define those typedefs.



In the comment, you are assuming that if you inherited from std::iterator

, it would be from std::iterator<std::bidirectional_iterator_tag,T>

. You can look in the standard (as in interjay's answer) or in your header files to see exactly what values ​​will be provided, which will show you what you want T

, T*

and T&

accordingly, in the same way as what it might provide.

+2


a source







All Articles