Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterator equality

I'm creating a container class which implements a double linked list.

template <class T>
class dl_list {
public:
    class link {
    public:
        T* data;
        link *prev, *next;
    };
    class iterator {
        link* node;
    public:
        link* get_node() { return node; }
        // ++, --, * operators, etc.
    };
    // other stuff
};

Pretty neat, I'm having fun with it. But one problem I'm having is when I define my equality operators for the iterator type, I have to do a template specialization.

template <class T>
bool operator==(typename dl_list<T>::iterator& lhv, typename dl_list<T>::iterator rhv) {
    return lhv.get_node() == rhv.get_node();
}

will not work, I have to specialize it like so:

bool operator==(typename dl_list<int>::iterator& lhv, typename dl_list<int>::iterator rhv) {
    return lhv.get_node() == rhv.get_node();
}

for every type I want to use it for, which is annoying for obvious reasons. How do I get around this?

like image 391
Michael Dorst Avatar asked Oct 27 '25 07:10

Michael Dorst


1 Answers

Make it a member of the iterator class:

bool operator==( const interator& other ) const
{
    return node == other.node;
}
like image 128
Daniel Frey Avatar answered Oct 29 '25 21:10

Daniel Frey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!