0
votes

I am trying to implement my own doubly-linked list (my_list) code in C++, and in particular an iterator class for my list. My problem is I want to have an implicit conversion from iterator to const_iterator so that for example the code my_list::iterator it = l.begin(); where l is an instance of my_list compiles. However, I cannot find a way to do this without my compiler complaining.

Here is the code implementing the list nodes and the iterator class:

template<class T> class node {
    node(const T& t = T()):data(t),next(0),prev(0) {}
    T data;
    node* next;
    node* prev;

    friend class my_list<T>;
    friend class my_list_iterator<T>;
};

template<class T> class my_list_iterator {
    public:
            // increment and decrement operators
            my_list_iterator operator++();
            my_list_iterator operator++(int);
            my_list_iterator operator--();
            my_list_iterator operator--(int);

            // bool comparison iterators
            bool operator==(const my_list_iterator& other) const {return pos_==other.pos_;}
            bool operator!=(const my_list_iterator& other) const {return pos_!=other.pos_;}

            // member access
            T& operator*() const {return pos_->data;}
            T* operator->() const {return &(pos_->data);}

            // implicit conversion to const iterator
            operator my_list_iterator<const T>() {return my_list_iterator<const T>(pos_);}
    private:
            node<T>* pos_;
            explicit my_list_iterator(node<T>* p=0):pos_(p) {}
            friend class my_list<T>;
};

I have omitted the my_list implementation but if you think it is relevant I can include it. When I test this code, it won't compile on GCC with the following error:

In file included from test.cpp:2:
my_list.h: In instantiation of ‘my_list_iterator<T>::operator my_list_iterator<const T>() [with T = int]’:
test.cpp:12:49:   required from here
my_list.h:37:48: error: no matching function for call to ‘my_list_iterator<const int>::my_list_iterator(node<int>*&)’
   operator my_list_iterator<const T>() {return my_list_iterator<const T>(pos_);}
                                                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
my_list.h:40:12: note: candidate: ‘my_list_iterator<T>::my_list_iterator(node<T>*) [with T = const int]’
   explicit my_list_iterator(node<T>* p=0):pos_(p) {}
            ^~~~~~~~~~~~~~~~
my_list.h:40:12: note:   no known conversion for argument 1 from ‘node<int>*’ to ‘node<const int>*’
my_list.h:20:25: note: candidate: ‘constexpr my_list_iterator<const int>::my_list_iterator(const my_list_iterator<const int>&)’
 template<class T> class my_list_iterator {
                         ^~~~~~~~~~~~~~~~
my_list.h:20:25: note:   no known conversion for argument 1 from ‘node<int>*’ to ‘const my_list_iterator<const int>&’
my_list.h:20:25: note: candidate: ‘constexpr my_list_iterator<const int>::my_list_iterator(my_list_iterator<const int>&&)’
my_list.h:20:25: note:   no known conversion for argument 1 from ‘node<int>*’ to ‘my_list_iterator<const int>&&’

Can someone help me with what I'm doing wrong?

2
To confirm, you want an iterator that cannot modify the object it represents, yes? - user4581301
Good fix, Miles. Didn't notice that. - user4581301
This is usually done with a different type for the iterator, not the same iterator with a different contained type. In the standard library this is performed with iterator and const_iterator, both templated around the same type. - user4581301
@user4581301 Yes exactly. I want a read-only access iterator, exactly the way std::list<T>::const_iterator would behave. As to your last comment, do I really need to implement the code twice? I am basically trying to create an implicit conversion from iterator<T> to iterator<const T>, isn't that possible? - John

2 Answers

2
votes
my_list.h:40:12: note: candidate: ‘my_list_iterator<T>::my_list_iterator(node<T>*) [with T = const int]’
   explicit my_list_iterator(node<T>* p=0):pos_(p) {}
            ^~~~~~~~~~~~~~~~
my_list.h:40:12: note:   no known conversion for argument 1 from ‘node<int>*’ to ‘node<const int>*’

A node<int> and a node<const int> are unrelated types. You cannot pass a pointer to a node<int> to a function that expects a pointer to a node<const int>.

Instead of templating your iterator class on the contained type you could move the const up one level and template your iterator on the node type:

template<class Node> class my_list_iterator {
public:

    //...

    // member access
    auto& operator*() const {return pos_->data;}
    auto* operator->() const {return &(pos_->data);}

    // implicit conversion to const iterator
    operator my_list_iterator<const Node>() {return my_list_iterator<const Node>{pos_};}
private:
    Node* pos_;
    explicit my_list_iterator(Node* p=0):pos_(p) {}
    friend class my_list<type>;
};

template <class T> class my_list {
public:
    using iterator = my_list_iterator<node<T>>;
    using const_iterator = my_list_iterator<const node<T>>;
    //...
};

Live Demo

Now you're passing a pointer to a node<int> to a function expecting a pointer to a const node<int>, and that's fine.

1
votes

This is one way to do it:

template <typename T>
class my_list {
public:
    using iterator = my_list_iterator<T>;
    using const_iterator = my_list_iterator<const T>;

    const_iterator cbegin() const { return {/*...*/}; }
    const_iterator cend() const { return {/*...*/}; }
    const_iterator begin() const { return {/*...*/}; }
    const_iterator end() const { return {/*...*/}; }
    iterator begin() { return {/*...*/}; }
    iterator end() { return {/*...*/}; }
};