命名空间
变体
操作

std::sized_sentinel_for, std::disable_sized_sentinel_for

来自 cppreference.cn
< cpp‎ | iterator
 
 
迭代器库
迭代器概念
sized_sentinel_for
(C++20)

迭代器原语
算法概念和工具
间接可调用概念
常用算法要求
(C++20)
(C++20)
(C++20)
工具
(C++20)
迭代器适配器
范围访问
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
定义于头文件 <iterator>
template< class S, class I >

    concept sized_sentinel_for =
        std::sentinel_for<S, I> &&
        !std::disable_sized_sentinel_for<std::remove_cv_t<S>,
                                         std::remove_cv_t<I>> &&
        requires(const I& i, const S& s) {
            { s - i } -> std::same_as<std::iter_difference_t<I>>;
            { i - s } -> std::same_as<std::iter_difference_t<I>>;

        };
(1) (自 C++20 起)
template< class S, class I >
    inline constexpr bool disable_sized_sentinel_for = false;
(2) (自 C++20 起)
1) `sized_sentinel_for` 概念指定迭代器类型 `I` 的对象和哨兵类型 `S` 的对象可以相减,以常数时间计算它们之间的距离。
2) `disable_sized_sentinel_for` 变量模板可用于阻止可以相减但实际上不符合 `sized_sentinel_for` 模型的迭代器和哨兵满足该概念。
程序可以为 cv 限定的非数组对象类型 `S` 和 `I` 特化 `disable_sized_sentinel_for`,只要其中至少一个是程序定义的类型。此类特化可在常量表达式中使用,并且类型为 const bool

目录

[编辑] 语义要求

i 为类型 I 的迭代器,s 为类型 S 的哨兵,使得 [i, s) 表示一个范围。设 n 为使 bool(i == s)true 所需的最少 ++i 应用次数。仅当满足以下所有条件时,IS 才符合 sized_sentinel_for<S, I> 模型

  • 如果 n 可以用 std::iter_difference_t<I> 表示,则 s - i 是良定义的,并且等于 n
  • 如果 -n 可以用 std::iter_difference_t<I> 表示,则 i - s 是良定义的,并且等于 -n

[编辑] 相等保持

标准库概念的 requires 表达式中声明的表达式必须是相等保持的(除非另有说明)。

[编辑] 隐式表达式变体

对于某些常量左值操作数使用非修改表达式的 requires 表达式也需要隐式表达式变体

[编辑] 参见

指定范围以常数时间知道其大小
(概念) [编辑]
返回一个等于范围大小的整数
(自定义点对象)[编辑]