如何在自定义列表迭代器 class 中将迭代器转换为 const_iterator?

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

我正在尝试用 C++ 实现我自己的双向链表 (my_list) 代码,尤其是我的列表的迭代器 class。我的问题是我想要从迭代器到 const_iterator 的隐式转换,以便例如代码 my_list::iterator it = l.begin(); 编译,其中 lmy_list 的一个实例。但是,如果我的编译器没有抱怨,我找不到办法做到这一点。

这是实现列表节点和迭代器的代码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>;
};

我省略了 my_list 实现,但如果您认为相关,我可以将其包括在内。当我测试这段代码时,它不会在 GCC 上编译并出现以下错误:

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>&&’

有人可以帮我解决我做错的事吗?

这是一种方法:

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 {/*...*/}; }
};
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>node<const int> 是不相关的类型。您不能将指向 node<int> 的指针传递给需要指向 node<const int>.

指针的函数

您可以将 const 向上移动一级并在节点类型上对迭代器进行模板化,而不是在包含的类型上对迭代器 class 进行模板化:

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

现在您将指向 node<int> 的指针传递给需要指向 const node<int> 指针的函数,这很好。