Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AnyIterator and boost iterator facade

Tags:

c++

iterator

any

Is it possible to implement an any iterator with boost iterator facade? I don't want to define implementation details in my baseclass

class Base
{
public:
typedef std::vector<int>::iterator iterator;//implementation detail
...
virtual iterator begin()=0;
virtual iterator end()=0;
};

or do i have to write one completely from scratch;

like image 523
P3trus Avatar asked Feb 22 '11 17:02

P3trus


1 Answers

The code you've posted has fixed the type of iterators returned from Base and all it's implementantions to std::vector<int>::iterator which is probably not what you want. Jeremiah's suggestion is one way to go with one drawback: you loose compatibility with STL... I know of three implementations of a polymorphic iterator wrapper:

  1. becker's any_iterator (which implements boost::iterator_facade)
  2. the opaque_iterator library (google for it), or
  3. Adobe's very interesting poly library which contains a hierarchy of STL conforming any_iterators.

The problem is harder than it might seem... I made an attempt myself mainly because I needed covariance in any_iterators type argument (any_iterator<Derived> should be automatically convertible to any_iterator<Base>) which is difficult to implement cleanly with STL like iterators. A C# like Enumerator<T> is easier to implement(*) (and imho generally a cleaner concept than STL-like pairs of iterators) but again, you "loose" the STL.

(*) = without 'yield' of course :-)

like image 71
Paul Michalik Avatar answered Sep 22 '22 13:09

Paul Michalik