3
\$\begingroup\$

I would like an opinion on my code. I need a random access iterator for my custom container. If I read the C++ 2011 standard, specially chapter 24, I understood that an implementation could be the following:

class my_iterator : public std::iterator<std::random_access_iterator_tag, my_element>
{
    private:
       my_container *_ref;
       my_iterator_state state; // some useful state
    public:
       my_iterator ();
       my_iterator (const my_iterator &);
       my_iterator (my_iterator &&);
       ~my_iterator ();
       my_iterator &operator = (const my_iterator &);
       my_iterator &operator = (my_iterator &&);
       reference operator * ();
       my_iterator &operator ++ ();
       bool operator != (my_iterator);
       value_type operator * ();
       pointer operator -> ();
       my_iterator &operator * ();
       void operator ++ (int);
       value_type operator ++ (int);
       const my_iterator &operator ++ (int);
       reference operator ++ (int);
       my_iterator &operator -- ();
       const my_iterator operator -- (int);
       reference operator -- (int);
       my_iterator &operator += (difference_type);
       my_iterator operator + (difference_type);
       my_iterator operator - (difference_type);
       reference operator [] (difference_type);
       bool operator < (my_iterator);
       bool operator > (my_iterator);
       bool operator <= (my_iterator);
       bool operator >= (my_iterator);
       friend difference_type (my_iterator, my_iterator);
       friend my_iterator operator + (difference_type, my_iterator);
       friend void swap (iterator &, iterator &);
};

void swap (iterator &, iterator &);
difference_type (my_iterator, my_iterator);
my_iterator operator + (difference_type, my_iterator);

The above implementation is correct and full-featured? There are too members?

\$\endgroup\$
6
\$\begingroup\$

Sure but:

Sure:

   my_iterator ();
   bool operator != (my_iterator);
   pointer            operator -> ();
   my_iterator&       operator ++ ();
   my_iterator&       operator -- ();
   my_iterator&       operator += (difference_type);
   my_iterator        operator +  (difference_type);
   my_iterator        operator -  (difference_type);
   reference          operator [] (difference_type);
   bool               operator < (my_iterator);
   bool               operator > (my_iterator);
   bool               operator <= (my_iterator);
   bool               operator >= (my_iterator);

But most of the above can be defined in terms of each other and are basically forwarding functions to one definition.

The default versions of these look like they should work. So why are you defining them:

   my_iterator (const my_iterator &);
   my_iterator (my_iterator &&);
   my_iterator &operator = (const my_iterator &);
   my_iterator &operator = (my_iterator &&);

Do you really need a destructor?

   ~my_iterator ();

Three different version of operator* that only differ by return type!
That does not look valid.

   reference    operator * ();
   value_type   operator * ();
   my_iterator& operator * ();

Lots of operator++(int) that only differ by return type.
So again does not look valid

   void               operator ++ (int);
   reference          operator ++ (int);
   value_type         operator ++ (int);
   const my_iterator& operator ++ (int);
   reference          operator ++ (int);

Lots of operator--(int) that only differ by return type.
So again does not look valid

   const my_iterator operator -- (int);
   reference         operator -- (int);

Look here for a definition of what you need to support.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.