I'm defining an iterator type that does not store its current value explicitly. Instead, it's a wrapper around another iterator, and returns std::pair
s of the underlying iterator's values and another value. The relevant part of the class definition:
class MyIterator : public std::iterator<input_iterator, std::pair<Foo *, Bar *> > {
FooPtrIterator d_iter;
Bar d_bar;
public:
MyIterator(FooPtrIterator iter, Bar const &bar)
:
d_iter(iter),
d_bar(bar)
{ }
// Returning by value, not storing the pair itself.
value_type operator*() const {
return std::make_pair(*d_iter, &d_bar);
}
// Can't return by value!
??? operator->() const {
return ???;
}
};
Now this gets me in trouble when overloading operator->
, because I'm expected to return a pointer, or something else that itself supports operator->
.
I could simply store the current std::pair
as a class member:
value_type d_current;
However, that gets me in trouble in the constructor, because I cannot know at that point whether FooPtrIterator iter
is a valid iterator, so I cannot dereference it to provide d_current
with a value.
I can also make operator->
return a boost::shared_ptr<value_type>
but that is a terrible hack with more overhead than should be needed.
I get the feeling that this "generating" iterator is not supposed to exist. Is that so, or is there another way around this?