std::sized_sentinel_for, std::disable_sized_sentinel_for

来自cppreference.com
< 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 限定的非数组对象类型 SI 特化该变量模板,只要其中之一是由程序定义的类型。这种特化必须可用于常量表达式并拥有 const bool 类型。

语义要求

iI 类型的迭代器,而 sS 类型的哨位,使得 [is) 代表一个范围。令 n 为需要应用 ++i 以令 bool(i == s)true 的最小次数。那么 SI 只有在满足以下所有条件时才会实现 sized_sentinel_for<S, I>

  • 如果 n 能以 ranges::difference_type_t<I> 表示,那么 s - i 良定义并等于 n
  • 如果 -n 能以 ranges::difference_type_t<I> 表示,那么 i - s 良定义并等于 -n

相等性保持

标准库概念的 requires 表达式中声明的表达式都要求保持相等性(除非另外说明)。

隐式表达式变种

使用了不修改某常量左值操作数的表达式的 requires 表达式,也会要求其隐式的表达式变种

参阅

指定类型为可在常数时间内知晓大小的范围
(概念)
返回等于范围大小的整数
(定制点对象)