Namespaces
Variants

C++ named requirements: LegacyRandomAccessIterator

From cppreference.net
C++ named requirements

一个 LegacyRandomAccessIterator 是一种 LegacyBidirectionalIterator ,它可以在常数时间内移动到指向任何元素。

若一个 LegacyRandomAccessIterator it 源自某个 Container ,则 it value_type 与容器的相同,因此解引用( * it )将获得容器的 value_type

指向数组元素的指针满足 LegacyRandomAccessIterator 的所有要求。

目录

要求

类型 It 满足 LegacyRandomAccessIterator

并且,给定

  • value_type ,由 std:: iterator_traits < It > :: value_type 表示的类型
  • difference_type ,由 std:: iterator_traits < It > :: difference_type 表示的类型
  • reference ,由 std:: iterator_traits < It > :: reference 表示的类型
  • i a b ,类型为 It const It 的对象
  • r ,类型为 It 的左值
  • n ,类型为 difference_type 的整数

以下表达式必须有效并具有其指定效果:

表达式 返回类型 操作语义 备注
r + = n It& difference_type m = n ;

if ( m >= 0 ) while ( m -- ) ++ r ;
else while ( m ++ ) -- r ;
return r ;

  • n 可为正数或负数
  • 复杂度为常数(即实现不能实际执行操作语义中展示的 while 循环)
a + n

n + a

It It temp = a ;

return temp + = n ;

  • n 可为正数或负数
  • a + n == n + a
r - = n It& return r + = - n ; n 的绝对值必须在 difference_type 的可表示值范围内
i - n It It temp = i ;
return temp - = n ;
b - a difference_type return n ;
(参见前置条件)

前置条件:

  • 存在 difference_type 类型的值 n 使得 a + n == b

后置条件:

  • b == a + ( b - a )
i [ n ] 可转换为 reference * ( i + n )
a < b

满足 BooleanTestable

(C++20 前)

建模 boolean-testable

(C++20 起)
等价于 return b - a > 0 ; 前置条件:
  • b - a 相同

严格全序关系:

  • ! ( a < a )
  • a < b ! ( b < a )
  • a < b b < c a < c
  • a < b b < a a == b
    (恰好有一个表达式为真)
a > b

满足 BooleanTestable

(C++20 前)

建模 boolean-testable

(C++20 起)
b < a a < b 相反的全序关系
a >= b

满足 BooleanTestable

(C++20 前)

建模 boolean-testable

(C++20 起)
! ( a < b )
a <= b

满足 BooleanTestable

(C++20 前)

建模 boolean-testable

(C++20 起)
! ( a > b )

上述规则意味着 LegacyRandomAccessIterator 也实现了 LessThanComparable

一个 可变 LegacyRandomAccessIterator 是指同时满足 LegacyOutputIterator 要求的 LegacyRandomAccessIterator

概念

为定义 std::iterator_traits ,特定义以下仅用于说明的概念。

template < class I >

concept __LegacyRandomAccessIterator =
__LegacyBidirectionalIterator < I > && std:: totally_ordered < I > &&
requires ( I i, typename std:: incrementable_traits < I > :: difference_type n )
{
{ i + = n } - > std:: same_as < I & > ;
{ i - = n } - > std:: same_as < I & > ;
{ i + n } - > std:: same_as < I > ;
{ n + i } - > std:: same_as < I > ;
{ i - n } - > std:: same_as < I > ;
{ i - i } - > std:: same_as < decltype ( n ) > ;
{ i [ n ] } - > std:: convertible_to < std:: iter_reference_t < I >> ;

} ;

其中仅用于说明的概念 __LegacyBidirectionalIterator LegacyBidirectionalIterator 中有详细说明。

(C++20 起)

缺陷报告

以下行为变更缺陷报告被追溯应用于先前发布的C++标准。

缺陷报告 适用范围 发布时行为 修正后行为
LWG 299
( N3066 )
C++98 要求 a [ n ] 的返回类型
可转换为 const value_type &
要求返回类型
可转换为 reference
LWG 448 C++98 要求 a [ n ] 的返回类型
可转换为 value_type
要求返回类型
可转换为 const value_type & [1]
LWG 1079 C++98 b - a 使用 a < b 定义,
导致循环定义
从定义中移除 a < b
LWG 2114
( P2167R3 )
C++98 转换为 bool 的要求过弱,
无法反映实现预期
强化了要求规范
  1. LWG issue 299 在此决议后重新开放。

另请参阅

指定 bidirectional_iterator 为随机访问迭代器,支持常数时间内的前进后退及下标操作
(概念)
迭代器库 提供迭代器、迭代器特征、适配器及工具函数的定义