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?
Make it a member of the iterator class:
bool operator==( const interator& other ) const
{
return node == other.node;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With