1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001-2018 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
28 * Hewlett-Packard Company
30 * Permission to use, copy, modify, distribute and sell this software
31 * and its documentation for any purpose is hereby granted without fee,
32 * provided that the above copyright notice appear in all copies and
33 * that both that copyright notice and this permission notice appear
34 * in supporting documentation. Hewlett-Packard Company makes no
35 * representations about the suitability of this software for any
36 * purpose. It is provided "as is" without express or implied warranty.
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
42 * Permission to use, copy, modify, distribute and sell this software
43 * and its documentation for any purpose is hereby granted without fee,
44 * provided that the above copyright notice appear in all copies and
45 * that both that copyright notice and this permission notice appear
46 * in supporting documentation. Silicon Graphics makes no
47 * representations about the suitability of this software for any
48 * purpose. It is provided "as is" without express or implied warranty.
51 /** @file bits/stl_iterator.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{iterator}
55 * This file implements reverse_iterator, back_insert_iterator,
56 * front_insert_iterator, insert_iterator, __normal_iterator, and their
57 * supporting functions and overloaded operators.
60 #ifndef _STL_ITERATOR_H
61 #define _STL_ITERATOR_H 1
63 #include <bits/cpp_type_traits.h>
64 #include <ext/type_traits.h>
65 #include <bits/move.h>
66 #include <bits/ptr_traits.h>
68 #if __cplusplus > 201402L
69 # define __cpp_lib_array_constexpr 201603
72 namespace std
_GLIBCXX_VISIBILITY(default)
74 _GLIBCXX_BEGIN_NAMESPACE_VERSION
77 * @addtogroup iterators
81 // 24.4.1 Reverse iterators
83 * Bidirectional and random access iterators have corresponding reverse
84 * %iterator adaptors that iterate through the data structure in the
85 * opposite direction. They have the same signatures as the corresponding
86 * iterators. The fundamental relation between a reverse %iterator and its
87 * corresponding %iterator @c i is established by the identity:
89 * &*(reverse_iterator(i)) == &*(i - 1)
92 * <em>This mapping is dictated by the fact that while there is always a
93 * pointer past the end of an array, there might not be a valid pointer
94 * before the beginning of an array.</em> [24.4.1]/1,2
96 * Reverse iterators can be tricky and surprising at first. Their
97 * semantics make sense, however, and the trickiness is a side effect of
98 * the requirement that the iterators must be safe.
100 template<typename _Iterator
>
101 class reverse_iterator
102 : public iterator
<typename iterator_traits
<_Iterator
>::iterator_category
,
103 typename iterator_traits
<_Iterator
>::value_type
,
104 typename iterator_traits
<_Iterator
>::difference_type
,
105 typename iterator_traits
<_Iterator
>::pointer
,
106 typename iterator_traits
<_Iterator
>::reference
>
111 typedef iterator_traits
<_Iterator
> __traits_type
;
114 typedef _Iterator iterator_type
;
115 typedef typename
__traits_type::difference_type difference_type
;
116 typedef typename
__traits_type::pointer pointer
;
117 typedef typename
__traits_type::reference reference
;
120 * The default constructor value-initializes member @p current.
121 * If it is a pointer, that means it is zero-initialized.
123 // _GLIBCXX_RESOLVE_LIB_DEFECTS
124 // 235 No specification of default ctor for reverse_iterator
126 reverse_iterator() : current() { }
129 * This %iterator will move in the opposite direction that @p x does.
131 explicit _GLIBCXX17_CONSTEXPR
132 reverse_iterator(iterator_type __x
) : current(__x
) { }
135 * The copy constructor is normal.
138 reverse_iterator(const reverse_iterator
& __x
)
139 : current(__x
.current
) { }
141 #if __cplusplus >= 201103L
142 reverse_iterator
& operator=(const reverse_iterator
&) = default;
146 * A %reverse_iterator across other types can be copied if the
147 * underlying %iterator can be converted to the type of @c current.
149 template<typename _Iter
>
151 reverse_iterator(const reverse_iterator
<_Iter
>& __x
)
152 : current(__x
.base()) { }
155 * @return @c current, the %iterator used for underlying work.
157 _GLIBCXX17_CONSTEXPR iterator_type
162 * @return A reference to the value at @c --current
164 * This requires that @c --current is dereferenceable.
166 * @warning This implementation requires that for an iterator of the
167 * underlying iterator type, @c x, a reference obtained by
168 * @c *x remains valid after @c x has been modified or
169 * destroyed. This is a bug: http://gcc.gnu.org/PR51823
171 _GLIBCXX17_CONSTEXPR reference
174 _Iterator __tmp
= current
;
179 * @return A pointer to the value at @c --current
181 * This requires that @c --current is dereferenceable.
183 _GLIBCXX17_CONSTEXPR pointer
185 { return &(operator*()); }
190 * Decrements the underlying iterator.
192 _GLIBCXX17_CONSTEXPR reverse_iterator
&
200 * @return The original value of @c *this
202 * Decrements the underlying iterator.
204 _GLIBCXX17_CONSTEXPR reverse_iterator
207 reverse_iterator __tmp
= *this;
215 * Increments the underlying iterator.
217 _GLIBCXX17_CONSTEXPR reverse_iterator
&
225 * @return A reverse_iterator with the previous value of @c *this
227 * Increments the underlying iterator.
229 _GLIBCXX17_CONSTEXPR reverse_iterator
232 reverse_iterator __tmp
= *this;
238 * @return A reverse_iterator that refers to @c current - @a __n
240 * The underlying iterator must be a Random Access Iterator.
242 _GLIBCXX17_CONSTEXPR reverse_iterator
243 operator+(difference_type __n
) const
244 { return reverse_iterator(current
- __n
); }
249 * Moves the underlying iterator backwards @a __n steps.
250 * The underlying iterator must be a Random Access Iterator.
252 _GLIBCXX17_CONSTEXPR reverse_iterator
&
253 operator+=(difference_type __n
)
260 * @return A reverse_iterator that refers to @c current - @a __n
262 * The underlying iterator must be a Random Access Iterator.
264 _GLIBCXX17_CONSTEXPR reverse_iterator
265 operator-(difference_type __n
) const
266 { return reverse_iterator(current
+ __n
); }
271 * Moves the underlying iterator forwards @a __n steps.
272 * The underlying iterator must be a Random Access Iterator.
274 _GLIBCXX17_CONSTEXPR reverse_iterator
&
275 operator-=(difference_type __n
)
282 * @return The value at @c current - @a __n - 1
284 * The underlying iterator must be a Random Access Iterator.
286 _GLIBCXX17_CONSTEXPR reference
287 operator[](difference_type __n
) const
288 { return *(*this + __n
); }
293 * @param __x A %reverse_iterator.
294 * @param __y A %reverse_iterator.
295 * @return A simple bool.
297 * Reverse iterators forward many operations to their underlying base()
298 * iterators. Others are implemented in terms of one another.
301 template<typename _Iterator
>
302 inline _GLIBCXX17_CONSTEXPR
bool
303 operator==(const reverse_iterator
<_Iterator
>& __x
,
304 const reverse_iterator
<_Iterator
>& __y
)
305 { return __x
.base() == __y
.base(); }
307 template<typename _Iterator
>
308 inline _GLIBCXX17_CONSTEXPR
bool
309 operator<(const reverse_iterator
<_Iterator
>& __x
,
310 const reverse_iterator
<_Iterator
>& __y
)
311 { return __y
.base() < __x
.base(); }
313 template<typename _Iterator
>
314 inline _GLIBCXX17_CONSTEXPR
bool
315 operator!=(const reverse_iterator
<_Iterator
>& __x
,
316 const reverse_iterator
<_Iterator
>& __y
)
317 { return !(__x
== __y
); }
319 template<typename _Iterator
>
320 inline _GLIBCXX17_CONSTEXPR
bool
321 operator>(const reverse_iterator
<_Iterator
>& __x
,
322 const reverse_iterator
<_Iterator
>& __y
)
323 { return __y
< __x
; }
325 template<typename _Iterator
>
326 inline _GLIBCXX17_CONSTEXPR
bool
327 operator<=(const reverse_iterator
<_Iterator
>& __x
,
328 const reverse_iterator
<_Iterator
>& __y
)
329 { return !(__y
< __x
); }
331 template<typename _Iterator
>
332 inline _GLIBCXX17_CONSTEXPR
bool
333 operator>=(const reverse_iterator
<_Iterator
>& __x
,
334 const reverse_iterator
<_Iterator
>& __y
)
335 { return !(__x
< __y
); }
337 // _GLIBCXX_RESOLVE_LIB_DEFECTS
338 // DR 280. Comparison of reverse_iterator to const reverse_iterator.
339 template<typename _IteratorL
, typename _IteratorR
>
340 inline _GLIBCXX17_CONSTEXPR
bool
341 operator==(const reverse_iterator
<_IteratorL
>& __x
,
342 const reverse_iterator
<_IteratorR
>& __y
)
343 { return __x
.base() == __y
.base(); }
345 template<typename _IteratorL
, typename _IteratorR
>
346 inline _GLIBCXX17_CONSTEXPR
bool
347 operator<(const reverse_iterator
<_IteratorL
>& __x
,
348 const reverse_iterator
<_IteratorR
>& __y
)
349 { return __y
.base() < __x
.base(); }
351 template<typename _IteratorL
, typename _IteratorR
>
352 inline _GLIBCXX17_CONSTEXPR
bool
353 operator!=(const reverse_iterator
<_IteratorL
>& __x
,
354 const reverse_iterator
<_IteratorR
>& __y
)
355 { return !(__x
== __y
); }
357 template<typename _IteratorL
, typename _IteratorR
>
358 inline _GLIBCXX17_CONSTEXPR
bool
359 operator>(const reverse_iterator
<_IteratorL
>& __x
,
360 const reverse_iterator
<_IteratorR
>& __y
)
361 { return __y
< __x
; }
363 template<typename _IteratorL
, typename _IteratorR
>
364 inline _GLIBCXX17_CONSTEXPR
bool
365 operator<=(const reverse_iterator
<_IteratorL
>& __x
,
366 const reverse_iterator
<_IteratorR
>& __y
)
367 { return !(__y
< __x
); }
369 template<typename _IteratorL
, typename _IteratorR
>
370 inline _GLIBCXX17_CONSTEXPR
bool
371 operator>=(const reverse_iterator
<_IteratorL
>& __x
,
372 const reverse_iterator
<_IteratorR
>& __y
)
373 { return !(__x
< __y
); }
376 #if __cplusplus < 201103L
377 template<typename _Iterator
>
378 inline typename reverse_iterator
<_Iterator
>::difference_type
379 operator-(const reverse_iterator
<_Iterator
>& __x
,
380 const reverse_iterator
<_Iterator
>& __y
)
381 { return __y
.base() - __x
.base(); }
383 template<typename _IteratorL
, typename _IteratorR
>
384 inline typename reverse_iterator
<_IteratorL
>::difference_type
385 operator-(const reverse_iterator
<_IteratorL
>& __x
,
386 const reverse_iterator
<_IteratorR
>& __y
)
387 { return __y
.base() - __x
.base(); }
389 // _GLIBCXX_RESOLVE_LIB_DEFECTS
390 // DR 685. reverse_iterator/move_iterator difference has invalid signatures
391 template<typename _IteratorL
, typename _IteratorR
>
392 inline _GLIBCXX17_CONSTEXPR
auto
393 operator-(const reverse_iterator
<_IteratorL
>& __x
,
394 const reverse_iterator
<_IteratorR
>& __y
)
395 -> decltype(__y
.base() - __x
.base())
396 { return __y
.base() - __x
.base(); }
399 template<typename _Iterator
>
400 inline _GLIBCXX17_CONSTEXPR reverse_iterator
<_Iterator
>
401 operator+(typename reverse_iterator
<_Iterator
>::difference_type __n
,
402 const reverse_iterator
<_Iterator
>& __x
)
403 { return reverse_iterator
<_Iterator
>(__x
.base() - __n
); }
405 #if __cplusplus >= 201103L
406 // Same as C++14 make_reverse_iterator but used in C++03 mode too.
407 template<typename _Iterator
>
408 inline _GLIBCXX17_CONSTEXPR reverse_iterator
<_Iterator
>
409 __make_reverse_iterator(_Iterator __i
)
410 { return reverse_iterator
<_Iterator
>(__i
); }
412 # if __cplusplus > 201103L
413 # define __cpp_lib_make_reverse_iterator 201402
415 // _GLIBCXX_RESOLVE_LIB_DEFECTS
416 // DR 2285. make_reverse_iterator
417 /// Generator function for reverse_iterator.
418 template<typename _Iterator
>
419 inline _GLIBCXX17_CONSTEXPR reverse_iterator
<_Iterator
>
420 make_reverse_iterator(_Iterator __i
)
421 { return reverse_iterator
<_Iterator
>(__i
); }
425 #if __cplusplus >= 201103L
426 template<typename _Iterator
>
428 __niter_base(reverse_iterator
<_Iterator
> __it
)
429 -> decltype(__make_reverse_iterator(__niter_base(__it
.base())))
430 { return __make_reverse_iterator(__niter_base(__it
.base())); }
432 template<typename _Iterator
>
433 struct __is_move_iterator
<reverse_iterator
<_Iterator
> >
434 : __is_move_iterator
<_Iterator
>
437 template<typename _Iterator
>
439 __miter_base(reverse_iterator
<_Iterator
> __it
)
440 -> decltype(__make_reverse_iterator(__miter_base(__it
.base())))
441 { return __make_reverse_iterator(__miter_base(__it
.base())); }
444 // 24.4.2.2.1 back_insert_iterator
446 * @brief Turns assignment into insertion.
448 * These are output iterators, constructed from a container-of-T.
449 * Assigning a T to the iterator appends it to the container using
452 * Tip: Using the back_inserter function to create these iterators can
455 template<typename _Container
>
456 class back_insert_iterator
457 : public iterator
<output_iterator_tag
, void, void, void, void>
460 _Container
* container
;
463 /// A nested typedef for the type of whatever container you used.
464 typedef _Container container_type
;
466 /// The only way to create this %iterator is with a container.
468 back_insert_iterator(_Container
& __x
)
469 : container(std::__addressof(__x
)) { }
472 * @param __value An instance of whatever type
473 * container_type::const_reference is; presumably a
474 * reference-to-const T for container<T>.
475 * @return This %iterator, for chained operations.
477 * This kind of %iterator doesn't really have a @a position in the
478 * container (you can think of the position as being permanently at
479 * the end, if you like). Assigning a value to the %iterator will
480 * always append the value to the end of the container.
482 #if __cplusplus < 201103L
483 back_insert_iterator
&
484 operator=(typename
_Container::const_reference __value
)
486 container
->push_back(__value
);
490 back_insert_iterator
&
491 operator=(const typename
_Container::value_type
& __value
)
493 container
->push_back(__value
);
497 back_insert_iterator
&
498 operator=(typename
_Container::value_type
&& __value
)
500 container
->push_back(std::move(__value
));
505 /// Simply returns *this.
506 back_insert_iterator
&
510 /// Simply returns *this. (This %iterator does not @a move.)
511 back_insert_iterator
&
515 /// Simply returns *this. (This %iterator does not @a move.)
522 * @param __x A container of arbitrary type.
523 * @return An instance of back_insert_iterator working on @p __x.
525 * This wrapper function helps in creating back_insert_iterator instances.
526 * Typing the name of the %iterator requires knowing the precise full
527 * type of the container, which can be tedious and impedes generic
528 * programming. Using this function lets you take advantage of automatic
529 * template parameter deduction, making the compiler match the correct
532 template<typename _Container
>
533 inline back_insert_iterator
<_Container
>
534 back_inserter(_Container
& __x
)
535 { return back_insert_iterator
<_Container
>(__x
); }
538 * @brief Turns assignment into insertion.
540 * These are output iterators, constructed from a container-of-T.
541 * Assigning a T to the iterator prepends it to the container using
544 * Tip: Using the front_inserter function to create these iterators can
547 template<typename _Container
>
548 class front_insert_iterator
549 : public iterator
<output_iterator_tag
, void, void, void, void>
552 _Container
* container
;
555 /// A nested typedef for the type of whatever container you used.
556 typedef _Container container_type
;
558 /// The only way to create this %iterator is with a container.
559 explicit front_insert_iterator(_Container
& __x
)
560 : container(std::__addressof(__x
)) { }
563 * @param __value An instance of whatever type
564 * container_type::const_reference is; presumably a
565 * reference-to-const T for container<T>.
566 * @return This %iterator, for chained operations.
568 * This kind of %iterator doesn't really have a @a position in the
569 * container (you can think of the position as being permanently at
570 * the front, if you like). Assigning a value to the %iterator will
571 * always prepend the value to the front of the container.
573 #if __cplusplus < 201103L
574 front_insert_iterator
&
575 operator=(typename
_Container::const_reference __value
)
577 container
->push_front(__value
);
581 front_insert_iterator
&
582 operator=(const typename
_Container::value_type
& __value
)
584 container
->push_front(__value
);
588 front_insert_iterator
&
589 operator=(typename
_Container::value_type
&& __value
)
591 container
->push_front(std::move(__value
));
596 /// Simply returns *this.
597 front_insert_iterator
&
601 /// Simply returns *this. (This %iterator does not @a move.)
602 front_insert_iterator
&
606 /// Simply returns *this. (This %iterator does not @a move.)
607 front_insert_iterator
613 * @param __x A container of arbitrary type.
614 * @return An instance of front_insert_iterator working on @p x.
616 * This wrapper function helps in creating front_insert_iterator instances.
617 * Typing the name of the %iterator requires knowing the precise full
618 * type of the container, which can be tedious and impedes generic
619 * programming. Using this function lets you take advantage of automatic
620 * template parameter deduction, making the compiler match the correct
623 template<typename _Container
>
624 inline front_insert_iterator
<_Container
>
625 front_inserter(_Container
& __x
)
626 { return front_insert_iterator
<_Container
>(__x
); }
629 * @brief Turns assignment into insertion.
631 * These are output iterators, constructed from a container-of-T.
632 * Assigning a T to the iterator inserts it in the container at the
633 * %iterator's position, rather than overwriting the value at that
636 * (Sequences will actually insert a @e copy of the value before the
637 * %iterator's position.)
639 * Tip: Using the inserter function to create these iterators can
642 template<typename _Container
>
643 class insert_iterator
644 : public iterator
<output_iterator_tag
, void, void, void, void>
647 _Container
* container
;
648 typename
_Container::iterator iter
;
651 /// A nested typedef for the type of whatever container you used.
652 typedef _Container container_type
;
655 * The only way to create this %iterator is with a container and an
656 * initial position (a normal %iterator into the container).
658 insert_iterator(_Container
& __x
, typename
_Container::iterator __i
)
659 : container(std::__addressof(__x
)), iter(__i
) {}
662 * @param __value An instance of whatever type
663 * container_type::const_reference is; presumably a
664 * reference-to-const T for container<T>.
665 * @return This %iterator, for chained operations.
667 * This kind of %iterator maintains its own position in the
668 * container. Assigning a value to the %iterator will insert the
669 * value into the container at the place before the %iterator.
671 * The position is maintained such that subsequent assignments will
672 * insert values immediately after one another. For example,
674 * // vector v contains A and Z
676 * insert_iterator i (v, ++v.begin());
681 * // vector v contains A, 1, 2, 3, and Z
684 #if __cplusplus < 201103L
686 operator=(typename
_Container::const_reference __value
)
688 iter
= container
->insert(iter
, __value
);
694 operator=(const typename
_Container::value_type
& __value
)
696 iter
= container
->insert(iter
, __value
);
702 operator=(typename
_Container::value_type
&& __value
)
704 iter
= container
->insert(iter
, std::move(__value
));
710 /// Simply returns *this.
715 /// Simply returns *this. (This %iterator does not @a move.)
720 /// Simply returns *this. (This %iterator does not @a move.)
727 * @param __x A container of arbitrary type.
728 * @param __i An iterator into the container.
729 * @return An instance of insert_iterator working on @p __x.
731 * This wrapper function helps in creating insert_iterator instances.
732 * Typing the name of the %iterator requires knowing the precise full
733 * type of the container, which can be tedious and impedes generic
734 * programming. Using this function lets you take advantage of automatic
735 * template parameter deduction, making the compiler match the correct
738 template<typename _Container
, typename _Iterator
>
739 inline insert_iterator
<_Container
>
740 inserter(_Container
& __x
, _Iterator __i
)
742 return insert_iterator
<_Container
>(__x
,
743 typename
_Container::iterator(__i
));
746 // @} group iterators
748 _GLIBCXX_END_NAMESPACE_VERSION
751 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
753 _GLIBCXX_BEGIN_NAMESPACE_VERSION
755 // This iterator adapter is @a normal in the sense that it does not
756 // change the semantics of any of the operators of its iterator
757 // parameter. Its primary purpose is to convert an iterator that is
758 // not a class, e.g. a pointer, into an iterator that is a class.
759 // The _Container parameter exists solely so that different containers
760 // using this template can instantiate different types, even if the
761 // _Iterator parameter is the same.
762 using std::iterator_traits
;
764 template<typename _Iterator
, typename _Container
>
765 class __normal_iterator
768 _Iterator _M_current
;
770 typedef iterator_traits
<_Iterator
> __traits_type
;
773 typedef _Iterator iterator_type
;
774 typedef typename
__traits_type::iterator_category iterator_category
;
775 typedef typename
__traits_type::value_type value_type
;
776 typedef typename
__traits_type::difference_type difference_type
;
777 typedef typename
__traits_type::reference reference
;
778 typedef typename
__traits_type::pointer pointer
;
780 _GLIBCXX_CONSTEXPR
__normal_iterator() _GLIBCXX_NOEXCEPT
781 : _M_current(_Iterator()) { }
784 __normal_iterator(const _Iterator
& __i
) _GLIBCXX_NOEXCEPT
785 : _M_current(__i
) { }
787 // Allow iterator to const_iterator conversion
788 template<typename _Iter
>
789 __normal_iterator(const __normal_iterator
<_Iter
,
790 typename __enable_if
<
791 (std::__are_same
<_Iter
, typename
_Container::pointer
>::__value
),
792 _Container
>::__type
>& __i
) _GLIBCXX_NOEXCEPT
793 : _M_current(__i
.base()) { }
795 // Forward iterator requirements
797 operator*() const _GLIBCXX_NOEXCEPT
798 { return *_M_current
; }
801 operator->() const _GLIBCXX_NOEXCEPT
802 { return _M_current
; }
805 operator++() _GLIBCXX_NOEXCEPT
812 operator++(int) _GLIBCXX_NOEXCEPT
813 { return __normal_iterator(_M_current
++); }
815 // Bidirectional iterator requirements
817 operator--() _GLIBCXX_NOEXCEPT
824 operator--(int) _GLIBCXX_NOEXCEPT
825 { return __normal_iterator(_M_current
--); }
827 // Random access iterator requirements
829 operator[](difference_type __n
) const _GLIBCXX_NOEXCEPT
830 { return _M_current
[__n
]; }
833 operator+=(difference_type __n
) _GLIBCXX_NOEXCEPT
834 { _M_current
+= __n
; return *this; }
837 operator+(difference_type __n
) const _GLIBCXX_NOEXCEPT
838 { return __normal_iterator(_M_current
+ __n
); }
841 operator-=(difference_type __n
) _GLIBCXX_NOEXCEPT
842 { _M_current
-= __n
; return *this; }
845 operator-(difference_type __n
) const _GLIBCXX_NOEXCEPT
846 { return __normal_iterator(_M_current
- __n
); }
849 base() const _GLIBCXX_NOEXCEPT
850 { return _M_current
; }
853 // Note: In what follows, the left- and right-hand-side iterators are
854 // allowed to vary in types (conceptually in cv-qualification) so that
855 // comparison between cv-qualified and non-cv-qualified iterators be
856 // valid. However, the greedy and unfriendly operators in std::rel_ops
857 // will make overload resolution ambiguous (when in scope) if we don't
858 // provide overloads whose operands are of the same type. Can someone
859 // remind me what generic programming is about? -- Gaby
861 // Forward iterator requirements
862 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
864 operator==(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
865 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
867 { return __lhs
.base() == __rhs
.base(); }
869 template<typename _Iterator
, typename _Container
>
871 operator==(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
872 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
874 { return __lhs
.base() == __rhs
.base(); }
876 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
878 operator!=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
879 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
881 { return __lhs
.base() != __rhs
.base(); }
883 template<typename _Iterator
, typename _Container
>
885 operator!=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
886 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
888 { return __lhs
.base() != __rhs
.base(); }
890 // Random access iterator requirements
891 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
893 operator<(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
894 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
896 { return __lhs
.base() < __rhs
.base(); }
898 template<typename _Iterator
, typename _Container
>
900 operator<(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
901 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
903 { return __lhs
.base() < __rhs
.base(); }
905 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
907 operator>(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
908 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
910 { return __lhs
.base() > __rhs
.base(); }
912 template<typename _Iterator
, typename _Container
>
914 operator>(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
915 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
917 { return __lhs
.base() > __rhs
.base(); }
919 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
921 operator<=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
922 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
924 { return __lhs
.base() <= __rhs
.base(); }
926 template<typename _Iterator
, typename _Container
>
928 operator<=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
929 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
931 { return __lhs
.base() <= __rhs
.base(); }
933 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
935 operator>=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
936 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
938 { return __lhs
.base() >= __rhs
.base(); }
940 template<typename _Iterator
, typename _Container
>
942 operator>=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
943 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
945 { return __lhs
.base() >= __rhs
.base(); }
947 // _GLIBCXX_RESOLVE_LIB_DEFECTS
948 // According to the resolution of DR179 not only the various comparison
949 // operators but also operator- must accept mixed iterator/const_iterator
951 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
952 #if __cplusplus >= 201103L
955 operator-(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
956 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
) noexcept
957 -> decltype(__lhs
.base() - __rhs
.base())
959 inline typename __normal_iterator
<_IteratorL
, _Container
>::difference_type
960 operator-(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
961 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
963 { return __lhs
.base() - __rhs
.base(); }
965 template<typename _Iterator
, typename _Container
>
966 inline typename __normal_iterator
<_Iterator
, _Container
>::difference_type
967 operator-(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
968 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
970 { return __lhs
.base() - __rhs
.base(); }
972 template<typename _Iterator
, typename _Container
>
973 inline __normal_iterator
<_Iterator
, _Container
>
974 operator+(typename __normal_iterator
<_Iterator
, _Container
>::difference_type
975 __n
, const __normal_iterator
<_Iterator
, _Container
>& __i
)
977 { return __normal_iterator
<_Iterator
, _Container
>(__i
.base() + __n
); }
979 _GLIBCXX_END_NAMESPACE_VERSION
982 namespace std
_GLIBCXX_VISIBILITY(default)
984 _GLIBCXX_BEGIN_NAMESPACE_VERSION
986 template<typename _Iterator
, typename _Container
>
988 __niter_base(__gnu_cxx::__normal_iterator
<_Iterator
, _Container
> __it
)
989 { return __it
.base(); }
991 #if __cplusplus >= 201103L
994 * @addtogroup iterators
998 // 24.4.3 Move iterators
1000 * Class template move_iterator is an iterator adapter with the same
1001 * behavior as the underlying iterator except that its dereference
1002 * operator implicitly converts the value returned by the underlying
1003 * iterator's dereference operator to an rvalue reference. Some
1004 * generic algorithms can be called with move iterators to replace
1005 * copying with moving.
1007 template<typename _Iterator
>
1011 _Iterator _M_current
;
1013 typedef iterator_traits
<_Iterator
> __traits_type
;
1014 typedef typename
__traits_type::reference __base_ref
;
1017 typedef _Iterator iterator_type
;
1018 typedef typename
__traits_type::iterator_category iterator_category
;
1019 typedef typename
__traits_type::value_type value_type
;
1020 typedef typename
__traits_type::difference_type difference_type
;
1022 typedef _Iterator pointer
;
1023 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1024 // 2106. move_iterator wrapping iterators returning prvalues
1025 typedef typename conditional
<is_reference
<__base_ref
>::value
,
1026 typename remove_reference
<__base_ref
>::type
&&,
1027 __base_ref
>::type reference
;
1029 _GLIBCXX17_CONSTEXPR
1033 explicit _GLIBCXX17_CONSTEXPR
1034 move_iterator(iterator_type __i
)
1035 : _M_current(__i
) { }
1037 template<typename _Iter
>
1038 _GLIBCXX17_CONSTEXPR
1039 move_iterator(const move_iterator
<_Iter
>& __i
)
1040 : _M_current(__i
.base()) { }
1042 _GLIBCXX17_CONSTEXPR iterator_type
1044 { return _M_current
; }
1046 _GLIBCXX17_CONSTEXPR reference
1048 { return static_cast<reference
>(*_M_current
); }
1050 _GLIBCXX17_CONSTEXPR pointer
1052 { return _M_current
; }
1054 _GLIBCXX17_CONSTEXPR move_iterator
&
1061 _GLIBCXX17_CONSTEXPR move_iterator
1064 move_iterator __tmp
= *this;
1069 _GLIBCXX17_CONSTEXPR move_iterator
&
1076 _GLIBCXX17_CONSTEXPR move_iterator
1079 move_iterator __tmp
= *this;
1084 _GLIBCXX17_CONSTEXPR move_iterator
1085 operator+(difference_type __n
) const
1086 { return move_iterator(_M_current
+ __n
); }
1088 _GLIBCXX17_CONSTEXPR move_iterator
&
1089 operator+=(difference_type __n
)
1095 _GLIBCXX17_CONSTEXPR move_iterator
1096 operator-(difference_type __n
) const
1097 { return move_iterator(_M_current
- __n
); }
1099 _GLIBCXX17_CONSTEXPR move_iterator
&
1100 operator-=(difference_type __n
)
1106 _GLIBCXX17_CONSTEXPR reference
1107 operator[](difference_type __n
) const
1108 { return std::move(_M_current
[__n
]); }
1111 // Note: See __normal_iterator operators note from Gaby to understand
1112 // why there are always 2 versions for most of the move_iterator
1114 template<typename _IteratorL
, typename _IteratorR
>
1115 inline _GLIBCXX17_CONSTEXPR
bool
1116 operator==(const move_iterator
<_IteratorL
>& __x
,
1117 const move_iterator
<_IteratorR
>& __y
)
1118 { return __x
.base() == __y
.base(); }
1120 template<typename _Iterator
>
1121 inline _GLIBCXX17_CONSTEXPR
bool
1122 operator==(const move_iterator
<_Iterator
>& __x
,
1123 const move_iterator
<_Iterator
>& __y
)
1124 { return __x
.base() == __y
.base(); }
1126 template<typename _IteratorL
, typename _IteratorR
>
1127 inline _GLIBCXX17_CONSTEXPR
bool
1128 operator!=(const move_iterator
<_IteratorL
>& __x
,
1129 const move_iterator
<_IteratorR
>& __y
)
1130 { return !(__x
== __y
); }
1132 template<typename _Iterator
>
1133 inline _GLIBCXX17_CONSTEXPR
bool
1134 operator!=(const move_iterator
<_Iterator
>& __x
,
1135 const move_iterator
<_Iterator
>& __y
)
1136 { return !(__x
== __y
); }
1138 template<typename _IteratorL
, typename _IteratorR
>
1139 inline _GLIBCXX17_CONSTEXPR
bool
1140 operator<(const move_iterator
<_IteratorL
>& __x
,
1141 const move_iterator
<_IteratorR
>& __y
)
1142 { return __x
.base() < __y
.base(); }
1144 template<typename _Iterator
>
1145 inline _GLIBCXX17_CONSTEXPR
bool
1146 operator<(const move_iterator
<_Iterator
>& __x
,
1147 const move_iterator
<_Iterator
>& __y
)
1148 { return __x
.base() < __y
.base(); }
1150 template<typename _IteratorL
, typename _IteratorR
>
1151 inline _GLIBCXX17_CONSTEXPR
bool
1152 operator<=(const move_iterator
<_IteratorL
>& __x
,
1153 const move_iterator
<_IteratorR
>& __y
)
1154 { return !(__y
< __x
); }
1156 template<typename _Iterator
>
1157 inline _GLIBCXX17_CONSTEXPR
bool
1158 operator<=(const move_iterator
<_Iterator
>& __x
,
1159 const move_iterator
<_Iterator
>& __y
)
1160 { return !(__y
< __x
); }
1162 template<typename _IteratorL
, typename _IteratorR
>
1163 inline _GLIBCXX17_CONSTEXPR
bool
1164 operator>(const move_iterator
<_IteratorL
>& __x
,
1165 const move_iterator
<_IteratorR
>& __y
)
1166 { return __y
< __x
; }
1168 template<typename _Iterator
>
1169 inline _GLIBCXX17_CONSTEXPR
bool
1170 operator>(const move_iterator
<_Iterator
>& __x
,
1171 const move_iterator
<_Iterator
>& __y
)
1172 { return __y
< __x
; }
1174 template<typename _IteratorL
, typename _IteratorR
>
1175 inline _GLIBCXX17_CONSTEXPR
bool
1176 operator>=(const move_iterator
<_IteratorL
>& __x
,
1177 const move_iterator
<_IteratorR
>& __y
)
1178 { return !(__x
< __y
); }
1180 template<typename _Iterator
>
1181 inline _GLIBCXX17_CONSTEXPR
bool
1182 operator>=(const move_iterator
<_Iterator
>& __x
,
1183 const move_iterator
<_Iterator
>& __y
)
1184 { return !(__x
< __y
); }
1187 template<typename _IteratorL
, typename _IteratorR
>
1188 inline _GLIBCXX17_CONSTEXPR
auto
1189 operator-(const move_iterator
<_IteratorL
>& __x
,
1190 const move_iterator
<_IteratorR
>& __y
)
1191 -> decltype(__x
.base() - __y
.base())
1192 { return __x
.base() - __y
.base(); }
1194 template<typename _Iterator
>
1195 inline _GLIBCXX17_CONSTEXPR move_iterator
<_Iterator
>
1196 operator+(typename move_iterator
<_Iterator
>::difference_type __n
,
1197 const move_iterator
<_Iterator
>& __x
)
1198 { return __x
+ __n
; }
1200 template<typename _Iterator
>
1201 inline _GLIBCXX17_CONSTEXPR move_iterator
<_Iterator
>
1202 make_move_iterator(_Iterator __i
)
1203 { return move_iterator
<_Iterator
>(__i
); }
1205 template<typename _Iterator
, typename _ReturnType
1206 = typename conditional
<__move_if_noexcept_cond
1207 <typename iterator_traits
<_Iterator
>::value_type
>::value
,
1208 _Iterator
, move_iterator
<_Iterator
>>::type
>
1209 inline _GLIBCXX17_CONSTEXPR _ReturnType
1210 __make_move_if_noexcept_iterator(_Iterator __i
)
1211 { return _ReturnType(__i
); }
1213 // Overload for pointers that matches std::move_if_noexcept more closely,
1214 // returning a constant iterator when we don't want to move.
1215 template<typename _Tp
, typename _ReturnType
1216 = typename conditional
<__move_if_noexcept_cond
<_Tp
>::value
,
1217 const _Tp
*, move_iterator
<_Tp
*>>::type
>
1218 inline _GLIBCXX17_CONSTEXPR _ReturnType
1219 __make_move_if_noexcept_iterator(_Tp
* __i
)
1220 { return _ReturnType(__i
); }
1222 // @} group iterators
1224 template<typename _Iterator
>
1226 __niter_base(move_iterator
<_Iterator
> __it
)
1227 -> decltype(make_move_iterator(__niter_base(__it
.base())))
1228 { return make_move_iterator(__niter_base(__it
.base())); }
1230 template<typename _Iterator
>
1231 struct __is_move_iterator
<move_iterator
<_Iterator
> >
1233 enum { __value
= 1 };
1234 typedef __true_type __type
;
1237 template<typename _Iterator
>
1239 __miter_base(move_iterator
<_Iterator
> __it
)
1240 -> decltype(__miter_base(__it
.base()))
1241 { return __miter_base(__it
.base()); }
1243 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter)
1244 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \
1245 std::__make_move_if_noexcept_iterator(_Iter)
1247 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
1248 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter)
1251 #if __cpp_deduction_guides >= 201606
1252 // These helper traits are used for deduction guides
1253 // of associative containers.
1254 template<typename _InputIterator
>
1255 using __iter_key_t
= remove_const_t
<
1256 typename iterator_traits
<_InputIterator
>::value_type::first_type
>;
1258 template<typename _InputIterator
>
1259 using __iter_val_t
=
1260 typename iterator_traits
<_InputIterator
>::value_type::second_type
;
1262 template<typename _T1
, typename _T2
>
1265 template<typename _InputIterator
>
1266 using __iter_to_alloc_t
=
1267 pair
<add_const_t
<__iter_key_t
<_InputIterator
>>,
1268 __iter_val_t
<_InputIterator
>>;
1272 _GLIBCXX_END_NAMESPACE_VERSION
1275 #ifdef _GLIBCXX_DEBUG
1276 # include <debug/stl_iterator.h>