In various places in the c++ Standard, the phrase:
Requires: [p, p + len) is a valid range.
(where p is a pointer, and len is a number) is used.
An alternate version for iterators is:
Requires: [first, last) is a valid range.
What does this mean?
I think the relevant part of the C++17 Standard is here
27.2.1 In general [ iterator.requirements.general ]
...
8 An iterator
jis called reachable from an iteratoriif and only if there is a finite sequence of applications of the expression++ithat makesi == j. Ifjis reachable fromi, they refer to elements of the same sequence.9 Most of the library’s algorithmic templates that operate on data structures have interfaces that use ranges. A range is a pair of iterators that designate the beginning and end of the computation. A range
[i, i)is an empty range; in general, a range[i, j)refers to the elements in the data structure starting with the element pointed to byiand up to but not including the element pointed to byj. Range[i, j)is valid if and only ifjis reachable fromi. The result of the application of functions in the library to invalid ranges is undefined.
So extracting the pertinent parts:
8 An iterator
jis called reachable from an iteratoriif and only if there is a finite sequence of applications of the expression++ithat makesi == j...9 ... Range
[i, j)is valid if and only ifjis reachable fromi....
So it seems that a "valid range" is a pair of iterators that belong to the same container (or array), with j being farther along than i.
But also not forgetting:
in general, a range
[i, j)refers to the elements in the data structure starting with the element pointed to byiand up to but not including the element pointed to byj.
So all of the iterators between i and j (but not necessarily including j) must be "in the data structure" (ie. not out-of-bounds).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With