Can I easily override an (STL) iterator's category?
Right now, I have a class that can satisfy an API requirement with a
random-access iterator. However, I can envision a situation where the
implementation will change and only a forward iterator can be provided.
Therefore, I would like to restrict callers from using the random-access
functionality. I know I can write my own implementation (e.g.
restricted_bar_iterator), but was wondering if there is anything simpler
(i.e. requiring less coding).
class BAR { ... };
class FOO {
public:
// Bad...clients may expect 'bar_iterator' to be random access...
typedef std::vector<BAR>::iterator bar_iterator;
bar_iterator begin_bar() const;
bar_iterator end_bar() const;
// Possible solution here!
class restricted_bar_iterator :
public std::iterator< std::forward_iterator_tag, BAR > { ... };
};
void baz()
{
FOO foo;
bar_iterator it = foo.begin_bar() + 5; // want a compile time error here!
}
No comments:
Post a Comment