Am 04.03.2011 20:20, schrieb backminator:
Hi. Below is a small example that puzzles me.
I have tried it on g++ 4.2.4 and cl.exe (VS2010: 16.00.31118.01), same
result.
It shows how an iterator is modified, by inserting an element into a
list.
But only if a reverse_iterator is used. Not with an ordinary iterator.
Could someone tell me if this is part of the standard?
I get the feeling that it is contradicting with the following:
http://www.sgi.com/tech/stl/List.html:
"the important property that insertion and splicing do not invalidate
iterators to list elements"
I'm not convinced that your example demonstrates that the iterator
values has been "invalidated". To proof that we must test our
expectations with experiments that make these expectations observable.
First we need to start with the definition of a valid iterator. We check
the current standard and find - nothing. At least no clear answer to
that question. There is an issue that is concerned about this lack of
definition, see:
http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-active.html#1213
In absence of such a definition it might help to compare with existing
definitions, e.g. that of the pre-standard library and we can look at
the SGI definition shown in
http://www.sgi.com/tech/stl/InputIterator.html
which says:
"An iterator is valid if it is dereferenceable or past-the-end."
I see nothing within this definition that guarantees that repeated calls
of the expression *it will return the same value when during that time
the underlying container is modified. Thuis I don't think that your
observation that the result of this expression is different with an
intervening mutating container proofs that the iterator has been
invalidated. Looking at the definition of reverse_iterator's operator*
we find that the return value is that of a decrementing a copy of the
current iterator and returning the value of that copy. Since a new value
has been inserted to the point where we are decrementing the copy, we
obtain the expected value after the modification of the container.
the former vector::end iterator. This doesn't sound right, and is not
how the term "invalidate" is used in the current standard.
element (or past-the-end)". IMHO, better definitions and terms