Mikhail
Mikhail

Reputation: 21809

How to properly use boost::iterator_adaptor for making cycling iterator?

I'm trying to implement cycling iterator, which is quite useful in my problem. According to this thread, I'm using boost::iterator_adaptor for this purpose, and iterator itself works fine. Implementation is much like in this answer.

However, there is some problem when operating both cycle_iterator and "native" IteratorBase in the same sentence, like this:

vector<int> v;
vector<int>::iterator it = v.begin();
cyclic_iterator<vector<int>::iterator> cit(v.begin(), v.end());

if (cit != it)  // Don't compile
{
  ...
}

Compiler generates error C2678: binary '!=' : no operator found which takes a left-hand operand of type 'cyclic_iterator' (or there is no acceptable conversion).

I can resolve it explicitly specifying operator!= in cyclic_iterator for IteratorBase. However, I need to explicitly overload operator==, operator= and so on.

Is there some more convenient way to make this stuff work?

Upvotes: 2

Views: 655

Answers (1)

Pawel Zubrycki
Pawel Zubrycki

Reputation: 2713

Have you tried to do something like this:

template<class IteratorBase>
class cycle_iterator : public  // (...) 
{
    // (...)
    operator IteratorBase() {
        return base_reference();
    }
};

Upvotes: 1

Related Questions