This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofCD1 status.
Section: 23.3.13.5[vector.modifiers]Status:CD1Submitter: Matt AusternOpened: 2003-08-19Last modified: 2016-01-28
Priority:Not Prioritized
View all otherissues in [vector.modifiers].
View all issues withCD1 status.
Discussion:
Consider the following code fragment:
int A[8] = { 1,3,5,7,9,8,4,2 };std::vector<int> v(A, A+8);std::vector<int>::iterator i1 = v.begin() + 3;std::vector<int>::iterator i2 = v.begin() + 4;v.erase(i1);Which iterators are invalidated byv.erase(i1): i1, i2,both, or neither?
On all existing implementations that I know of, the status of i1 andi2 is the same: both of them will be iterators that point to someelements of the vector (albeit not the same elements they didbefore). You won't get a crash if you use them. Depending on exactly what you mean by "invalidate", you might say that neither onehas been invalidated because they still point tosomething,or you might say that both have been invalidated because in bothcases the elements they point to have been changed out from under theiterator.
The standard doesn't say either of those things. It says that eraseinvalidates all iterators and references "after the point of theerase". This doesn't include i1, since it's at the point of theerase instead of after it. I can't think of any sensible definitionof invalidation by which one can say that i2 is invalidated but i1isn't.
(This issue is important if you try to reason about iterator validitybased only on the guarantees in the standard, rather than reasoningfrom typical implementation techniques. Strict debugging modes,which some programmers find useful, do not use typical implementationtechniques.)
Proposed resolution:
In 23.3.13.5[vector.modifiers] paragraph 3, change "Invalidates all theiterators and references after the point of the erase" to"Invalidates iterators and references at or after the point of theerase".
Rationale:
I believe this was essentially a typographical error, and that it was taken for granted that erasing an element invalidates iterators that point to it. The effects clause in question treats iterators and references in parallel, and it would seem counterintuitive to say that a reference to an erased value remains valid.