Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/weakly incrementable"

From cppreference.com
< cpp‎ | iterator
(+the rest of [iterator.concept.winc])
(Removed the definition of “integer-like type”, it is in a new page now.)
 
(21 intermediate revisions by 9 users not shown)
Line 3: Line 3:
 
{{dcl begin}}
 
{{dcl begin}}
 
{{dcl header|iterator|since=c++20}}
 
{{dcl header|iterator|since=c++20}}
{{dcl | since=c++20 | 1=
+
{{dcl|since=c++20|1=
template<class I>
+
template< class I >
  concept weakly_incrementable =
+
    concept weakly_incrementable =
    std::default_constructible<I> && std::movable<I> &&
+
        std::movable<I> &&
    requires(I i) {
+
        requires(I i) {
      typename std::iter_difference_t<I>;
+
            typename std::iter_difference_t<I>;
      requires /*is-signed-integer-like*/<std::iter_difference_t<I>>;
+
            requires /*is-signed-integer-like*/<std::iter_difference_t<I>>;
      { ++i } -> std::same_as<I&>;   // not required to be equality-preserving
+
            { ++i } -> std::same_as<I&>; // not required to be equality-preserving
      i++;                           // not required to be equality-preserving
+
            i++;                         // not required to be equality-preserving
    };
+
        };
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
where {{c|/*is-signed-integer-like*/<I>}} is true if {{c|std::signed_­integral<I>}} is true or if {{c|I}} is a class that behaves like a signed integer type including all operators, implicit conversions, and {{lc|std::numeric_limits}} specializations.
 
  
This concept specifies requirements on types that can be incremented with the pre- and post-increment operators, but those increment operations are not necessarily equality-preserving, and the type itself is not required to be {{lc|std::equality_­comparable}}.
+
For the definition of {{c/core|/*is-signed-integer-like*/}}, see {{rlpi|is-integer-like}}{{sep}}.
  
Additionally, for any object {{tt|i}} of type {{tt|I}}, {{tt|I}} satisfies {{tt|std::weakly_incrementable}} only if
+
This concept specifies requirements on types that can be incremented with the pre- and post-increment operators, but those increment operations are not necessarily [[cpp/concepts#Equality preservation|equality-preserving]], and the type itself is not required to be {{lc|std::equality_comparable}}.
* The expressions {{c|++i}} and {{c|i++}} have the same domain
+
* If {{tt|i}} is incrementable, then both {{c|++i}} and {{c|i++}} advance {[tt|t}}
+
* If {{tt|i}} is incrementable, then {{c|addressof(++i) {{==}} addressof(i)}}
+
  
For {{lc|std::weakly_­incrementable}} types, a {{==}} b does not imply that ++a {{==}} ++b.  Algorithms on weakly incrementable types must be single-pass algorithms. These algorithms can be used with istreams as the source of the input data through {{lc|std::istream_­iterator}}
+
For {{tt|std::weakly_incrementable}} types, {{c|1=a == b}} does not imply that {{c|1=++a == ++b}}.  Algorithms on weakly incrementable types must be single-pass algorithms. These algorithms can be used with istreams as the source of the input data through {{lc|std::istream_iterator}}.
  
=== See also ===
+
===Semantic requirements===
 +
For an object {{c|i}} of type {{tt|I}}, {{tt|I}} models {{tt|std::weakly_incrementable}} only if all following conditions are satisfied:
 +
* The expressions {{c|++i}} and {{c|i++}} have the same domain.
 +
* If {{c|i}} is incrementable, then both {{c|++i}} and {{c|i++}} advance {{c|i}}.
 +
* If {{c|i}} is incrementable, then {{c|1=std::addressof(++i) == std::addressof(i)}}.
 +
 
 +
===Defect reports===
 +
{{dr list begin}}
 +
{{dr list item|paper=P2325R3|std=C++20|before={{lconcept|default_initializable}} was required|after=not required}}
 +
{{dr list end}}
 +
 
 +
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/iterator/dsc Incrementable}}
+
{{dsc inc|cpp/iterator/dsc incrementable}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|ja|zh}}
+
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 18:40, 9 September 2024

 
 
Iterator library
Iterator concepts
weakly_incrementable
(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 weakly_incrementable =
        std::movable<I> &&
        requires(I i) {
            typename std::iter_difference_t<I>;
            requires /*is-signed-integer-like*/<std::iter_difference_t<I>>;
            { ++i } -> std::same_as<I&>; // not required to be equality-preserving
            i++;                         // not required to be equality-preserving

        };
(since C++20)

For the definition of /*is-signed-integer-like*/, see is-integer-like .

This concept specifies requirements on types that can be incremented with the pre- and post-increment operators, but those increment operations are not necessarily equality-preserving, and the type itself is not required to be std::equality_comparable.

For std::weakly_incrementable types, a == b does not imply that ++a == ++b. Algorithms on weakly incrementable types must be single-pass algorithms. These algorithms can be used with istreams as the source of the input data through std::istream_iterator.

[edit] Semantic requirements

For an object i of type I, I models std::weakly_incrementable only if all following conditions are satisfied:

  • The expressions ++i and i++ have the same domain.
  • If i is incrementable, then both ++i and i++ advance i.
  • If i is incrementable, then std::addressof(++i) == std::addressof(i).

[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
P2325R3 C++20 default_initializable was required not required

[edit] See also

specifies that the increment operation on a weakly_incrementable type is equality-preserving and that the type is equality_comparable
(concept) [edit]