On this page
std::ranges::sized_range, std::ranges::disable_sized_range
Defined in header <ranges> |
||
|---|---|---|
|
(1) | (since C++20) |
|
(2) | (since C++20) |
1) The
sized_range concept specifies the requirements of a range type that knows its size in constant time with the size function.
2)
disable_sized_range exists to allow use of range types that provide a size function (either as a member or as a non-member) but do not in fact model sized_range. Users may specialize disable_sized_range for cv-unqualified program-defined types. Such specializations shall be usable in constant expressions and have type const bool.
Semantic requirements
1) Given an lvaluet of type std::remove_reference_t<T>, T models sized_range only if
ranges::size(t)- has amortized constant-time complexity,
- does not alter the value of
tin a manner observable to equality-preserving expressions, and - is equal to
ranges::distance(ranges::begin(t), ranges::end(t)), and - if
ranges::iterator_t<T>modelsforward_iterator,ranges::size(t)is well-defined regardless of the evaluation ofranges::begin(t)(in other words, a single-pass sized range may support a call to size only before the first call to begin, but a forward range must support size at all times).
Notes
disable_sized_range cannot be used to opt-out a range whose iterator and sentinel satisfy sized_sentinel_for; std::disable_sized_sentinel_for must be used instead.
disable_sized_range cannot be specialized for array types or reference types.
Example
#include <forward_list>
#include <list>
#include <ranges>
static_assert
(
std::ranges::sized_range<std::list<int>> and
not std::ranges::sized_range<std::forward_list<int>>
);
int main() {}
See also
|
(C++20)
|
specifies a range whose iterator type satisfies random_access_iterator (concept) |
|
(C++20)
|
specifies a range whose iterator type satisfies contiguous_iterator (concept) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/ranges/sized_range