Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/input or output iterator"

From cppreference.com
< cpp‎ | iterator
m (Use c template for - can-reference)
(mention how to make a class satisfy this concept)
Line 17: Line 17:
 
The exposition-only concept {{c|/*can-reference*/}} is satisfied if and only if the type is referenceable (in particular, not {{c|void}}).
 
The exposition-only concept {{c|/*can-reference*/}} is satisfied if and only if the type is referenceable (in particular, not {{c|void}}).
  
{{cpp/concepts/equality preservation}}
+
{{todo|Is *i required to be equality-preserving?}}
 +
<!-- {{cpp/concepts/equality preservation}} -->
 +
 
 +
===Notes===
 +
A typical {{tt|input_or_output_iterator}} class only needs to be {{lconcept|movable}}, and provide the following members:
 +
 
 +
* Member typedef {{tt|difference_type}} (used by {{lc|std::iter_difference_t}}).
 +
* Prefix {{tt|operator++}} that returns a reference to {{tt|*this}}.
 +
* Postfix {{tt|operator++}}.
 +
* The dereference operator {{tt|operator*}}.
 +
 
 +
Alternatively, the {{tt|difference_type}} can be provided by specializing either {{lc|std::iterator_traits}} or {{lc|std::incrementable_traits}}. The functions can be defined as non-members, to be found by [[cpp/language/adl|argument-dependent lookup]].
  
=== Notes ===
 
 
{{tt|input_or_output_iterator}} itself only specifies operations for dereferencing and incrementing an iterator. Most algorithms will require additional operations, for example:
 
{{tt|input_or_output_iterator}} itself only specifies operations for dereferencing and incrementing an iterator. Most algorithms will require additional operations, for example:
 
* comparing iterators with sentinels (see {{lconcept|sentinel_for}});
 
* comparing iterators with sentinels (see {{lconcept|sentinel_for}});
* reading values from an iterator (see {{lconcept|indirectly_readable}} and {{lconcept|input_iterator}})
+
* reading values from an iterator (see {{lconcept|indirectly_readable}} and {{lconcept|input_iterator}});
* writing values to an iterator (see {{lconcept|indirectly_writable}} and {{lconcept|output_iterator}})
+
* writing values to an iterator (see {{lconcept|indirectly_writable}} and {{lconcept|output_iterator}});
* a richer set of iterator movements (see {{lconcept|forward_iterator}}, {{lconcept|bidirectional_iterator}}, {{lconcept|random_access_iterator}})
+
* a richer set of iterator movements (see {{lconcept|forward_iterator}}, {{lconcept|bidirectional_iterator}}, {{lconcept|random_access_iterator}}).
  
 
Unlike the {{named req|Iterator}} requirements, the {{tt|input_or_output_iterator}} concept does not require copyability.
 
Unlike the {{named req|Iterator}} requirements, the {{tt|input_or_output_iterator}} concept does not require copyability.
  
 
{{langlinks|es|ja|zh}}
 
{{langlinks|es|ja|zh}}

Revision as of 21:57, 7 September 2023

 
 
Iterator library
Iterator concepts
input_or_output_iterator
(C++20)

Iterator primitives
Algorithm concepts and utilities
Indirect callable concepts
Common algorithm requirements
(C++20)
(C++20)
(C++20)
Utilities
(C++20)
Iterator adaptors
Range access
(C++11)(C++14)
(C++14)(C++14)  
(C++11)(C++14)
(C++14)(C++14)  
(C++17)(C++20)
(C++17)
(C++17)
 
Defined in header <iterator>
template <class I>

concept input_or_output_iterator =
  requires(I i) {
    { *i } -> /*can-reference*/;
  } &&

  std::weakly_incrementable<I>;
(since C++20)

The input_or_output_iterator concept forms the basis of the iterator concept taxonomy; every iterator type satisfies the input_or_output_iterator requirements.

The exposition-only concept /*can-reference*/ is satisfied if and only if the type is referenceable (in particular, not void).

Notes

A typical input_or_output_iterator class only needs to be movable, and provide the following members:

  • Member typedef difference_type (used by std::iter_difference_t).
  • Prefix operator++ that returns a reference to *this.
  • Postfix operator++.
  • The dereference operator operator*.

Alternatively, the difference_type can be provided by specializing either std::iterator_traits or std::incrementable_traits. The functions can be defined as non-members, to be found by argument-dependent lookup.

input_or_output_iterator itself only specifies operations for dereferencing and incrementing an iterator. Most algorithms will require additional operations, for example:

Unlike the LegacyIterator requirements, the input_or_output_iterator concept does not require copyability.