std::ranges::sized_range, std::ranges::disable_sized_range
From cppreference.com
 
 
 
 
 
 C++ 
 Feature test macros (C++20)
 Concepts library (C++20)
 Metaprogramming library (C++11)
 Ranges library (C++20)
 Filesystem library (C++17)
 Concurrency support library (C++11)
 Execution control library (C++26)
Ranges library 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
(until C++26)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
	
		
(C++23)(C++23)
  (C++23)(C++23)
(C++23)(C++23)
  (C++26)(C++26)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
  (C++23)(C++23)
(C++23)
(C++23)(C++23)
(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)(C++23)
(C++23)
    Defined in header 
 
 
<ranges> 
  
 (1)
 
template< class T >
 
 (since C++20) concept sized_range = ranges::range <T> &&
    requires(T& t) {
        ranges::size (t);
(until C++26)
template< class T >
 
 (since C++26) 
concept sized_range = ranges::approximately_sized_range<T> &&
    requires(T& t) {
        ranges::size (t);
template< class >
constexpr bool disable_sized_range = false;
 (2) 
 (since C++20) 
constexpr bool disable_sized_range = false;
1) The 
sized_range concept specifies the requirements of a range (until C++26)approximately_sized_range (since C++26) type that knows its size in constant time with the size function.2) The 
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.[edit] Semantic requirements
1) Given an lvalue t 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 t in 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> models forward_iterator, ranges::size (t) is well-defined regardless of the evaluation of ranges::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).
[edit] 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.
| Feature-test macro | Value | Std | Feature | 
|---|---|---|---|
| __cpp_lib_ranges_reserve_hint | 202502L | (C++26) | ranges::approximately_sized_range and ranges::reserve_hint | 
[edit] Example
Run this code
#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() {}