278. What does iterator validity mean?

Section: [list.ops] Status: CD1 Submitter: P.J. Plauger Opened: 2000-11-27 Last modified: 2016-02-10

Priority: Not Prioritized

View all other issues in [list.ops].

View all issues with CD1 status.


Section [list.ops] states that

  void splice(iterator position, list<T, Allocator>& x);

invalidates all iterators and references to list x.

But what does the C++ Standard mean by "invalidate"? You can still dereference the iterator to a spliced list element, but you'd better not use it to delimit a range within the original list. For the latter operation, it has definitely lost some of its validity.

If we accept the proposed resolution to issue 250, then we'd better clarify that a "valid" iterator need no longer designate an element within the same container as it once did. We then have to clarify what we mean by invalidating a past-the-end iterator, as when a vector or string grows by reallocation. Clearly, such an iterator has a different kind of validity. Perhaps we should introduce separate terms for the two kinds of "validity."

Proposed resolution:

Add the following text to the end of section 99 [iterator.concepts], after paragraph 5:

An invalid iterator is an iterator that may be singular. [Footnote: This definition applies to pointers, since pointers are iterators. The effect of dereferencing an iterator that has been invalidated is undefined.]

[post-Copenhagen: Matt provided wording.]

[Redmond: General agreement with the intent, some objections to the wording. Dave provided new wording.]


This resolution simply defines a term that the Standard uses but never defines, "invalid", in terms of a term that is defined, "singular".

Why do we say "may be singular", instead of "is singular"? That's becuase a valid iterator is one that is known to be nonsingular. Invalidating an iterator means changing it in such a way that it's no longer known to be nonsingular. An example: inserting an element into the middle of a vector is correctly said to invalidate all iterators pointing into the vector. That doesn't necessarily mean they all become singular.