Is it possible to implement an any iterator with boost iterator facad开发者_开发技巧e? 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:
- becker's
any_iterator
(which implementsboost::iterator_facade
) - the
opaque_iterator
library (google for it), or - Adobe's very interesting poly library which contains a hierarchy of STL conforming
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 :-)
I think this may be what you're looking for:
any_iterator: Type Erasure for C++ Iterators
Here's a snippet from that page::
Overview
The class template any_iterator is the analog to boost::function for iterators. It allows you to have a single variable and assign to it iterators of different types, as long as these iterators have a suitable commonality.
精彩评论