2

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?

Community
  • 1
  • 1
Mikhail
  • 20,685
  • 7
  • 70
  • 146

1 Answers1

1

Have you tried to do something like this:

template<class IteratorBase>
class cycle_iterator : public  // (...) 
{
    // (...)
    operator IteratorBase() {
        return base_reference();
    }
};
Pawel Zubrycki
  • 2,703
  • 17
  • 26
  • It can handle only the case `it V cit`, where V is some comparison operator, but not the assignment or even `cit V it` case. – Mikhail Mar 11 '12 at 20:28
  • Well, after all, I decided to add this conversion operator along with assignment operator, and not to compare `cyclic_itertor` with raw iterators at all since it might by error-prone. So I'll accept your answer :) – Mikhail Mar 15 '12 at 16:48