28

Regarding this question on iterator invalidation rules, it seems obvious that the spirit of the standard means, for example, that "an erase in the middle of the deque invalidates all the iterators and references to elements of the deque" also refers to the end iterator.

However, I can't find anywhere that the standard makes this explicit, and strictly speaking the end iterator is not an iterator to an element in the container.

Does the 2003 standard make this clear somewhere?

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

1 Answers1

9

For example, 23.1/10:

no swap() function invalidates any references, pointers, or iterators referring to the elements of the containers being swapped. [ Note: The end() iterator does not refer to any element, so it may be invalidated. —end note ]

I do not know if we can be certain that iterator referring to an element has been used consistently in the Standard to exclude end iterators :/

As said in a comment, I suppose this is to allow end iterators pointing to sentinel values within the container.

For example, a typical doubly linked List implementation is to create a Node structure, and have one Node by value within the List to act as the end node.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • 2
    I'm satisfied, through this answer and through the relevant comments, that C++03 is ambiguous in this matter. Of course, in practice, we know that we should treat past-the-end iterators in the same manner as iterators to container elements; however, this answer is the answer to _this_ question. – Lightness Races in Orbit Jun 22 '11 at 23:27
  • Actually cppreference.com does talk about end() iterator's invalidation. See https://stackoverflow.com/a/72624474/199150 – Shriram V Jun 14 '22 at 23:42
  • Interesting; though not authoritative. I haven't followed the situation to know if requirements were tightened in newer standard versions (post C++11). – Matthieu M. Jun 15 '22 at 09:40