Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/named req/RandomAccessIterator"

From cppreference.com
< cpp‎ | named req
m (Text replace - "{{cpp|" to "{{c|")
(LWG2114/P2167R3)
 
(40 intermediate revisions by 18 users not shown)
Line 1: Line 1:
{{cpp/concept/title|RandomAccessIterator}}
+
{{cpp/named req/title|RandomAccessIterator}}
{{cpp/concept/sidebar}}
+
{{cpp/named req/navbar}}
  
An Iterator that can be moved to point to any element in constant time.
+
A {{named req|RandomAccessIterator}} is a {{named req|BidirectionalIterator}} that can be moved to point to any element in constant time.
  
A standard pointer satisfies this concept.
+
If a {{named req|RandomAccessIterator}} {{c|it}} originates from a {{named req|Container}}, then {{c|it}}'s {{tt|value_type}} is the same as the container's, so dereferencing ({{c|*it}}) obtains the container's {{tt|value_type}}.
 +
 
 +
A pointer to an element of an array satisfies all requirements of {{named req/core|RandomAccessIterator}}.
  
 
===Requirements===
 
===Requirements===
 +
The type {{tt|It}} satisfies {{named req/core|RandomAccessIterator}} if
 +
* The type {{tt|It}} satisfies {{named req|BidirectionalIterator}}
  
* {{concept|BidirectionalIterator}}
+
And, given
* {{concept|LessThanComparable}}
+
* {{tt|value_type}}, the type denoted by {{c/core|std::iterator_traits<It>::value_type}}
 
+
* {{tt|difference_type}}, the type denoted by {{c/core|std::iterator_traits<It>::difference_type}}
 +
* {{tt|reference}}, the type denoted by {{c/core|std::iterator_traits<It>::reference}}
 +
* {{c|i}}, {{c|a}}, {{c|b}}, objects of type {{tt|It}} or {{c/core|const It}}
 +
* {{c|r}}, an lvalue of type {{tt|It}}
 +
* {{c|n}}, an integer of type {{tt|difference_type}}
  
 +
The following expressions must be valid and have their specified effects:
 
{|table class="wikitable"
 
{|table class="wikitable"
 
|-
 
|-
!Expression||Return||Equivalent expression||Notes
+
!Expression||Return type||Operational semantics||Notes
 
|-
 
|-
|{{c|i +{{=}} n}}
+
|{{c|1=r += n}}
|{{c|It&}}
+
|{{tt|It&}}
|{{c|while ( n > 0 )
+
|{{c|1=difference_type m = n;
  ++i;}}
+
if (m >= 0) while (m--) ++r;
 +
else while (m++) --r;
 +
return r;}}
 
|
 
|
*{{ttb|n}} can b both positive or negative
+
*{{c|n}} can be both positive or negative
*Constant complexity
+
*The complexity is constant (that is, the implementation cannot actually execute the while loop shown in operational semantics)
 
|-
 
|-
|{{c|i + n}}
+
|{{c|a + n}}
|{{c|It}}
+
{{c|n + a}}
|{{c|It temp {{=}} i;
+
|{{tt|It}}
return i +{{=}} n;}}  
+
|{{c|1=It temp = a;
 +
return temp += n;}}
 +
|
 +
*{{c|n}} can be both positive or negative
 +
*{{c|1=a + n == n + a}}
 
|-
 
|-
|{{c|n + i}}||{{c|It}}||{{c|i + n}}||
+
|{{c|1=r -= n}}||{{tt|It&}}||{{c|1=return r += -n;}}
 +
|The absolute value of {{c|n}} must be within the range of representable values of {{tt|difference_type}}. {{mark unreviewed dr|LWG|2519}}
 
|-
 
|-
|{{c|i -{{=}} n}}||{{c|It&}}||{{c|i +{{=}} -n}}||
+
|{{c|i - n}}||{{tt|It}}||{{c|1=It temp = i;
 +
return temp -= n;}}||
 
|-
 
|-
|{{c|i - n}}||{{c|It}}||{{c|i + -n}}||
+
|{{c|b - a}}||{{tt|difference_type}}||{{c|return n;}}<br>(see the precondition)||
 +
Precondition:
 +
* there exists a value {{c|n}} of type {{tt|difference_type}} such that {{c|1=a + n == b}}
 +
Postcondition:
 +
* {{c|1=b == a + (b - a)}}.
 
|-
 
|-
|{{c|n - i}}||{{c|It}}||{{c|i - n}}||
+
|{{c|i[n]}}||convertible to {{tt|reference}}||{{c|*(i + n)}}||
 
|-
 
|-
|{{c|a < b}}||{{c|bool}}||{{c|(a-b) > 0}}
+
|{{c|a < b}}
|Strict total ordering relation:
+
|{{rrev multi|noborder=true
* {{ttb|!(a < a)}}
+
|rev1=meets {{named req|BooleanTestable}}
* if {{ttb|a < b}} then {{ttb|!(b < a)}}
+
|since2=c++20|rev2=models {{lconcept|boolean-testable}}
* if {{ttb|a < b}} and {{ttb|b < c}} then {{ttb|a < c}}
+
}}
* {{ttb|a < b}} or {{ttb|b < a}} or {{ttb|a {{==}} b}} <br> (exactly one of the expression is true)
+
|Equivalent to {{c|return b - a > 0;}}
 +
|Precondition:
 +
* same as of {{c|b - a}}<!-- LWG3236 -->
 +
Strict total ordering relation:
 +
* {{c|!(a < a)}}
 +
* if {{c|a < b}} then {{c|!(b < a)}}
 +
* if {{c|a < b}} and {{c|b < c}} then {{c|a < c}}
 +
* {{c|a < b}} or {{c|b < a}} or {{c|1=a == b}}<br> (exactly one of the expressions is true)
 
|-
 
|-
|{{c|a > b}}||{{c|bool}}||{{c|b < a}}||
+
|{{c|a > b}}
 +
|{{rrev multi|noborder=true
 +
|rev1=meets {{named req|BooleanTestable}}
 +
|since2=c++20|rev2=models {{lconcept|boolean-testable}}
 +
}}
 +
|{{c|b < a}}
 +
|Total ordering relation opposite to {{c|a < b}}
 
|-
 
|-
|{{c|a >{{=}} b}}||{{c|bool}}||{{c|!(a < b)}} ||
+
|{{c|1=a >= b}}
 +
|{{rrev multi|noborder=true
 +
|rev1=meets {{named req|BooleanTestable}}
 +
|since2=c++20|rev2=models {{lconcept|boolean-testable}}
 +
}}
 +
|{{c|!(a < b)}}
 +
|
 
|-
 
|-
|{{c|a <{{=}} b}}||{{c|bool}}||{{c|!(a > b)}}||
+
|{{c|1=a <= b}}
 +
|{{rrev multi|noborder=true
 +
|rev1=meets {{named req|BooleanTestable}}
 +
|since2=c++20|rev2=models {{lconcept|boolean-testable}}
 +
}}
 +
|{{c|!(a > b)}}
 +
|
 
|}
 
|}
====Table Notes====
+
 
*{{ttb|It}} is the type implementing this concept
+
The above rules imply that {{named req/core|RandomAccessIterator}} also implements {{named req|LessThanComparable}}.
*{{ttb|i}}, {{ttb|a}}, {{ttb|b}} are objects of type {{ttb|It}}
+
 
*{{ttb|n}} is an integer of type {{ttb|difference_type}}
+
A ''mutable'' {{named req/core|RandomAccessIterator}} is a {{named req/core|RandomAccessIterator}} that additionally satisfies the {{named req|OutputIterator}} requirements.
 +
 
 +
{{rrev|since=c++20|
 +
===Concept===
 +
For the definition of {{lc|std::iterator_traits}}, the following exposition-only concept is defined.
 +
 
 +
{{dcl begin}}
 +
{{dcl|1=
 +
template<class I>
 +
concept __LegacyRandomAccessIterator<!-- called cpp17-random-access-iterator in the standard --> =
 +
    __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>>;
 +
        };
 +
}}
 +
{{dcl end}}
 +
 
 +
where the exposition-only concept {{tt|__LegacyBidirectionalIterator}} is described in {{rlp|BidirectionalIterator#Concept|LegacyBidirectionalIterator}}.
 +
}}
 +
 
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|wg=lwg|dr=299|paper=N3066|std=C++98|before=the return type of {{c|a[n]}} was required<br>to be convertible to {{c/core|const value_type&}}|after=the return type is required to<br>be convertible to {{tt|reference}}}}
 +
{{dr list item|wg=lwg|dr=448|std=C++98|before=the return type of {{c|a[n]}} was required<br>to be convertible to {{tt|value_type}}|after=the return type is required to be<br>convertible to {{c/core|const value_type&}}<ref>{{lwg|299}} was reopened after this resolution.</ref>}}
 +
{{dr list item|wg=lwg|dr=1079|std=C++98|before={{c|b - a}} was defined using {{c|a < b}},<br>resulted in circular definition|after=removed {{c|a < b}} from the definition}}
 +
{{dr list item|wg=lwg|dr=2114|paper=P2167R3|std=C++98|before=convertibility to {{c/core|bool}} was too weak to reflect the expectation of implementations|after=requirements strengthened}}
 +
{{dr list end}}
 +
<references/>
 +
 
 +
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/iterator/dsc random_access_iterator}}
 +
{{see_also_iterator_library}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 19:46, 8 September 2024

 
 
C++ named requirements
 

A LegacyRandomAccessIterator is a LegacyBidirectionalIterator that can be moved to point to any element in constant time.

If a LegacyRandomAccessIterator it originates from a Container, then it's value_type is the same as the container's, so dereferencing (*it) obtains the container's value_type.

A pointer to an element of an array satisfies all requirements of LegacyRandomAccessIterator.

Contents

[edit] Requirements

The type It satisfies LegacyRandomAccessIterator if

And, given

  • value_type, the type denoted by std::iterator_traits<It>::value_type
  • difference_type, the type denoted by std::iterator_traits<It>::difference_type
  • reference, the type denoted by std::iterator_traits<It>::reference
  • i, a, b, objects of type It or const It
  • r, an lvalue of type It
  • n, an integer of type difference_type

The following expressions must be valid and have their specified effects:

Expression Return type Operational semantics Notes
r += n It& difference_type m = n;

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

  • n can be both positive or negative
  • The complexity is constant (that is, the implementation cannot actually execute the while loop shown in operational semantics)
a + n

n + a

It It temp = a;

return temp += n;

  • n can be both positive or negative
  • a + n == n + a
r -= n It& return r += -n; The absolute value of n must be within the range of representable values of difference_type.
i - n It It temp = i;
return temp -= n;
b - a difference_type return n;
(see the precondition)

Precondition:

  • there exists a value n of type difference_type such that a + n == b

Postcondition:

  • b == a + (b - a).
i[n] convertible to reference *(i + n)
a < b

meets BooleanTestable

(until C++20)

models boolean-testable

(since C++20)
Equivalent to return b - a > 0; Precondition:
  • same as of b - a

Strict total ordering relation:

  • !(a < a)
  • if a < b then !(b < a)
  • if a < b and b < c then a < c
  • a < b or b < a or a == b
    (exactly one of the expressions is true)
a > b

meets BooleanTestable

(until C++20)

models boolean-testable

(since C++20)
b < a Total ordering relation opposite to a < b
a >= b

meets BooleanTestable

(until C++20)

models boolean-testable

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

meets BooleanTestable

(until C++20)

models boolean-testable

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

The above rules imply that LegacyRandomAccessIterator also implements LessThanComparable.

A mutable LegacyRandomAccessIterator is a LegacyRandomAccessIterator that additionally satisfies the LegacyOutputIterator requirements.

Concept

For the definition of std::iterator_traits, the following exposition-only concept is defined.

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>>;

        };

where the exposition-only concept __LegacyBidirectionalIterator is described in LegacyBidirectionalIterator.

(since C++20)

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 299
(N3066)
C++98 the return type of a[n] was required
to be convertible to const value_type&
the return type is required to
be convertible to reference
LWG 448 C++98 the return type of a[n] was required
to be convertible to value_type
the return type is required to be
convertible to const value_type&[1]
LWG 1079 C++98 b - a was defined using a < b,
resulted in circular definition
removed a < b from the definition
LWG 2114
(P2167R3)
C++98 convertibility to bool was too weak to reflect the expectation of implementations requirements strengthened
  1. LWG issue 299 was reopened after this resolution.

[edit] See also

specifies that a bidirectional_iterator is a random-access iterator, supporting advancement in constant time and subscripting
(concept) [edit]
Iterator library provides definitions for iterators, iterator traits, adaptors, and utility functions