What operations are defined for invalid iterators?

As an answer to the question regarding comparing invalid iterators , I tried to find the definition of allowed expressions for invalid iterators in the C ++ standard. A search for an "invalid iterator" finds only one link in Β§24.2.1.11. It says that invalid iterators can be "singular", but only states that their dereferencing can be undefined. No further semantics are specified.

One of the initial answers suggests that this is behavior defined by the implementation, but I think this cannot be accepted at all, because the above paragraph explicitly refers to UB.

This answer shows that "any other use of an invalid pointer value has an implementation-defined behavior." Since iterators for vectors are often implemented as pointers, I would say that comparing two invalid iterators is at least a behavior-driven implementation.

Can someone point me to the relevant sections in the standard where the semantics for invalid iterators are defined?

+3
source share
1 answer

Maybe [iterator.requirements.general, 24.2.1] / 6 contains what you are after?

The results of most expressions are undefined for singular values; the only exceptions are destroying an iterator that contains a singular value, assigning nonsingular values ​​to an iterator that contains a singular value, and for iterators satisfying the DefaultConstructible condition using the iterator initialized by the value as the source of the copy or move operation. [Note: this guarantee is not offered for initialization by default, although the difference only matters for types with trivial default constructors, such as pointers or aggregates containing pointers. - end note] In these cases, the singular value is overwritten just like any other value. Called values ​​are always nonsingular.

In addition, invalid iterators are defined in clause 11:

An invalid iterator is an iterator that can be singular.

Thus, invalid iterators are at least as limited as singular iterators; in particular, the only authorized operations are destruction and purpose.

+3
source

All Articles