Is it defined to provide an inverted range to C++ standard algorithms?

后端 未结 5 647
南旧
南旧 2021-01-17 19:29

Consider standard algorithms like, say, std::for_each.

template
Function for_each(InputIterator first         


        
5条回答
  •  日久生厌
    2021-01-17 19:35

    The standard explicitly requires the last iterator to be reachable from the first iterator. That means that by incrementing first one should be able to eventually hit last.

    24.1 Iterator requirements

    ...

    6 An iterator j is called reachable from an iterator i if and only if there is a finite sequence of applications of the expression ++i that makes i == j. If j is reachable from i, they refer to the same container.

    7 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 one pointed to by i and up to but not including the one pointed to by j. Range [i, j) is valid if and only if j is reachable from i. The result of the application of functions in the library to invalid ranges is undefined.

提交回复
热议问题