Topic: Problems interpreting requirement for std::copy (25.2.1)
Author: jpotter@lhup.edu (John Potter)
Date: Fri, 16 Sep 2005 02:26:18 GMT Raw View
On Wed, 14 Sep 2005 23:23:03 GMT, AlbertoBarbati@libero.it (Alberto
Ganesh Barbati) wrote:
> kanze wrote:
> > =A723.2.23/1, in the decription of insert, push_front and
> > push_back: "Does not affect the validity of iterators and
> > references."
> > I don't find an exact definition in the standard as to what it
> > means by validity, but from the actual use, I think it means
> > that the iterator continues to designate the same element (or in
> > the case of end(), it continues to designate an element one past
> > the end).
> I agree that the statement means that a valid past-the-end iterator
> remains valid after a push_back, but it's not obvious to me that it
> should remain a past-the-end iterator. One could implement a
> past-the-end iterator in such a way that, after a push_back, it points
> to (newly added) last element and I don't see how such implementation
> would be violating the standard. Of course I may be missing something.
A look at vector may help. With size less than capacity, push_back is
the same as insert end and invalidates any end iterator. The old
iterator will point to the newly added element unless the
implementation goes out of its way to modify the old iterator, but it
is invalid. For list, it is still valid. Conclusion, it is still an
end iterator.
For validity amusement:
vector<int> v(3, 42);
v.reserve(10);
vector<int>::iterator b(v.begin()), m(b + 1), l(m + 1), p(l + 1);
v.insert(l, 24);
// l and p are now invalid
// b and m are still valid
v.push_back(13);
// b and m are still valid
rotate(m, v.end() - 1, v.end());
// b and m are still valid
The result is the same as v.insert(m, 13), except that it does
not invalidate any iterators other than end.
Does that explain why the standard does not define validity?
John
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]