sized-random-access-range
From cppreference.com
template< class T >
concept /*sized-random-access-range*/ =
ranges::random_access_range<T> &&
ranges::sized_range<T>;
|
(since C++26) (exposition only*) |
|
The exposition-only concept sized-random-access-range specifies the requirements of a range type that knows its size in constant time and allows random access to its elements.
Notes
sized-random-access-range is used by parallel range algorithms to reject unbounded range argument. The reasons for that are:
- For efficient parallel implementation, the iteration space bounds need to be known. Otherwise, it is hard to apply the “divide and conquer” strategy for creating work for multiple execution threads.
- While serial range algorithms allow passing an “infinite” range like
std::ranges::views::iota(0), it may result in an endless loop. It is hard to imagine usefulness of that in the case of parallel execution. Requiring data sequences to be bounded potentially prevents errors at run-time. - Using explicitly bounded output ranges follows established practices of secure coding, which recommend or even require to always specify the size of the output in order to prevent out-of-range data modifications.
- If several input ranges or sequences are bounded, an algorithm should usually stop as soon as the end is reached for the shortest one. There are already precedents in the standard that an algorithm takes two sequences with potentially different input sizes and chooses the smaller size as the number of iterations it is going to make, such as std::ranges::transform and std::ranges::mismatch.
- As the output data sequences are bounded, they are no more assumed to have sufficient space for all the input. There is thus a possibility that some elements of the input sequences are not processed by an algorithm, and it makes sense for it to return iterators to the positions in these sequences where the processing stopped.