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;
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:
any_iterator
(which implements boost::iterator_facade
) opaque_iterator
library (google for it), or any_iterator
s.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 :-)
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