Title
On vector iterator invalidation
Status
nad
Section
[vector.modifiers]
Submitter
Howard Hinnant

Created on 2013-04-29.00:00:00 last changed 94 months ago

Messages

Date: 2017-03-15.00:00:00

[ 2017-03-04, Kona ]

NAD. "Works as designed" Also, the example does not work in today's world of launder.

Date: 2017-02-02.00:41:18

[ 2016-05 Issues Telecon ]

This is related to 2698

Date: 2013-04-29.00:00:00

[vector.modifiers]/p3 says:

iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);

Effects: Invalidates iterators and references at or after the point of the erase.

Consider this example:

#include <vector>
#include <cassert>

int main()
{
  typedef std::vector<int> C;
  C c = {1, 2, 3, 4};
  C::iterator i = c.begin() + 1;
  C::iterator j = c.end() - 1;
  assert(*i == 2);
  assert(*j == 4);
  c.erase(c.begin());
  assert(*i == 3); // Why is this not perfectly fine?!
}

Why has the iterator i been invalidated? It still refers to a perfectly reasonable, fully constructed object. If vector::iterator were to be implemented as a pointer (which is legal), it is not possible for that last line to do anything but run fine.

The iterator j on the other hand now points at end, and any iterators that may now point beyond end(), into uninitialized memory, are clearly invalid.

But why do we say that an iterator that must point to a valid object is invalid? This looks to me like we simply got sloppy in our specification.

History
Date User Action Args
2017-03-14 03:14:09adminsetmessages: + msg9100
2017-03-14 03:14:09adminsetstatus: new -> nad
2016-05-22 15:38:38adminsetmessages: + msg8131
2013-04-29 00:00:00admincreate