Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | named req
m (Defect reports: r)
(Requirements: +type of *it)
Line 15: Line 15:
 
* [[cpp/language/value_category|lvalues]] of type {{c|It}} satisfy {{named req|Swappable}}, and
 
* [[cpp/language/value_category|lvalues]] of type {{c|It}} satisfy {{named req|Swappable}}, and
 
* {{c|std::iterator_traits<It>}} has member typedefs {{rev inl|until=c++20|{{tt|value_type}}}}, {{tt|difference_type}}, {{tt|reference}}, {{tt|pointer}}, and {{tt|iterator_category}} {{mark unreviewed dr|LWG|2578}}, and
 
* {{c|std::iterator_traits<It>}} has member typedefs {{rev inl|until=c++20|{{tt|value_type}}}}, {{tt|difference_type}}, {{tt|reference}}, {{tt|pointer}}, and {{tt|iterator_category}} {{mark unreviewed dr|LWG|2578}}, and
* Given {{c|r}}, an lvalue of type {{c|It}}, the following expressions must be valid and have their specified effects:
+
* Given {{c|it_lvalue}}, an lvalue of type {{c|It}}, the following expressions must be valid and have their specified effects:
  
 
{|table class=wikitable
 
{|table class=wikitable
Line 21: Line 21:
 
!Expression||Return Type||Precondition
 
!Expression||Return Type||Precondition
 
|-
 
|-
|{{c|*r}}
+
|{{c|*it_lvalue}}
|unspecified
+
|{{c|std::iterator_traits<It>::reference}}
|{{c|r}} is [[cpp/iterator#Dereferenceability and validity|dereferenceable]]
+
|{{c|it_lvalue}} is [[cpp/iterator#Dereferenceability and validity|dereferenceable]]
 
|-
 
|-
|{{c|++r}}
+
|{{c|++it_lvalue}}
 
|{{c|It&}}
 
|{{c|It&}}
|{{c|r}} is ''incrementable'' (the behavior of the expression {{c|++r}} is defined)
+
|{{c|it_lvalue}} is ''incrementable'' (the behavior of the expression {{c|++it_lvalue}} is defined)
 
|}
 
|}
  

Revision as of 02:33, 17 July 2024

 
 
C++ named requirements
 

The LegacyIterator requirements describe types that can be used to identify and traverse the elements of a container.

LegacyIterator is the base set of requirements used by other iterator types: LegacyInputIterator, LegacyOutputIterator, LegacyForwardIterator, LegacyBidirectionalIterator, and LegacyRandomAccessIterator. Iterators can be thought of as an abstraction of pointers.

All the categories of iterators require only those functions that are realizable for a given category in constant time (amortized). Therefore, requirement tables and concept definitions (since C++20)for the iterators do not specify complexity.

Contents

Requirements

The type It satisfies LegacyIterator if

  • The type It satisfies CopyConstructible, and
  • The type It satisfies CopyAssignable, and
  • The type It satisfies Destructible, and
  • lvalues of type It satisfy Swappable, and
  • std::iterator_traits<It> has member typedefs value_type(until C++20), difference_type, reference, pointer, and iterator_category, and
  • Given it_lvalue, an lvalue of type It, the following expressions must be valid and have their specified effects:
Expression Return Type Precondition
*it_lvalue std::iterator_traits<It>::reference it_lvalue is dereferenceable
++it_lvalue It& it_lvalue is incrementable (the behavior of the expression ++it_lvalue is defined)

Concept

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

template<class I>

concept __LegacyIterator =
    requires(I i)
    {
        {   *i } -> __Referenceable;
        {  ++i } -> std::same_as<I&>;
        { *i++ } -> __Referenceable;

    } && std::copyable<I>;

where the exposition-only concept __Referenceable<T> is satisfied if and only if T& is a valid type (in particular, T must not be void).

(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 3420 C++20 the exposition-only concept checks copyable first copyable is checked only if the requires-expression yields true

See also

specifies that objects of a type can be incremented and dereferenced
(concept) [edit]
Iterator library provides definitions for iterators, iterator traits, adaptors, and utility functions