stl2 icon indicating copy to clipboard operation
stl2 copied to clipboard

Precondition for `advance(i, n)`

Open morinmorin opened this issue 7 years ago • 2 comments

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, where itn is the iterator obtained by incrementing it by n.
  • For negative n, [itn, it) should be a valid range, where itn is the iterator obtained by decrementing it by |n|.

While the Standard too doesn't have this requirement, the SGI STL documentation has a similar requirement.

morinmorin avatar Jul 24 '17 11:07 morinmorin

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. If n < 0, then I models BidirectionalIterator and other stuff.

Where "other stuff" is one of:

  • [j, i) denotes a range where j is the result of decrementing i 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
  • ...?

CaseyCarter avatar Dec 13 '18 20:12 CaseyCarter

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"?

morinmorin avatar Jan 03 '19 15:01 morinmorin