stl2
stl2 copied to clipboard
Precondition for `advance(i, n)`
In [iterator.operations] advance
does not have a requirement on iterator validity, but I think something like this is needed:
- For non-negative
n
,[it, itn)
should be a valid range, whereitn
is the iterator obtained by incrementingit
byn
. - For negative
n
,[itn, it)
should be a valid range, whereitn
is the iterator obtained by decrementingit
by|n|
.
While the Standard too doesn't have this requirement, the SGI STL documentation has a similar requirement.
For [range.iter.op.advance]/1 (the "i, n" overload) the precondition we have now, " If I
does not model BidirectionalIterator
, n
is not negative." is sufficient to support purely operational "evaluates these expressions and means whatever that means" semantics. But it's not terribly useful. It could be improved to:
If n > 0
, then[i, n)
denotes a range. Ifn < 0
, thenI
modelsBidirectionalIterator
and other stuff.
Where "other stuff" is one of:
-
[j, i)
denotes a range wherej
is the result of decrementingi
by-n
-
i
is decrementable, as are its-n - 1
predecessors [what's a "predecessor"?] - there exists an iterator
j
such that[j,-n)
and[j,i)
denote the same range [what does "the same range" mean?] - there exists an iterator
j
such that[j,-n)
denotes a range and(advance(j, -n), j) == i
- ...?
The STI doc (mirror site) does not use interval notation there and provides a concise description (which is not divided into positive and negative cases):
Preconditions
- Every iterator between i and i+n (inclusive) is nonsingular.
- …
But I think more descriptive wording (like Casey's) would be better suited for the Standard.
BTW, is there any guideline on which one to use "a valid range" or "a range"?