How do I convert iterator to const_iterator in my custom list iterator class?

…衆ロ難τιáo~ 提交于 2019-12-13 03:18:16

问题


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?


回答1:


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.




回答2:


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 {/*...*/}; }
};


来源:https://stackoverflow.com/questions/57718018/how-do-i-convert-iterator-to-const-iterator-in-my-custom-list-iterator-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!