[predef.iterators] (original) (raw)
23 Iterators library [iterators]
23.5 Iterator adaptors [predef.iterators]
23.5.1 Reverse iterators [reverse.iterators]
23.5.1.1 General [reverse.iterators.general]
Class template reverse_iterator is an iterator adaptor that iterates from the end of the sequence defined by its underlying iterator to the beginning of that sequence.
23.5.1.2 Class template reverse_iterator [reverse.iterator]
namespace std { template<class Iterator> class reverse_iterator { public: using iterator_type = Iterator;using iterator_concept = see below;using iterator_category = see below;using value_type = iter_value_t<Iterator>;using difference_type = iter_difference_t<Iterator>;using pointer = typename iterator_traits<Iterator>::pointer;using reference = iter_reference_t<Iterator>;constexpr reverse_iterator();constexpr explicit reverse_iterator(Iterator x);template<class U> constexpr reverse_iterator(const reverse_iterator<U>& u);template<class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);constexpr Iterator base() const;constexpr reference operator*() const;constexpr pointer operator->() const requires see below;constexpr reverse_iterator& operator++();constexpr reverse_iterator operator++(int);constexpr reverse_iterator& operator--();constexpr reverse_iterator operator--(int);constexpr reverse_iterator operator+ (difference_type n) const;constexpr reverse_iterator& operator+=(difference_type n);constexpr reverse_iterator operator- (difference_type n) const;constexpr reverse_iterator& operator-=(difference_type n);constexpr unspecified operator[](difference_type n) const;friend constexpr iter_rvalue_reference_t<Iterator> iter_move(const reverse_iterator& i) noexcept(see below);template<indirectly_swappable<Iterator> Iterator2> friend constexpr void iter_swap(const reverse_iterator& x,const reverse_iterator<Iterator2>& y) noexcept(see below);protected: Iterator current;};}
The member typedef-name iterator_category denotes
- random_access_iterator_tag if the typeiterator_traits<Iterator>::iterator_category modelsderived_from<random_access_iterator_tag>, and
- iterator_traits<Iterator>::iterator_category otherwise.
23.5.1.3 Requirements [reverse.iter.requirements]
Additionally,Iteratorshall either meet the requirements of a_Cpp17RandomAccessIterator_ ([random.access.iterators]) or modelrandom_access_iterator ([iterator.concept.random.access]) if the definitions of any of the members
- operator+,operator-,operator+=,operator-= ([reverse.iter.nav]), or
- operator[] ([reverse.iter.elem]),
or the non-member operators ([reverse.iter.cmp])
- operator<,operator>,operator<=,operator>=,operator-, oroperator+ ([reverse.iter.nonmember])
are instantiated ([temp.inst]).
23.5.1.4 Construction and assignment [reverse.iter.cons]
constexpr reverse_iterator();
Effects: Value-initializescurrent.
Iterator operations applied to the resulting iterator have defined behavior if and only if the corresponding operations are defined on a value-initialized iterator of typeIterator.
constexpr explicit reverse_iterator(Iterator x);
Effects: Initializescurrentwith x.
template<class U> constexpr reverse_iterator(const reverse_iterator<U>& u);
Effects: Initializescurrentwithu.current.
template<class U> constexpr reverse_iterator& operator=(const reverse_iterator<U>& u);
Effects: Assigns u.base() to current.
23.5.1.6 Element access [reverse.iter.elem]
constexpr reference operator*() const;
Effects: As if by:Iterator tmp = current;return *--tmp;
constexpr pointer operator->() const requires (is_pointer_v<Iterator> || requires (const Iterator i) { i.operator->(); });
Effects:
- If Iterator is a pointer type, equivalent to:return prev(current);
- Otherwise, equivalent to:return prev(current).operator->();
constexpr _unspecified_ operator[](difference_type n) const;
23.5.1.7 Navigation [reverse.iter.nav]
constexpr reverse_iterator operator+(difference_type n) const;
Returns: reverse_iterator(current-n).
constexpr reverse_iterator operator-(difference_type n) const;
Returns: reverse_iterator(current+n).
constexpr reverse_iterator& operator++();
Effects: As if by: --current;
constexpr reverse_iterator operator++(int);
Effects: As if by:reverse_iterator tmp = *this;--current;return tmp;
constexpr reverse_iterator& operator--();
Effects: As if by ++current.
constexpr reverse_iterator operator--(int);
Effects: As if by:reverse_iterator tmp = *this;++current;return tmp;
constexpr reverse_iterator& operator+=(difference_type n);
Effects: As if by: current -= n;
constexpr reverse_iterator& operator-=(difference_type n);
Effects: As if by: current += n;
23.5.1.8 Comparisons [reverse.iter.cmp]
template<class Iterator1, class Iterator2> constexpr bool operator==( const reverse_iterator<Iterator1>& x,const reverse_iterator<Iterator2>& y);
Constraints: x.base() == y.base() is well-formed and convertible to bool.
Returns: x.base() == y.base().
template<class Iterator1, class Iterator2> constexpr bool operator!=( const reverse_iterator<Iterator1>& x,const reverse_iterator<Iterator2>& y);
Constraints: x.base() != y.base() is well-formed and convertible to bool.
Returns: x.base() != y.base().
template<class Iterator1, class Iterator2> constexpr bool operator<( const reverse_iterator<Iterator1>& x,const reverse_iterator<Iterator2>& y);
Constraints: x.base() > y.base() is well-formed and convertible to bool.
Returns: x.base() > y.base().
template<class Iterator1, class Iterator2> constexpr bool operator>( const reverse_iterator<Iterator1>& x,const reverse_iterator<Iterator2>& y);
Constraints: x.base() < y.base() is well-formed and convertible to bool.
Returns: x.base() < y.base().
template<class Iterator1, class Iterator2> constexpr bool operator<=( const reverse_iterator<Iterator1>& x,const reverse_iterator<Iterator2>& y);
Constraints: x.base() >= y.base() is well-formed and convertible to bool.
Returns: x.base() >= y.base().
template<class Iterator1, class Iterator2> constexpr bool operator>=( const reverse_iterator<Iterator1>& x,const reverse_iterator<Iterator2>& y);
Constraints: x.base() <= y.base() is well-formed and convertible to bool.
Returns: x.base() <= y.base().
template<class Iterator1, [three_way_comparable_with](cmp.concept#concept:three%5Fway%5Fcomparable%5Fwith "17.11.4 Concept three_way_comparable [cmp.concept]")<Iterator1> Iterator2> constexpr compare_three_way_result_t<Iterator1, Iterator2> operator<=>(const reverse_iterator<Iterator1>& x,const reverse_iterator<Iterator2>& y);
Returns: y.base() <=> x.base().
[Note 1:
The argument order in the Returns: element is reversed because this is a reverse iterator.
— _end note_]
23.5.1.9 Non-member functions [reverse.iter.nonmember]
template<class Iterator1, class Iterator2> constexpr auto operator-( const reverse_iterator<Iterator1>& x,const reverse_iterator<Iterator2>& y) -> decltype(y.base() - x.base());
Returns: y.base() - x.base().
template<class Iterator> constexpr reverse_iterator<Iterator> operator+( iter_difference_t<Iterator> n,const reverse_iterator<Iterator>& x);
Returns: reverse_iterator<Iterator>(x.base() - n).
friend constexpr iter_rvalue_reference_t<Iterator> iter_move(const reverse_iterator& i) noexcept(_see below_);
Effects: Equivalent to:auto tmp = i.base();return ranges::iter_move(--tmp);
Remarks: The expression in noexcept is equivalent to:is_nothrow_copy_constructible_v<Iterator> && noexcept(ranges::iter_move(--declval<Iterator&>()))
template<[indirectly_swappable](alg.req.ind.swap#concept:indirectly%5Fswappable "23.3.7.4 Concept indirectly_swappable [alg.req.ind.swap]")<Iterator> Iterator2> friend constexpr void iter_swap(const reverse_iterator& x,const reverse_iterator<Iterator2>& y) noexcept(_see below_);
Effects: Equivalent to:auto xtmp = x.base();auto ytmp = y.base(); ranges::iter_swap(--xtmp, --ytmp);
Remarks: The expression in noexcept is equivalent to:is_nothrow_copy_constructible_v<Iterator> &&is_nothrow_copy_constructible_v<Iterator2> && noexcept(ranges::iter_swap(--declval<Iterator&>(), --declval<Iterator2&>()))
template<class Iterator> constexpr reverse_iterator<Iterator> make_reverse_iterator(Iterator i);
Returns: reverse_iterator<Iterator>(i).
23.5.2 Insert iterators [insert.iterators]
23.5.2.1 General [insert.iterators.general]
To make it possible to deal with insertion in the same way as writing into an array, a special kind of iterator adaptors, calledinsert iterators, are provided in the library.
With regular iterator classes,while (first != last) *result++ = *first++;causes a range [first, last) to be copied into a range starting with result.
The same code withresultbeing an insert iterator will insert corresponding elements into the container.
This device allows all of the copying algorithms in the library to work in theinsert modeinstead of the regular overwrite mode.
An insert iterator is constructed from a container and possibly one of its iterators pointing to where insertion takes place if it is neither at the beginning nor at the end of the container.
Insert iterators meet the requirements of output iterators.
operator*returns the insert iterator itself.
The assignmentoperator=(const T& x)is defined on insert iterators to allow writing into them, it insertsxright before where the insert iterator is pointing.
In other words, an insert iterator is like a cursor pointing into the container where the insertion takes place.
back_insert_iteratorinserts elements at the end of a container,front_insert_iteratorinserts elements at the beginning of a container, andinsert_iteratorinserts elements where the iterator points to in a container.
back_inserter,front_inserter, andinserterare three functions making the insert iterators out of a container.
23.5.2.2 Class template back_insert_iterator [back.insert.iterator]
namespace std { template<class Container> class back_insert_iterator { protected: Container* container = nullptr;public: using iterator_category = output_iterator_tag;using value_type = void;using difference_type = ptrdiff_t;using pointer = void;using reference = void;using container_type = Container;constexpr back_insert_iterator() noexcept = default;constexpr explicit back_insert_iterator(Container& x);constexpr back_insert_iterator& operator=(const typename Container::value_type& value);constexpr back_insert_iterator& operator=(typename Container::value_type&& value);constexpr back_insert_iterator& operator*();constexpr back_insert_iterator& operator++();constexpr back_insert_iterator operator++(int);};}
23.5.2.2.1 Operations [back.insert.iter.ops]
constexpr explicit back_insert_iterator(Container& x);
Effects: Initializescontainerwith addressof(x).
constexpr back_insert_iterator& operator=(const typename Container::value_type& value);
Effects: As if by: container->push_back(value);
constexpr back_insert_iterator& operator=(typename Container::value_type&& value);
Effects: As if by: container->push_back(std::move(value));
constexpr back_insert_iterator& operator*();
constexpr back_insert_iterator& operator++();constexpr back_insert_iterator operator++(int);
23.5.2.2.2 back_inserter [back.inserter]
template<class Container> constexpr back_insert_iterator<Container> back_inserter(Container& x);
Returns: back_insert_iterator<Container>(x).
23.5.2.3 Class template front_insert_iterator [front.insert.iterator]
namespace std { template<class Container> class front_insert_iterator { protected: Container* container = nullptr;public: using iterator_category = output_iterator_tag;using value_type = void;using difference_type = ptrdiff_t;using pointer = void;using reference = void;using container_type = Container;constexpr front_insert_iterator() noexcept = default;constexpr explicit front_insert_iterator(Container& x);constexpr front_insert_iterator& operator=(const typename Container::value_type& value);constexpr front_insert_iterator& operator=(typename Container::value_type&& value);constexpr front_insert_iterator& operator*();constexpr front_insert_iterator& operator++();constexpr front_insert_iterator operator++(int);};}
23.5.2.3.1 Operations [front.insert.iter.ops]
constexpr explicit front_insert_iterator(Container& x);
Effects: Initializescontainerwith addressof(x).
constexpr front_insert_iterator& operator=(const typename Container::value_type& value);
Effects: As if by: container->push_front(value);
constexpr front_insert_iterator& operator=(typename Container::value_type&& value);
Effects: As if by: container->push_front(std::move(value));
constexpr front_insert_iterator& operator*();
constexpr front_insert_iterator& operator++();constexpr front_insert_iterator operator++(int);
23.5.2.3.2 front_inserter [front.inserter]
template<class Container> constexpr front_insert_iterator<Container> front_inserter(Container& x);
Returns: front_insert_iterator<Container>(x).
23.5.2.4 Class template insert_iterator [insert.iterator]
namespace std { template<class Container> class insert_iterator { protected: Container* container = nullptr; ranges::iterator_t<Container> iter = ranges::iterator_t<Container>();public: using iterator_category = output_iterator_tag;using value_type = void;using difference_type = ptrdiff_t;using pointer = void;using reference = void;using container_type = Container; insert_iterator() = default;constexpr insert_iterator(Container& x, ranges::iterator_t<Container> i);constexpr insert_iterator& operator=(const typename Container::value_type& value);constexpr insert_iterator& operator=(typename Container::value_type&& value);constexpr insert_iterator& operator*();constexpr insert_iterator& operator++();constexpr insert_iterator& operator++(int);};}
23.5.2.4.1 Operations [insert.iter.ops]
constexpr insert_iterator(Container& x, ranges::iterator_t<Container> i);
Effects: Initializescontainerwith addressof(x) anditerwith i.
constexpr insert_iterator& operator=(const typename Container::value_type& value);
Effects: As if by:iter = container->insert(iter, value);++iter;
constexpr insert_iterator& operator=(typename Container::value_type&& value);
Effects: As if by:iter = container->insert(iter, std::move(value));++iter;
constexpr insert_iterator& operator*();
constexpr insert_iterator& operator++();constexpr insert_iterator& operator++(int);
23.5.2.4.2 inserter [inserter]
template<class Container> constexpr insert_iterator<Container> inserter(Container& x, ranges::iterator_t<Container> i);
Returns: insert_iterator<Container>(x, i).
23.5.3 Move iterators and sentinels [move.iterators]
23.5.3.1 General [move.iterators.general]
Class template move_iterator is an iterator adaptor with the same behavior as the underlying iterator except that its indirection operator implicitly converts the value returned by the underlying iterator's indirection operator to an rvalue.
Some generic algorithms can be called with move iterators to replace copying with moving.
[Example 1: list<string> s; vector<string> v1(s.begin(), s.end()); vector<string> v2(make_move_iterator(s.begin()), make_move_iterator(s.end())); — _end example_]
23.5.3.2 Class template move_iterator [move.iterator]
namespace std { template<class Iterator> class move_iterator { public: using iterator_type = Iterator;using iterator_concept = input_iterator_tag;using iterator_category = see below;using value_type = iter_value_t<Iterator>;using difference_type = iter_difference_t<Iterator>;using pointer = Iterator;using reference = iter_rvalue_reference_t<Iterator>;constexpr move_iterator();constexpr explicit move_iterator(Iterator i);template<class U> constexpr move_iterator(const move_iterator<U>& u);template<class U> constexpr move_iterator& operator=(const move_iterator<U>& u);constexpr iterator_type base() const &;constexpr iterator_type base() &&;constexpr reference operator*() const;constexpr move_iterator& operator++();constexpr auto operator++(int);constexpr move_iterator& operator--();constexpr move_iterator operator--(int);constexpr move_iterator operator+(difference_type n) const;constexpr move_iterator& operator+=(difference_type n);constexpr move_iterator operator-(difference_type n) const;constexpr move_iterator& operator-=(difference_type n);constexpr reference operator[](difference_type n) const;template<sentinel_for<Iterator> S> friend constexpr bool operator==(const move_iterator& x, const move_sentinel<S>& y);template<sized_sentinel_for<Iterator> S> friend constexpr iter_difference_t<Iterator> operator-(const move_sentinel<S>& x, const move_iterator& y);template<sized_sentinel_for<Iterator> S> friend constexpr iter_difference_t<Iterator> operator-(const move_iterator& x, const move_sentinel<S>& y);friend constexpr iter_rvalue_reference_t<Iterator> iter_move(const move_iterator& i) noexcept(noexcept(ranges::iter_move(i.current)));template<indirectly_swappable<Iterator> Iterator2> friend constexpr void iter_swap(const move_iterator& x, const move_iterator<Iterator2>& y) noexcept(noexcept(ranges::iter_swap(x.current, y.current)));private: Iterator current; };}
The member typedef-name iterator_category denotes
- random_access_iterator_tag if the typeiterator_traits<Iterator>::iterator_category modelsderived_from<random_access_iterator_tag>, and
- iterator_traits<Iterator>::iterator_category otherwise.
23.5.3.4 Construction and assignment [move.iter.cons]
constexpr move_iterator();
Effects: Constructs a move_iterator, value-initializingcurrent.
Iterator operations applied to the resulting iterator have defined behavior if and only if the corresponding operations are defined on a value-initialized iterator of type Iterator.
constexpr explicit move_iterator(Iterator i);
Effects: Constructs a move_iterator, initializingcurrent with std::move(i).
template<class U> constexpr move_iterator(const move_iterator<U>& u);
Mandates: U is convertible to Iterator.
Effects: Constructs a move_iterator, initializingcurrent with u.base().
template<class U> constexpr move_iterator& operator=(const move_iterator<U>& u);
Mandates: U is convertible to Iterator.
Effects: Assigns u.base() tocurrent.
23.5.3.5 Conversion [move.iter.op.conv]
constexpr Iterator base() const &;
constexpr Iterator base() &&;
Returns: std::move(current).
23.5.3.6 Element access [move.iter.elem]
constexpr reference operator*() const;
Effects: Equivalent to: return ranges::iter_move(current);
constexpr reference operator[](difference_type n) const;
Effects: Equivalent to: return ranges::iter_move(current + n);
23.5.3.7 Navigation [move.iter.nav]
constexpr move_iterator& operator++();
Effects: As if by ++current.
constexpr auto operator++(int);
Effects: If Iterator models forward_iterator, equivalent to:move_iterator tmp = *this;++current;return tmp;
Otherwise, equivalent to ++current.
constexpr move_iterator& operator--();
Effects: As if by --current.
constexpr move_iterator operator--(int);
Effects: As if by:move_iterator tmp = *this;--current;return tmp;
constexpr move_iterator operator+(difference_type n) const;
Returns: move_iterator(current + n).
constexpr move_iterator& operator+=(difference_type n);
Effects: As if by: current += n;
constexpr move_iterator operator-(difference_type n) const;
Returns: move_iterator(current - n).
constexpr move_iterator& operator-=(difference_type n);
Effects: As if by: current -= n;
23.5.3.8 Comparisons [move.iter.op.comp]
template<class Iterator1, class Iterator2> constexpr bool operator==(const move_iterator<Iterator1>& x,const move_iterator<Iterator2>& y);template<[sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "23.3.4.7 Concept sentinel_for [iterator.concept.sentinel]")<Iterator> S> friend constexpr bool operator==(const move_iterator& x,const move_sentinel<S>& y);
Constraints: x.base() == y.base() is well-formed and convertible to bool.
Returns: x.base() == y.base().
template<class Iterator1, class Iterator2> constexpr bool operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
Constraints: x.base() < y.base() is well-formed and convertible to bool.
Returns: x.base() < y.base().
template<class Iterator1, class Iterator2> constexpr bool operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
Constraints: y.base() < x.base() is well-formed and convertible to bool.
template<class Iterator1, class Iterator2> constexpr bool operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
Constraints: y.base() < x.base() is well-formed and convertible to bool.
template<class Iterator1, class Iterator2> constexpr bool operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y);
Constraints: x.base() < y.base() is well-formed and convertible to bool.
template<class Iterator1, [three_way_comparable_with](cmp.concept#concept:three%5Fway%5Fcomparable%5Fwith "17.11.4 Concept three_way_comparable [cmp.concept]")<Iterator1> Iterator2> constexpr compare_three_way_result_t<Iterator1, Iterator2> operator<=>(const move_iterator<Iterator1>& x,const move_iterator<Iterator2>& y);
Returns: x.base() <=> y.base().
23.5.3.9 Non-member functions [move.iter.nonmember]
template<class Iterator1, class Iterator2> constexpr auto operator-( const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base());template<[sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized%5Fsentinel%5Ffor "23.3.4.8 Concept sized_sentinel_for [iterator.concept.sizedsentinel]")<Iterator> S> friend constexpr iter_difference_t<Iterator> operator-(const move_sentinel<S>& x, const move_iterator& y);template<[sized_sentinel_for](iterator.concept.sizedsentinel#concept:sized%5Fsentinel%5Ffor "23.3.4.8 Concept sized_sentinel_for [iterator.concept.sizedsentinel]")<Iterator> S> friend constexpr iter_difference_t<Iterator> operator-(const move_iterator& x, const move_sentinel<S>& y);
Returns: x.base() - y.base().
template<class Iterator> constexpr move_iterator<Iterator> operator+(iter_difference_t<Iterator> n, const move_iterator<Iterator>& x);
Constraints: x + n is well-formed and has type Iterator.
friend constexpr iter_rvalue_reference_t<Iterator> iter_move(const move_iterator& i) noexcept(noexcept(ranges::iter_move(i.current)));
Effects: Equivalent to: return ranges::iter_move(i.current);
template<[indirectly_swappable](alg.req.ind.swap#concept:indirectly%5Fswappable "23.3.7.4 Concept indirectly_swappable [alg.req.ind.swap]")<Iterator> Iterator2> friend constexpr void iter_swap(const move_iterator& x, const move_iterator<Iterator2>& y) noexcept(noexcept(ranges::iter_swap(x.current, y.current)));
Effects: Equivalent to: ranges::iter_swap(x.current, y.current).
template<class Iterator> constexpr move_iterator<Iterator> make_move_iterator(Iterator i);
Returns: move_iterator<Iterator>(std::move(i)).
23.5.3.10 Class template move_sentinel [move.sentinel]
Class template move_sentinel is a sentinel adaptor useful for denoting ranges together with move_iterator.
When an input iterator typeI and sentinel type S model sentinel_for<S, I>,move_sentinel<S> and move_iterator<I> modelsentinel_for<move_sentinel<S>, move_iterator<I>> as well.
[Example 1:
A move_if algorithm is easily implemented withcopy_if using move_iterator and move_sentinel:template<input_iterator I, sentinel_for<I> S, weakly_incrementable O,indirect_unary_predicate<I> Pred> requires indirectly_movable<I, O> void move_if(I first, S last, O out, Pred pred) { std::ranges::copy_if(move_iterator<I>{first}, move_sentinel<S>{last}, out, pred);}
— _end example_]
namespace std { template<semiregular S> class move_sentinel { public: constexpr move_sentinel();constexpr explicit move_sentinel(S s);template<class S2> requires convertible_to<const S2&, S> constexpr move_sentinel(const move_sentinel<S2>& s);template<class S2> requires assignable_from<S&, const S2&> constexpr move_sentinel& operator=(const move_sentinel<S2>& s);constexpr S base() const;private: S last; };}
23.5.3.11 Operations [move.sent.ops]
constexpr move_sentinel();
Effects: Value-initializes last.
If is_trivially_default_constructible_v<S> is true, then this constructor is a constexpr constructor.
constexpr explicit move_sentinel(S s);
Effects: Initializes last with std::move(s).
template<class S2> requires [convertible_to](concept.convertible#concept:convertible%5Fto "18.4.4 Concept convertible_to [concept.convertible]")<const S2&, S> constexpr move_sentinel(const move_sentinel<S2>& s);
Effects: Initializes last with s.last.
template<class S2> requires [assignable_from](concept.assignable#concept:assignable%5Ffrom "18.4.8 Concept assignable_from [concept.assignable]")<S&, const S2&> constexpr move_sentinel& operator=(const move_sentinel<S2>& s);
Effects: Equivalent to: last = s.last; return *this;
constexpr S base() const;
23.5.4 Common iterators [iterators.common]
23.5.4.1 Class template common_iterator [common.iterator]
Class template common_iterator is an iterator/sentinel adaptor that is capable of representing a non-common range of elements (where the types of the iterator and sentinel differ) as a common range (where they are the same).
It does this by holding either an iterator or a sentinel, and implementing the equality comparison operators appropriately.
[Note 1:
The common_iterator type is useful for interfacing with legacy code that expects the begin and end of a range to have the same type.
— _end note_]
[Example 1: template<class ForwardIterator> void fun(ForwardIterator begin, ForwardIterator end); list<int> s;using CI = common_iterator<counted_iterator<list<int>::iterator>, default_sentinel_t>; fun(CI(counted_iterator(s.begin(), 10)), CI(default_sentinel)); — _end example_]
namespace std { template<input_or_output_iterator I, sentinel_for<I> S> requires (<I, S> && copyable<I>) class common_iterator { public: constexpr common_iterator() = default;constexpr common_iterator(I i);constexpr common_iterator(S s);template<class I2, class S2> requires convertible_to<const I2&, I> && convertible_to<const S2&, S> constexpr common_iterator(const common_iterator<I2, S2>& x);template<class I2, class S2> requires convertible_to<const I2&, I> && convertible_to<const S2&, S> && assignable_from<I&, const I2&> && assignable_from<S&, const S2&> common_iterator& operator=(const common_iterator<I2, S2>& x);decltype(auto) operator*();decltype(auto) operator*() const requires dereferenceable<const I>;decltype(auto) operator->() const requires see below; common_iterator& operator++();decltype(auto) operator++(int);template<class I2, sentinel_for<I> S2> requires sentinel_for<S, I2> friend bool operator==( const common_iterator& x, const common_iterator<I2, S2>& y);template<class I2, sentinel_for<I> S2> requires sentinel_for<S, I2> && equality_comparable_with<I, I2> friend bool operator==( const common_iterator& x, const common_iterator<I2, S2>& y);template<sized_sentinel_for<I> I2, sized_sentinel_for<I> S2> requires sized_sentinel_for<S, I2> friend iter_difference_t<I2> operator-( const common_iterator& x, const common_iterator<I2, S2>& y);friend iter_rvalue_reference_t<I> iter_move(const common_iterator& i) noexcept(noexcept(ranges::iter_move(declval<const I&>()))) requires input_iterator<I>;template<indirectly_swappable<I> I2, class S2> friend void iter_swap(const common_iterator& x, const common_iterator<I2, S2>& y) noexcept(noexcept(ranges::iter_swap(declval<const I&>(), declval<const I2&>())));private: variant<I, S> v_; };template<class I, class S> struct incrementable_traits<common_iterator<I, S>> { using difference_type = iter_difference_t<I>;};template<input_iterator I, class S> struct iterator_traits<common_iterator<I, S>> { using iterator_concept = see below;using iterator_category = see below;using value_type = iter_value_t<I>;using difference_type = iter_difference_t<I>;using pointer = see below;using reference = iter_reference_t<I>;};}
23.5.4.2 Associated types [common.iter.types]
The nested typedef-names of the specialization ofiterator_traits for common_iterator<I, S> are defined as follows.
- iterator_concept denotes forward_iterator_tagif I models forward_iterator; otherwise it denotes input_iterator_tag.
- iterator_category denotesforward_iterator_tagif iterator_traits<I>::iterator_categorymodels derived_from<forward_iterator_tag>; otherwise it denotes input_iterator_tag.
- If the expression a.operator->() is well-formed, where a is an lvalue of type const common_iterator<I, S>, then pointer denotes the type of that expression.
Otherwise, pointer denotes void.
23.5.4.3 Constructors and conversions [common.iter.const]
constexpr common_iterator(I i);
Effects: Initializes v_ as if by v_{in_place_type<I>, std::move(i)}.
constexpr common_iterator(S s);
Effects: Initializes v_ as if byv_{in_place_type<S>, std::move(s)}.
template<class I2, class S2> requires [convertible_to](concept.convertible#concept:convertible%5Fto "18.4.4 Concept convertible_to [concept.convertible]")<const I2&, I> && [convertible_to](concept.convertible#concept:convertible%5Fto "18.4.4 Concept convertible_to [concept.convertible]")<const S2&, S> constexpr common_iterator(const common_iterator<I2, S2>& x);
Preconditions: x.v_.valueless_by_exception() is false.
Effects: Initializes v_ as if byv_{in_place_index<i>, get<i>(x.v_)}, where i is x.v_.index().
Preconditions: x.v_.valueless_by_exception() is false.
Effects: Equivalent to:
- If v_.index() == x.v_.index(), thenget<i>(v_) = get<i>(x.v_).
- Otherwise, v_.emplace<i>(get<i>(x.v_)).
where i is x.v_.index().
23.5.4.4 Accessors [common.iter.access]
decltype(auto) operator*();decltype(auto) operator*() const requires [_dereferenceable_](iterator.synopsis#concept:dereferenceable "23.2 Header <iterator> synopsis [iterator.synopsis]")<const I>;
Preconditions: holds_alternative<I>(v_).
Effects: Equivalent to: return *get<I>(v_);
decltype(auto) operator->() const requires _see below_;
Preconditions: holds_alternative<I>(v_).
Effects:
- If I is a pointer type or if the expressionget<I>(v_).operator->() is well-formed, equivalent to: return get<I>(v_);
- Otherwise, if iter_reference_t<I> is a reference type, equivalent to:auto&& tmp = *get<I>(v_);return addressof(tmp);
- Otherwise, equivalent to:return proxy(*get<I>(v_)); where_proxy_ is the exposition-only class:class proxy { iter_value_t<I> keep_;proxy(iter_reference_t<I>&& x) : keep_(std::move(x)) {} public: const iter_value_t<I>* operator->() const { return addressof(keep_);} };
23.5.4.5 Navigation [common.iter.nav]
common_iterator& operator++();
Preconditions: holds_alternative<I>(v_).
Effects: Equivalent to ++get<I>(v_).
decltype(auto) operator++(int);
Preconditions: holds_alternative<I>(v_).
Effects: If I models forward_iterator, equivalent to:common_iterator tmp = *this;++*this;return tmp;
Otherwise, equivalent to: return get<I>(v_)++;
23.5.4.6 Comparisons [common.iter.cmp]
template<class I2, [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "23.3.4.7 Concept sentinel_for [iterator.concept.sentinel]")<I> S2> requires [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "23.3.4.7 Concept sentinel_for [iterator.concept.sentinel]")<S, I2> friend bool operator==( const common_iterator& x, const common_iterator<I2, S2>& y);
Preconditions: x.v_.valueless_by_exception() and y.v_.valueless_by_exception()are each false.
Returns: true if i == j, and otherwise get<i>(x.v_) == get<j>(y.v_), where i is x.v_.index() and j is y.v_.index().
template<class I2, [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "23.3.4.7 Concept sentinel_for [iterator.concept.sentinel]")<I> S2> requires [sentinel_for](iterator.concept.sentinel#concept:sentinel%5Ffor "23.3.4.7 Concept sentinel_for [iterator.concept.sentinel]")<S, I2> && equality_comparable_with<I, I2> friend bool operator==( const common_iterator& x, const common_iterator<I2, S2>& y);
Preconditions: x.v_.valueless_by_exception() and y.v_.valueless_by_exception()are each false.
Returns: true if i and j are each 1, and otherwiseget<i>(x.v_) == get<j>(y.v_), wherei is x.v_.index() and j is y.v_.index().
Preconditions: x.v_.valueless_by_exception() and y.v_.valueless_by_exception()are each false.
Returns: 0 if i and j are each 1, and otherwiseget<i>(x.v_) - get<j>(y.v_), wherei is x.v_.index() and j is y.v_.index().
23.5.4.7 Customizations [common.iter.cust]
friend iter_rvalue_reference_t<I> iter_move(const common_iterator& i) noexcept(noexcept(ranges::iter_move(declval<const I&>()))) requires [input_iterator](iterator.concept.input#concept:input%5Fiterator "23.3.4.9 Concept input_iterator [iterator.concept.input]")<I>;
Preconditions: holds_alternative<I>(v_).
Effects: Equivalent to: return ranges::iter_move(get<I>(i.v_));
template<[indirectly_swappable](alg.req.ind.swap#concept:indirectly%5Fswappable "23.3.7.4 Concept indirectly_swappable [alg.req.ind.swap]")<I> I2, class S2> friend void iter_swap(const common_iterator& x, const common_iterator<I2, S2>& y) noexcept(noexcept(ranges::iter_swap(declval<const I&>(), declval<const I2&>())));
Preconditions: holds_alternative<I>(x.v_) and holds_alternative<I2>(y.v_)are each true.
Effects: Equivalent to ranges::iter_swap(get<I>(x.v_), get<I2>(y.v_)).
23.5.5 Default sentinel [default.sentinel]
namespace std { struct default_sentinel_t { };}
Class default_sentinel_t is an empty type used to denote the end of a range.
It can be used together with iterator types that know the bound of their range (e.g., counted_iterator ([counted.iterator])).
23.5.6 Counted iterators [iterators.counted]
23.5.6.1 Class template counted_iterator [counted.iterator]
Class template counted_iterator is an iterator adaptor with the same behavior as the underlying iterator except that it keeps track of the distance to the end of its range.
It can be used together with default_sentinelin calls to generic algorithms to operate on a range of N elements starting at a given position without needing to know the end position a priori.
[Example 1: list<string> s; vector<string> v; ranges::copy(counted_iterator(s.begin(), 10), default_sentinel, back_inserter(v)); — _end example_]
Two values i1 and i2 of typescounted_iterator<I1>andcounted_iterator<I2>refer to elements of the same sequence if and only ifnext(i1.base(), i1.count())andnext(i2.base(), i2.count())refer to the same (possibly past-the-end) element.
namespace std { template<input_or_output_iterator I> class counted_iterator { public: using iterator_type = I;constexpr counted_iterator() = default;constexpr counted_iterator(I x, iter_difference_t<I> n);template<class I2> requires convertible_to<const I2&, I> constexpr counted_iterator(const counted_iterator<I2>& x);template<class I2> requires assignable_from<I&, const I2&> constexpr counted_iterator& operator=(const counted_iterator<I2>& x);constexpr I base() const & requires copy_constructible<I>;constexpr I base() &&;constexpr iter_difference_t<I> count() const noexcept;constexpr decltype(auto) operator*();constexpr decltype(auto) operator*() const requires dereferenceable<const I>;constexpr counted_iterator& operator++();decltype(auto) operator++(int);constexpr counted_iterator operator++(int) requires forward_iterator<I>;constexpr counted_iterator& operator--() requires bidirectional_iterator<I>;constexpr counted_iterator operator--(int) requires bidirectional_iterator<I>;constexpr counted_iterator operator+(iter_difference_t<I> n) const requires random_access_iterator<I>;friend constexpr counted_iterator operator+( iter_difference_t<I> n, const counted_iterator& x) requires random_access_iterator<I>;constexpr counted_iterator& operator+=(iter_difference_t<I> n) requires random_access_iterator<I>;constexpr counted_iterator operator-(iter_difference_t<I> n) const requires random_access_iterator<I>;template<common_with<I> I2> friend constexpr iter_difference_t<I2> operator-( const counted_iterator& x, const counted_iterator<I2>& y);friend constexpr iter_difference_t<I> operator-( const counted_iterator& x, default_sentinel_t);friend constexpr iter_difference_t<I> operator-( default_sentinel_t, const counted_iterator& y);constexpr counted_iterator& operator-=(iter_difference_t<I> n) requires random_access_iterator<I>;constexpr decltype(auto) operator[](iter_difference_t<I> n) const requires random_access_iterator<I>;template<common_with<I> I2> friend constexpr bool operator==( const counted_iterator& x, const counted_iterator<I2>& y);friend constexpr bool operator==( const counted_iterator& x, default_sentinel_t);template<common_with<I> I2> friend constexpr strong_ordering operator<=>( const counted_iterator& x, const counted_iterator<I2>& y);friend constexpr iter_rvalue_reference_t<I> iter_move(const counted_iterator& i) noexcept(noexcept(ranges::iter_move(i.current))) requires input_iterator<I>;template<indirectly_swappable<I> I2> friend constexpr void iter_swap(const counted_iterator& x, const counted_iterator<I2>& y) noexcept(noexcept(ranges::iter_swap(x.current, y.current)));private: I current = I(); iter_difference_t<I> length = 0; };template<class I> struct incrementable_traits<counted_iterator<I>> { using difference_type = iter_difference_t<I>;};template<input_iterator I> struct iterator_traits<counted_iterator<I>> : iterator_traits<I> { using pointer = void;};}
23.5.6.2 Constructors and conversions [counted.iter.const]
constexpr counted_iterator(I i, iter_difference_t<I> n);
Effects: Initializes current with std::move(i) andlength with n.
template<class I2> requires [convertible_to](concept.convertible#concept:convertible%5Fto "18.4.4 Concept convertible_to [concept.convertible]")<const I2&, I> constexpr counted_iterator(const counted_iterator<I2>& x);
Effects: Initializes current with x.current andlength with x.length.
template<class I2> requires [assignable_from](concept.assignable#concept:assignable%5Ffrom "18.4.8 Concept assignable_from [concept.assignable]")<I&, const I2&> constexpr counted_iterator& operator=(const counted_iterator<I2>& x);
Effects: Assigns x.current to current andx.length to length.
23.5.6.3 Accessors [counted.iter.access]
constexpr I base() const & requires copy_constructible<I>;
Effects: Equivalent to: return current;
Returns: std::move(current).
constexpr iter_difference_t<I> count() const noexcept;
Effects: Equivalent to: return length;
23.5.6.4 Element access [counted.iter.elem]
constexpr decltype(auto) operator*();constexpr decltype(auto) operator*() const requires [_dereferenceable_](iterator.synopsis#concept:dereferenceable "23.2 Header <iterator> synopsis [iterator.synopsis]")<const I>;
Effects: Equivalent to: return *current;
Preconditions: n < length.
Effects: Equivalent to: return current[n];
23.5.6.5 Navigation [counted.iter.nav]
constexpr counted_iterator& operator++();
Preconditions: length > 0.
Effects: Equivalent to:++current;--length;return *this;
decltype(auto) operator++(int);
Preconditions: length > 0.
Effects: Equivalent to:--length;try { return current++; } catch(...) { ++length; throw; }
Effects: Equivalent to:counted_iterator tmp = *this;++*this;return tmp;
Effects: Equivalent to:--current;++length;return *this;
Effects: Equivalent to:counted_iterator tmp = *this;--*this;return tmp;
Effects: Equivalent to: return counted_iterator(current + n, length - n);
friend constexpr counted_iterator operator+( iter_difference_t<I> n, const counted_iterator& x) requires [random_access_iterator](iterator.concept.random.access#concept:random%5Faccess%5Fiterator "23.3.4.13 Concept random_access_iterator [iterator.concept.random.access]")<I>;
Effects: Equivalent to: return x + n;
Preconditions: n <= length.
Effects: Equivalent to:current += n; length -= n;return *this;
Effects: Equivalent to: return counted_iterator(current - n, length + n);
template<[common_with](concept.common#concept:common%5Fwith "18.4.6 Concept common_with [concept.common]")<I> I2> friend constexpr iter_difference_t<I2> operator-( const counted_iterator& x, const counted_iterator<I2>& y);
Preconditions: x and y refer to elements of the same sequence ([counted.iterator]).
Effects: Equivalent to: return y.length - x.length;
friend constexpr iter_difference_t<I> operator-( const counted_iterator& x, default_sentinel_t);
Effects: Equivalent to:return -x.length;
friend constexpr iter_difference_t<I> operator-( default_sentinel_t, const counted_iterator& y);
Effects: Equivalent to: return y.length;
Preconditions: -n <= length.
Effects: Equivalent to:current -= n; length += n;return *this;
23.5.6.6 Comparisons [counted.iter.cmp]
template<[common_with](concept.common#concept:common%5Fwith "18.4.6 Concept common_with [concept.common]")<I> I2> friend constexpr bool operator==( const counted_iterator& x, const counted_iterator<I2>& y);
Preconditions: x and y refer to elements of the same sequence ([counted.iterator]).
Effects: Equivalent to: return x.length == y.length;
friend constexpr bool operator==( const counted_iterator& x, default_sentinel_t);
Effects: Equivalent to: return x.length == 0;
template<[common_with](concept.common#concept:common%5Fwith "18.4.6 Concept common_with [concept.common]")<I> I2> friend constexpr strong_ordering operator<=>( const counted_iterator& x, const counted_iterator<I2>& y);
Preconditions: x and y refer to elements of the same sequence ([counted.iterator]).
Effects: Equivalent to: return y.length <=> x.length;
[Note 1:
The argument order in the Effects: element is reversed because length counts down, not up.
— _end note_]
23.5.6.7 Customizations [counted.iter.cust]
friend constexpr iter_rvalue_reference_t<I> iter_move(const counted_iterator& i) noexcept(noexcept(ranges::iter_move(i.current))) requires [input_iterator](iterator.concept.input#concept:input%5Fiterator "23.3.4.9 Concept input_iterator [iterator.concept.input]")<I>;
Effects: Equivalent to: return ranges::iter_move(i.current);
template<[indirectly_swappable](alg.req.ind.swap#concept:indirectly%5Fswappable "23.3.7.4 Concept indirectly_swappable [alg.req.ind.swap]")<I> I2> friend constexpr void iter_swap(const counted_iterator& x, const counted_iterator<I2>& y) noexcept(noexcept(ranges::iter_swap(x.current, y.current)));
Effects: Equivalent to ranges::iter_swap(x.current, y.current).
23.5.7 Unreachable sentinel [unreachable.sentinel]
Class unreachable_sentinel_t can be used with any weakly_incrementable type to denote the “upper bound” of an unbounded interval.
[Example 1: char* p;char* nl = find(p, unreachable_sentinel, '\n');
Provided a newline character really exists in the buffer, the use ofunreachable_sentinel above potentially makes the call to find more efficient since the loop test against the sentinel does not require a conditional branch.
— _end example_]
namespace std { struct unreachable_sentinel_t { template<weakly_incrementable I> friend constexpr bool operator==(unreachable_sentinel_t, const I&) noexcept { return false; } };}