Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/iterator/sized sentinel for"

From cppreference.com
< cpp‎ | iterator
m (langlinks)
(Links to the definition of “program-defined type”. The standard never requires the complexity of iterator-sentinel subtraction to be constant.)
 
(2 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
{{cpp/iterator/navbar}}
 
{{cpp/iterator/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | iterator}}
+
{{dcl header|iterator}}
{{dcl | num=1 | since=c++20 | 1=
+
{{dcl|num=1|since=c++20|1=
template<class S, class I>
+
template< class S, class I >
  concept sized_sentinel_for =
+
    concept sized_sentinel_for =
    std::sentinel_for<S, I> &&
+
        std::sentinel_for<S, I> &&
    !std::disable_sized_sentinel_for<std::remove_cv_t<S>, std::remove_cv_t<I>> &&
+
        !std::disable_sized_sentinel_for<std::remove_cv_t<S>,
    requires(const I& i, const S& s) {
+
                                        std::remove_cv_t<I>> &&
      { s - i } -> std::same_as<std::iter_difference_t<I>>;
+
        requires(const I& i, const S& s) {
      { i - s } -> std::same_as<std::iter_difference_t<I>>;
+
            { s - i } -> std::same_as<std::iter_difference_t<I>>;
    };
+
            { i - s } -> std::same_as<std::iter_difference_t<I>>;
 +
        };
 
}}
 
}}
{{dcl | num=2 | since=c++20 | 1=
+
{{dcl|num=2|since=c++20|1=
template<class S, class I>
+
template< class S, class I >
  inline constexpr bool disable_sized_sentinel_for = false;
+
    inline constexpr bool disable_sized_sentinel_for = false;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
@1@ The {{tt|sized_sentinel_for}} concept specifies that an object of the iterator type {{tt|I}} and an object of the sentinel type {{tt|S}} can be subtracted to compute the distance between them in constant time.
+
@1@ The {{tt|sized_sentinel_for}} concept specifies that an object of the iterator type {{tt|I}} and an object of the sentinel type {{tt|S}} can be subtracted to compute the distance between them in constant time.
@2@ The {{tt|disable_sized_sentinel_for}} variable template can be used to prevent iterators and sentinels that can be subtracted but do not actually model {{tt|sized_sentinel_for}} from satisfying the concept.<br>
+
The variable template is allowed to be specialized for cv-unqualified non-array object type {{tt|S}} and {{tt|I}}, as long as at least one of which is a program-defined type. Such specializations shall be usable in [[cpp/language/constant expression|constant expressions]] and have type {{c|const bool}}.
+
  
===Semantic requirements===
+
@2@ The {{tt|disable_sized_sentinel_for}} variable template can be used to prevent iterators and sentinels that can be subtracted but do not actually model {{tt|sized_sentinel_for}} from satisfying the concept.
Let {{tt|i}} be an iterator of type {{tt|I}}, and {{tt|s}} a sentinel of type {{tt|S}} such that {{tt|[i, s)}} denotes a range. Let {{tt|n}} be the smallest number of applications of {{tt|++i}} necessary to make {{c|1=bool(i == s)}} be {{tt|true}}. {{tt|I}} and {{tt|S}} model {{tt|sized_sentinel_for&lt;S, I>}} only if:
+
@@ A program may specialize {{tt|disable_sized_sentinel_for}} for cv-unqualified non-array object type {{tt|S}} and {{tt|I}}, as long as at least one of which is a {{lsd|cpp/language/type#Program-defined type}}. Such specializations are usable in {{lt|cpp/language/constant expression}}s and have type {{c/core|const bool}}.
  
* If {{tt|n}} is representable by {{c|std::iter_difference_t<I>}}, then {{c|s - i}} is well-defined and equals {{tt|n}}; and
+
===Semantic requirements===
* If {{tt|-n}} is representable by {{c|std::iter_difference_t<I>}}, then {{c|i - s}} is well-defined and equals {{tt|-n}}.
+
Let {{c|i}} be an iterator of type {{tt|I}}, and {{c|s}} a sentinel of type {{tt|S}} such that {{range|i|s}} denotes a range. Let {{c|n}} be the smallest number of applications of {{c|++i}} necessary to make {{c|1=bool(i == s)}} be {{c|true}}. {{tt|I}} and {{tt|S}} model {{tt|sized_sentinel_for&lt;S, I>}} only if all following conditions are satisfied:
* Subtraction between {{tt|i}} and {{tt|s}} has constant time complexity.
+
* If {{c|n}} is representable by {{c/core|std::iter_difference_t<I>}}, then {{c|s - i}} is well-defined and equals {{c|n}}.
 +
* If {{c|-n}} is representable by {{c/core|std::iter_difference_t<I>}}, then {{c|i - s}} is well-defined and equals {{c|-n}}.
  
 
{{cpp/concepts/equality preservation}}
 
{{cpp/concepts/equality preservation}}
Line 35: Line 35:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/ranges/dsc SizedRange}}
+
{{dsc inc|cpp/ranges/dsc sized_range}}
{{dsc inc | cpp/ranges/dsc size}}
+
{{dsc inc|cpp/ranges/dsc size}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|es|ja|ru|zh}}
+
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 17:49, 13 March 2024

 
 
Iterator library
Iterator concepts
sized_sentinel_for
(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 S, class I >

    concept sized_sentinel_for =
        std::sentinel_for<S, I> &&
        !std::disable_sized_sentinel_for<std::remove_cv_t<S>,
                                         std::remove_cv_t<I>> &&
        requires(const I& i, const S& s) {
            { s - i } -> std::same_as<std::iter_difference_t<I>>;
            { i - s } -> std::same_as<std::iter_difference_t<I>>;

        };
(1) (since C++20)
template< class S, class I >
    inline constexpr bool disable_sized_sentinel_for = false;
(2) (since C++20)
1) The sized_sentinel_for concept specifies that an object of the iterator type I and an object of the sentinel type S can be subtracted to compute the distance between them in constant time.
2) The disable_sized_sentinel_for variable template can be used to prevent iterators and sentinels that can be subtracted but do not actually model sized_sentinel_for from satisfying the concept.
A program may specialize disable_sized_sentinel_for for cv-unqualified non-array object type S and I, as long as at least one of which is a program-defined type. Such specializations are usable in constant expressions and have type const bool.

Contents

[edit] Semantic requirements

Let i be an iterator of type I, and s a sentinel of type S such that [is) denotes a range. Let n be the smallest number of applications of ++i necessary to make bool(i == s) be true. I and S model sized_sentinel_for<S, I> only if all following conditions are satisfied:

[edit] Equality preservation

Expressions declared in requires expressions of the standard library concepts are required to be equality-preserving (except where stated otherwise).

[edit] Implicit expression variations

A requires expression that uses an expression that is non-modifying for some constant lvalue operand also requires implicit expression variations.

[edit] See also

specifies that a range knows its size in constant time
(concept) [edit]
returns an integer equal to the size of a range
(customization point object)[edit]