Martin said:
O.k sorry for being a bit short.
I am currently implementing the iterator design pattern, from
the book "design patterns" (gamma etc). In the implementation
section for this pattern, it is mentioned that there are many
ways to implement a so called "robust iterator". This iterator
works correct even if delete and insert operators are applied
to the aggregate. Since there are many ways one could implement
this, I was wondering if anyone knows of a good example
for this problem.
Even this is a bit on the vague side. The std::list has iterators
which are robust, in the sense that insert never affects such
iterators, and deletion affects only iterators refering to the
deleted item.
Now, it is possible to define an even more robust form of
list iterators, such that deletion of a node will also safely
invalidate the iterators to that node. This requires
registration of the iterators with the node (registration
with the list itself makes deletion O(N) ).
With this registration, it also is possible to invalidate
iterators when the entire list is deleted. This adds yet more
overhead (primarily time, can reuse the per-node data).
Another aspect of robustness is how dereferencing an invalid
iterator fails. The standard makes this UB, but a robust
iterator would probably throw an exception. Other
implementations exist, e.g. an iterator whose value_type
is double may return NaN instead. There may even be situations
in which it makes sense to return iterator::value_type();
Regards,
Michiel Salters