Namespaces
Variants

std::ranges:: sized_range, std::ranges:: disable_sized_range

From cppreference.net
Ranges library
Range adaptors
定义于头文件 <ranges>
(1)
template < class T >

concept sized_range = ranges:: range < T > &&
requires ( T & t ) {
ranges:: size ( t ) ;

} ;
(C++20 起)
(C++26 前)
template < class T >

concept sized_range = ranges :: approximately_sized_range < T > &&
requires ( T & t ) {
ranges:: size ( t ) ;

} ;
(C++26 起)
template < class >
constexpr bool disable_sized_range = false ;
(2) (C++20 起)
1) sized_range 概念规定了 range (C++26 前) approximately_sized_range (C++26 起) 类型的要求,该类型可通过 size 函数在常数时间内获知其大小。
2) disable_sized_range 的存在是为了允许使用那些提供了 size 函数(无论是作为成员函数还是非成员函数)但实际上并不满足 sized_range 概念的区间类型。用户可以为 cv 未限定的程序定义类型特化 disable_sized_range 。此类特化应当能在 常量表达式 中使用,并具有 const bool 类型。

目录

语义要求

1) 给定类型为 std:: remove_reference_t < T > 的左值 t ,当且仅当满足以下条件时 T 才建模 sized_range

注释

disable_sized_range 不能用于选择退出其迭代器和哨位满足 sized_sentinel_for 的范围;必须改用 std::disable_sized_sentinel_for

disable_sized_range 不能为数组类型或引用类型进行特化。

功能测试 标准 功能
__cpp_lib_ranges_reserve_hint 202502L (C++26) ranges::approximately_sized_range ranges::reserve_hint

示例

#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() {}

参见

指定其迭代器类型满足 random_access_iterator 的范围
(概念)
指定其迭代器类型满足 contiguous_iterator 的范围
(概念)