Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | named req
m (an lvalue of type T instead of a value of type T&)
(Added LWG issue #299 DR (part 2/2).)
Line 13: Line 13:
  
 
And, given
 
And, given
* {{tt|value_type}}, the type denoted by {{c|std::iterator_traits<It>::value_type}}
+
* {{tt|value_type}}, the type denoted by {{c/core|std::iterator_traits<It>::value_type}}
* {{tt|difference_type}}, the type denoted by {{c|std::iterator_traits<It>::difference_type}}
+
* {{tt|difference_type}}, the type denoted by {{c/core|std::iterator_traits<It>::difference_type}}
* {{tt|reference}}, the type denoted by {{c|std::iterator_traits<It>::reference}}
+
* {{tt|reference}}, the type denoted by {{c/core|std::iterator_traits<It>::reference}}
* {{c|i}}, {{c|a}}, {{c|b}}, objects of type {{c|It}} or {{c|const It}}
+
* {{c|i}}, {{c|a}}, {{c|b}}, objects of type {{tt|It}} or {{c/core|const It}}
* {{c|r}}, an lvalue of type {{c|It}}
+
* {{c|r}}, an lvalue of type {{tt|It}}
 
* {{c|n}}, an integer of type {{tt|difference_type}}
 
* {{c|n}}, an integer of type {{tt|difference_type}}
  
The following expressions must be valid and have their specified effects  
+
The following expressions must be valid and have their specified effects:
 
+
 
{|table class="wikitable"
 
{|table class="wikitable"
 
|-
 
|-
 
!Expression||Return type||Operational semantics||Notes
 
!Expression||Return type||Operational semantics||Notes
 
|-
 
|-
|{{c|r +{{=}} n}}
+
|{{c|1=r += n}}
|{{c|It&}}
+
|{{tt|It&}}
 
|{{c|1=difference_type m = n;
 
|{{c|1=difference_type m = n;
 
if (m >= 0) while (m--) ++r;
 
if (m >= 0) while (m--) ++r;
Line 33: Line 32:
 
return r;}}
 
return r;}}
 
|
 
|
*{{ttb|n}} can be both positive or negative
+
*{{c|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)
 
*The complexity is constant (that is, the implementation cannot actually execute the while loop shown in operational semantics)
 
|-
 
|-
 
|{{c|a + n}}
 
|{{c|a + n}}
 
{{c|n + a}}
 
{{c|n + a}}
|{{c|It}}
+
|{{tt|It}}
 
|{{c|1=It temp = a;
 
|{{c|1=It temp = a;
 
return temp += n;}}  
 
return temp += n;}}  
 
|
 
|
*{{ttb|n}} can be both positive or negative
+
*{{c|n}} can be both positive or negative
 
*{{c|1=a + n == n + a}}
 
*{{c|1=a + n == n + a}}
 
|-
 
|-
|{{c|1=r -= n}}||{{c|It&}}||{{c|1=return r += -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}}
 
|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|1=It temp = i;
+
|{{c|i - n}}||{{tt|It}}||{{c|1=It temp = i;
 
return temp -= n;}}||
 
return temp -= n;}}||
 
|-
 
|-
 
|{{c|b - a}}||{{tt|difference_type}}||{{c|return n;}}||
 
|{{c|b - a}}||{{tt|difference_type}}||{{c|return n;}}||
 
Precondition:
 
Precondition:
* there exists a value {{c|n}} of type {{tt|difference_type}} such that {{c|1=a+n==b}}
+
* there exists a value {{c|n}} of type {{tt|difference_type}} such that {{c|1=a + n == b}}
 
Postcondition:
 
Postcondition:
 
* {{c|1=b == a + (b - a)}}.
 
* {{c|1=b == a + (b - a)}}.
Line 59: Line 58:
 
|{{c|i[n]}}||convertible to {{tt|reference}}||{{c|*(i + n)}}||
 
|{{c|i[n]}}||convertible to {{tt|reference}}||{{c|*(i + n)}}||
 
|-
 
|-
|{{c|a < b}}||contextually convertible to {{c|bool}}||Equivalent to {{c|return b - a > 0;}}
+
|{{c|a < b}}||contextually convertible to {{c/core|bool}}||Equivalent to {{c|return b - a > 0;}}
 
|Precondition:
 
|Precondition:
 
* same as of {{c|b - a}}<!-- LWG3236 -->
 
* same as of {{c|b - a}}<!-- LWG3236 -->
Line 68: Line 67:
 
* {{c|a < b}} or {{c|b < a}} or {{c|1=a == b}}<br> (exactly one of the expressions is true)
 
* {{c|a < b}} or {{c|b < a}} or {{c|1=a == b}}<br> (exactly one of the expressions is true)
 
|-
 
|-
|{{c|a > b}}||contextually convertible to {{c|bool}}||{{c|b < a}}||Total ordering relation opposite to {{c|a < b}}
+
|{{c|a > b}}||contextually convertible to {{c/core|bool}}||{{c|b < a}}||Total ordering relation opposite to {{c|a < b}}
 
|-
 
|-
|{{c|1=a >= b}}||contextually convertible to {{c|bool}}||{{c|!(a < b)}}||
+
|{{c|1=a >= b}}||contextually convertible to {{c/core|bool}}||{{c|!(a < b)}}||
 
|-
 
|-
|{{c|1=a <= b}}||contextually convertible to {{c|bool}}||{{c|!(a > b)}}||
+
|{{c|1=a <= b}}||contextually convertible to {{c/core|bool}}||{{c|!(a > b)}}||
 
|}
 
|}
  
Line 79: Line 78:
 
A ''mutable'' {{named req/core|RandomAccessIterator}} is a {{named req/core|RandomAccessIterator}} that additionally satisfies the {{named req|OutputIterator}} requirements.
 
A ''mutable'' {{named req/core|RandomAccessIterator}} is a {{named req/core|RandomAccessIterator}} that additionally satisfies the {{named req|OutputIterator}} requirements.
  
{{rrev | since=c++20 |
+
{{rrev|since=c++20|
 
===Concept===
 
===Concept===
 
For the definition of {{lc|std::iterator_traits}}, the following exposition-only concept is defined.
 
For the definition of {{lc|std::iterator_traits}}, the following exposition-only concept is defined.
Line 87: Line 86:
 
template<class I>
 
template<class I>
 
concept __LegacyRandomAccessIterator<!-- called cpp17-random-access-iterator in the standard --> =
 
concept __LegacyRandomAccessIterator<!-- called cpp17-random-access-iterator in the standard --> =
  __LegacyBidirectionalIterator<I> && std::totally_ordered<I> &&
+
    __LegacyBidirectionalIterator<I> && std::totally_ordered<I> &&
  requires(I i, typename std::incrementable_traits<I>::difference_type n) {
+
        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&>;
    { 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 -  n } -> std::same_as<I>;
+
            { n +  i } -> std::same_as<I>;
    { i -  i } -> std::same_as<decltype(n)>;
+
            { i -  n } -> std::same_as<I>;
    {  i[n]  } -> std::convertible_to<std::iter_reference_t<I>>;
+
            { i -  i } -> std::same_as<decltype(n)>;
  };
+
            {  i[n]  } -> std::convertible_to<std::iter_reference_t<I>>;
 +
        };
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
Line 102: Line 102:
 
where the exposition-only concept {{tt|__LegacyBidirectionalIterator}} is described in {{rlp|BidirectionalIterator#Concept|LegacyBidirectionalIterator#Concept}}.
 
where the exposition-only concept {{tt|__LegacyBidirectionalIterator}} is described in {{rlp|BidirectionalIterator#Concept|LegacyBidirectionalIterator#Concept}}.
 
}}
 
}}
 +
 +
===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<br>required to be convertible to {{tt|T}}|after=the return type is required to<br>be convertible to {{tt|reference}}}}
 +
{{dr list end}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/iterator/dsc random_access_iterator}}
+
{{dsc inc|cpp/iterator/dsc random_access_iterator}}
 
{{see_also_iterator_library}}
 
{{see_also_iterator_library}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Revision as of 01:16, 8 February 2023

 
 
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

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;

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 contextually convertible to bool 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 contextually convertible to bool b < a Total ordering relation opposite to a < b
a >= b contextually convertible to bool !(a < b)
a <= b contextually convertible to bool !(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#Concept.

(since C++20)

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 T
the return type is required to
be convertible to reference

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