Files
cppdraft_translate/cppdraft/range/sized.md
2025-10-25 03:02:53 +03:00

74 lines
3.2 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[range.sized]
# 25 Ranges library [[ranges]](./#ranges)
## 25.4 Range requirements [[range.req]](range.req#range.sized)
### 25.4.4 Sized ranges [range.sized]
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L1467)
The [sized_range](#concept:sized_range "25.4.4Sized ranges[range.sized]") concept
refines [approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3Approximately sized ranges[range.approximately.sized]") with
the requirement that the number of elements in the range can be determined
in amortized constant time using ranges::size[.](#1.sentence-1)
[🔗](#concept:sized_range)
`template<class T>
concept [sized_range](#concept:sized_range "25.4.4Sized ranges[range.sized]") =
[approximately_sized_range](range.approximately.sized#concept:approximately_sized_range "25.4.3Approximately sized ranges[range.approximately.sized]")<T> && requires(T& t) { ranges::size(t); };
`
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L1480)
Given an lvalue t of type remove_reference_t<T>, T models [sized_range](#concept:sized_range "25.4.4Sized ranges[range.sized]") only if
- [(2.1)](#2.1)
ranges::size(t) is amortized O(1), does not modify t,
and is equal to ranges::distance(ranges::begin(t), ranges::end(t)), and
- [(2.2)](#2.2)
if iterator_t<T> models [forward_iterator](iterator.concept.forward#concept:forward_iterator "24.3.4.11Concept forward_­iterator[iterator.concept.forward]"),ranges::size(t) is well-defined regardless of the evaluation ofranges::begin(t)[.](#2.sentence-1)
[*Note [1](#note-1)*:
ranges::size(t) is otherwise not required to be
well-defined after evaluating ranges::begin(t)[.](#2.2.sentence-2)
For example, it is possible for ranges::size(t) to be well-defined
for a [sized_range](#concept:sized_range "25.4.4Sized ranges[range.sized]") whose iterator type
does not model [forward_iterator](iterator.concept.forward#concept:forward_iterator "24.3.4.11Concept forward_­iterator[iterator.concept.forward]") only if evaluated before the first call to ranges::begin(t)[.](#2.2.sentence-3)
— *end note*]
[🔗](#lib:disable_sized_range)
`template<class>
constexpr bool disable_sized_range = false;
`
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L1508)
*Remarks*: Pursuant to [[namespace.std]](namespace.std "16.4.5.2.1Namespace std"),
users may specialize disable_sized_range for cv-unqualified program-defined types[.](#3.sentence-1)
Such specializations shall
be usable in constant expressions ([[expr.const]](expr.const "7.7Constant expressions")) and
have type const bool[.](#3.sentence-2)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/ranges.tex#L1517)
[*Note [2](#note-2)*:
disable_sized_range allows use of [range](range.range#concept:range "25.4.2Ranges[range.range]") types with the library
that satisfy but do not in fact model [sized_range](#concept:sized_range "25.4.4Sized ranges[range.sized]")[.](#4.sentence-1)
— *end note*]