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
125 // 1012. reverse_iterator default ctor should value initialize
127 reverse_iterator() : current() { }
130 * This %iterator will move in the opposite direction that @p x does.
132 explicit _GLIBCXX17_CONSTEXPR
133 reverse_iterator(iterator_type __x
) : current(__x
) { }
136 * The copy constructor is normal.
139 reverse_iterator(const reverse_iterator
& __x
)
140 : current(__x
.current
) { }
142 #if __cplusplus >= 201103L
143 reverse_iterator
& operator=(const reverse_iterator
&) = default;
147 * A %reverse_iterator across other types can be copied if the
148 * underlying %iterator can be converted to the type of @c current.
150 template<typename _Iter
>
152 reverse_iterator(const reverse_iterator
<_Iter
>& __x
)
153 : current(__x
.base()) { }
156 * @return @c current, the %iterator used for underlying work.
158 _GLIBCXX17_CONSTEXPR iterator_type
163 * @return A reference to the value at @c --current
165 * This requires that @c --current is dereferenceable.
167 * @warning This implementation requires that for an iterator of the
168 * underlying iterator type, @c x, a reference obtained by
169 * @c *x remains valid after @c x has been modified or
170 * destroyed. This is a bug: http://gcc.gnu.org/PR51823
172 _GLIBCXX17_CONSTEXPR reference
175 _Iterator __tmp
= current
;
180 * @return A pointer to the value at @c --current
182 * This requires that @c --current is dereferenceable.
184 _GLIBCXX17_CONSTEXPR pointer
187 // _GLIBCXX_RESOLVE_LIB_DEFECTS
188 // 1052. operator-> should also support smart pointers
189 _Iterator __tmp
= current
;
191 return _S_to_pointer(__tmp
);
197 * Decrements the underlying iterator.
199 _GLIBCXX17_CONSTEXPR reverse_iterator
&
207 * @return The original value of @c *this
209 * Decrements the underlying iterator.
211 _GLIBCXX17_CONSTEXPR reverse_iterator
214 reverse_iterator __tmp
= *this;
222 * Increments the underlying iterator.
224 _GLIBCXX17_CONSTEXPR reverse_iterator
&
232 * @return A reverse_iterator with the previous value of @c *this
234 * Increments the underlying iterator.
236 _GLIBCXX17_CONSTEXPR reverse_iterator
239 reverse_iterator __tmp
= *this;
245 * @return A reverse_iterator that refers to @c current - @a __n
247 * The underlying iterator must be a Random Access Iterator.
249 _GLIBCXX17_CONSTEXPR reverse_iterator
250 operator+(difference_type __n
) const
251 { return reverse_iterator(current
- __n
); }
256 * Moves the underlying iterator backwards @a __n steps.
257 * The underlying iterator must be a Random Access Iterator.
259 _GLIBCXX17_CONSTEXPR reverse_iterator
&
260 operator+=(difference_type __n
)
267 * @return A reverse_iterator that refers to @c current - @a __n
269 * The underlying iterator must be a Random Access Iterator.
271 _GLIBCXX17_CONSTEXPR reverse_iterator
272 operator-(difference_type __n
) const
273 { return reverse_iterator(current
+ __n
); }
278 * Moves the underlying iterator forwards @a __n steps.
279 * The underlying iterator must be a Random Access Iterator.
281 _GLIBCXX17_CONSTEXPR reverse_iterator
&
282 operator-=(difference_type __n
)
289 * @return The value at @c current - @a __n - 1
291 * The underlying iterator must be a Random Access Iterator.
293 _GLIBCXX17_CONSTEXPR reference
294 operator[](difference_type __n
) const
295 { return *(*this + __n
); }
298 template<typename _Tp
>
299 static _GLIBCXX17_CONSTEXPR _Tp
*
300 _S_to_pointer(_Tp
* __p
)
303 template<typename _Tp
>
304 static _GLIBCXX17_CONSTEXPR pointer
305 _S_to_pointer(_Tp __t
)
306 { return __t
.operator->(); }
311 * @param __x A %reverse_iterator.
312 * @param __y A %reverse_iterator.
313 * @return A simple bool.
315 * Reverse iterators forward many operations to their underlying base()
316 * iterators. Others are implemented in terms of one another.
319 template<typename _Iterator
>
320 inline _GLIBCXX17_CONSTEXPR
bool
321 operator==(const reverse_iterator
<_Iterator
>& __x
,
322 const reverse_iterator
<_Iterator
>& __y
)
323 { return __x
.base() == __y
.base(); }
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
.base() < __x
.base(); }
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 template<typename _Iterator
>
338 inline _GLIBCXX17_CONSTEXPR
bool
339 operator>(const reverse_iterator
<_Iterator
>& __x
,
340 const reverse_iterator
<_Iterator
>& __y
)
341 { return __y
< __x
; }
343 template<typename _Iterator
>
344 inline _GLIBCXX17_CONSTEXPR
bool
345 operator<=(const reverse_iterator
<_Iterator
>& __x
,
346 const reverse_iterator
<_Iterator
>& __y
)
347 { return !(__y
< __x
); }
349 template<typename _Iterator
>
350 inline _GLIBCXX17_CONSTEXPR
bool
351 operator>=(const reverse_iterator
<_Iterator
>& __x
,
352 const reverse_iterator
<_Iterator
>& __y
)
353 { return !(__x
< __y
); }
355 // _GLIBCXX_RESOLVE_LIB_DEFECTS
356 // DR 280. Comparison of reverse_iterator to const reverse_iterator.
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 __x
.base() == __y
.base(); }
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
.base() < __x
.base(); }
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
); }
375 template<typename _IteratorL
, typename _IteratorR
>
376 inline _GLIBCXX17_CONSTEXPR
bool
377 operator>(const reverse_iterator
<_IteratorL
>& __x
,
378 const reverse_iterator
<_IteratorR
>& __y
)
379 { return __y
< __x
; }
381 template<typename _IteratorL
, typename _IteratorR
>
382 inline _GLIBCXX17_CONSTEXPR
bool
383 operator<=(const reverse_iterator
<_IteratorL
>& __x
,
384 const reverse_iterator
<_IteratorR
>& __y
)
385 { return !(__y
< __x
); }
387 template<typename _IteratorL
, typename _IteratorR
>
388 inline _GLIBCXX17_CONSTEXPR
bool
389 operator>=(const reverse_iterator
<_IteratorL
>& __x
,
390 const reverse_iterator
<_IteratorR
>& __y
)
391 { return !(__x
< __y
); }
394 #if __cplusplus < 201103L
395 template<typename _Iterator
>
396 inline typename reverse_iterator
<_Iterator
>::difference_type
397 operator-(const reverse_iterator
<_Iterator
>& __x
,
398 const reverse_iterator
<_Iterator
>& __y
)
399 { return __y
.base() - __x
.base(); }
401 template<typename _IteratorL
, typename _IteratorR
>
402 inline typename reverse_iterator
<_IteratorL
>::difference_type
403 operator-(const reverse_iterator
<_IteratorL
>& __x
,
404 const reverse_iterator
<_IteratorR
>& __y
)
405 { return __y
.base() - __x
.base(); }
407 // _GLIBCXX_RESOLVE_LIB_DEFECTS
408 // DR 685. reverse_iterator/move_iterator difference has invalid signatures
409 template<typename _IteratorL
, typename _IteratorR
>
410 inline _GLIBCXX17_CONSTEXPR
auto
411 operator-(const reverse_iterator
<_IteratorL
>& __x
,
412 const reverse_iterator
<_IteratorR
>& __y
)
413 -> decltype(__y
.base() - __x
.base())
414 { return __y
.base() - __x
.base(); }
417 template<typename _Iterator
>
418 inline _GLIBCXX17_CONSTEXPR reverse_iterator
<_Iterator
>
419 operator+(typename reverse_iterator
<_Iterator
>::difference_type __n
,
420 const reverse_iterator
<_Iterator
>& __x
)
421 { return reverse_iterator
<_Iterator
>(__x
.base() - __n
); }
423 #if __cplusplus >= 201103L
424 // Same as C++14 make_reverse_iterator but used in C++03 mode too.
425 template<typename _Iterator
>
426 inline _GLIBCXX17_CONSTEXPR reverse_iterator
<_Iterator
>
427 __make_reverse_iterator(_Iterator __i
)
428 { return reverse_iterator
<_Iterator
>(__i
); }
430 # if __cplusplus > 201103L
431 # define __cpp_lib_make_reverse_iterator 201402
433 // _GLIBCXX_RESOLVE_LIB_DEFECTS
434 // DR 2285. make_reverse_iterator
435 /// Generator function for reverse_iterator.
436 template<typename _Iterator
>
437 inline _GLIBCXX17_CONSTEXPR reverse_iterator
<_Iterator
>
438 make_reverse_iterator(_Iterator __i
)
439 { return reverse_iterator
<_Iterator
>(__i
); }
443 #if __cplusplus >= 201103L
444 template<typename _Iterator
>
446 __niter_base(reverse_iterator
<_Iterator
> __it
)
447 -> decltype(__make_reverse_iterator(__niter_base(__it
.base())))
448 { return __make_reverse_iterator(__niter_base(__it
.base())); }
450 template<typename _Iterator
>
451 struct __is_move_iterator
<reverse_iterator
<_Iterator
> >
452 : __is_move_iterator
<_Iterator
>
455 template<typename _Iterator
>
457 __miter_base(reverse_iterator
<_Iterator
> __it
)
458 -> decltype(__make_reverse_iterator(__miter_base(__it
.base())))
459 { return __make_reverse_iterator(__miter_base(__it
.base())); }
462 // 24.4.2.2.1 back_insert_iterator
464 * @brief Turns assignment into insertion.
466 * These are output iterators, constructed from a container-of-T.
467 * Assigning a T to the iterator appends it to the container using
470 * Tip: Using the back_inserter function to create these iterators can
473 template<typename _Container
>
474 class back_insert_iterator
475 : public iterator
<output_iterator_tag
, void, void, void, void>
478 _Container
* container
;
481 /// A nested typedef for the type of whatever container you used.
482 typedef _Container container_type
;
484 /// The only way to create this %iterator is with a container.
486 back_insert_iterator(_Container
& __x
)
487 : container(std::__addressof(__x
)) { }
490 * @param __value An instance of whatever type
491 * container_type::const_reference is; presumably a
492 * reference-to-const T for container<T>.
493 * @return This %iterator, for chained operations.
495 * This kind of %iterator doesn't really have a @a position in the
496 * container (you can think of the position as being permanently at
497 * the end, if you like). Assigning a value to the %iterator will
498 * always append the value to the end of the container.
500 #if __cplusplus < 201103L
501 back_insert_iterator
&
502 operator=(typename
_Container::const_reference __value
)
504 container
->push_back(__value
);
508 back_insert_iterator
&
509 operator=(const typename
_Container::value_type
& __value
)
511 container
->push_back(__value
);
515 back_insert_iterator
&
516 operator=(typename
_Container::value_type
&& __value
)
518 container
->push_back(std::move(__value
));
523 /// Simply returns *this.
524 back_insert_iterator
&
528 /// Simply returns *this. (This %iterator does not @a move.)
529 back_insert_iterator
&
533 /// Simply returns *this. (This %iterator does not @a move.)
540 * @param __x A container of arbitrary type.
541 * @return An instance of back_insert_iterator working on @p __x.
543 * This wrapper function helps in creating back_insert_iterator instances.
544 * Typing the name of the %iterator requires knowing the precise full
545 * type of the container, which can be tedious and impedes generic
546 * programming. Using this function lets you take advantage of automatic
547 * template parameter deduction, making the compiler match the correct
550 template<typename _Container
>
551 inline back_insert_iterator
<_Container
>
552 back_inserter(_Container
& __x
)
553 { return back_insert_iterator
<_Container
>(__x
); }
556 * @brief Turns assignment into insertion.
558 * These are output iterators, constructed from a container-of-T.
559 * Assigning a T to the iterator prepends it to the container using
562 * Tip: Using the front_inserter function to create these iterators can
565 template<typename _Container
>
566 class front_insert_iterator
567 : public iterator
<output_iterator_tag
, void, void, void, void>
570 _Container
* container
;
573 /// A nested typedef for the type of whatever container you used.
574 typedef _Container container_type
;
576 /// The only way to create this %iterator is with a container.
577 explicit front_insert_iterator(_Container
& __x
)
578 : container(std::__addressof(__x
)) { }
581 * @param __value An instance of whatever type
582 * container_type::const_reference is; presumably a
583 * reference-to-const T for container<T>.
584 * @return This %iterator, for chained operations.
586 * This kind of %iterator doesn't really have a @a position in the
587 * container (you can think of the position as being permanently at
588 * the front, if you like). Assigning a value to the %iterator will
589 * always prepend the value to the front of the container.
591 #if __cplusplus < 201103L
592 front_insert_iterator
&
593 operator=(typename
_Container::const_reference __value
)
595 container
->push_front(__value
);
599 front_insert_iterator
&
600 operator=(const typename
_Container::value_type
& __value
)
602 container
->push_front(__value
);
606 front_insert_iterator
&
607 operator=(typename
_Container::value_type
&& __value
)
609 container
->push_front(std::move(__value
));
614 /// Simply returns *this.
615 front_insert_iterator
&
619 /// Simply returns *this. (This %iterator does not @a move.)
620 front_insert_iterator
&
624 /// Simply returns *this. (This %iterator does not @a move.)
625 front_insert_iterator
631 * @param __x A container of arbitrary type.
632 * @return An instance of front_insert_iterator working on @p x.
634 * This wrapper function helps in creating front_insert_iterator instances.
635 * Typing the name of the %iterator requires knowing the precise full
636 * type of the container, which can be tedious and impedes generic
637 * programming. Using this function lets you take advantage of automatic
638 * template parameter deduction, making the compiler match the correct
641 template<typename _Container
>
642 inline front_insert_iterator
<_Container
>
643 front_inserter(_Container
& __x
)
644 { return front_insert_iterator
<_Container
>(__x
); }
647 * @brief Turns assignment into insertion.
649 * These are output iterators, constructed from a container-of-T.
650 * Assigning a T to the iterator inserts it in the container at the
651 * %iterator's position, rather than overwriting the value at that
654 * (Sequences will actually insert a @e copy of the value before the
655 * %iterator's position.)
657 * Tip: Using the inserter function to create these iterators can
660 template<typename _Container
>
661 class insert_iterator
662 : public iterator
<output_iterator_tag
, void, void, void, void>
665 _Container
* container
;
666 typename
_Container::iterator iter
;
669 /// A nested typedef for the type of whatever container you used.
670 typedef _Container container_type
;
673 * The only way to create this %iterator is with a container and an
674 * initial position (a normal %iterator into the container).
676 insert_iterator(_Container
& __x
, typename
_Container::iterator __i
)
677 : container(std::__addressof(__x
)), iter(__i
) {}
680 * @param __value An instance of whatever type
681 * container_type::const_reference is; presumably a
682 * reference-to-const T for container<T>.
683 * @return This %iterator, for chained operations.
685 * This kind of %iterator maintains its own position in the
686 * container. Assigning a value to the %iterator will insert the
687 * value into the container at the place before the %iterator.
689 * The position is maintained such that subsequent assignments will
690 * insert values immediately after one another. For example,
692 * // vector v contains A and Z
694 * insert_iterator i (v, ++v.begin());
699 * // vector v contains A, 1, 2, 3, and Z
702 #if __cplusplus < 201103L
704 operator=(typename
_Container::const_reference __value
)
706 iter
= container
->insert(iter
, __value
);
712 operator=(const typename
_Container::value_type
& __value
)
714 iter
= container
->insert(iter
, __value
);
720 operator=(typename
_Container::value_type
&& __value
)
722 iter
= container
->insert(iter
, std::move(__value
));
728 /// Simply returns *this.
733 /// Simply returns *this. (This %iterator does not @a move.)
738 /// Simply returns *this. (This %iterator does not @a move.)
745 * @param __x A container of arbitrary type.
746 * @param __i An iterator into the container.
747 * @return An instance of insert_iterator working on @p __x.
749 * This wrapper function helps in creating insert_iterator instances.
750 * Typing the name of the %iterator requires knowing the precise full
751 * type of the container, which can be tedious and impedes generic
752 * programming. Using this function lets you take advantage of automatic
753 * template parameter deduction, making the compiler match the correct
756 template<typename _Container
, typename _Iterator
>
757 inline insert_iterator
<_Container
>
758 inserter(_Container
& __x
, _Iterator __i
)
760 return insert_iterator
<_Container
>(__x
,
761 typename
_Container::iterator(__i
));
764 // @} group iterators
766 _GLIBCXX_END_NAMESPACE_VERSION
769 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
771 _GLIBCXX_BEGIN_NAMESPACE_VERSION
773 // This iterator adapter is @a normal in the sense that it does not
774 // change the semantics of any of the operators of its iterator
775 // parameter. Its primary purpose is to convert an iterator that is
776 // not a class, e.g. a pointer, into an iterator that is a class.
777 // The _Container parameter exists solely so that different containers
778 // using this template can instantiate different types, even if the
779 // _Iterator parameter is the same.
780 using std::iterator_traits
;
782 template<typename _Iterator
, typename _Container
>
783 class __normal_iterator
786 _Iterator _M_current
;
788 typedef iterator_traits
<_Iterator
> __traits_type
;
791 typedef _Iterator iterator_type
;
792 typedef typename
__traits_type::iterator_category iterator_category
;
793 typedef typename
__traits_type::value_type value_type
;
794 typedef typename
__traits_type::difference_type difference_type
;
795 typedef typename
__traits_type::reference reference
;
796 typedef typename
__traits_type::pointer pointer
;
798 _GLIBCXX_CONSTEXPR
__normal_iterator() _GLIBCXX_NOEXCEPT
799 : _M_current(_Iterator()) { }
802 __normal_iterator(const _Iterator
& __i
) _GLIBCXX_NOEXCEPT
803 : _M_current(__i
) { }
805 // Allow iterator to const_iterator conversion
806 template<typename _Iter
>
807 __normal_iterator(const __normal_iterator
<_Iter
,
808 typename __enable_if
<
809 (std::__are_same
<_Iter
, typename
_Container::pointer
>::__value
),
810 _Container
>::__type
>& __i
) _GLIBCXX_NOEXCEPT
811 : _M_current(__i
.base()) { }
813 // Forward iterator requirements
815 operator*() const _GLIBCXX_NOEXCEPT
816 { return *_M_current
; }
819 operator->() const _GLIBCXX_NOEXCEPT
820 { return _M_current
; }
823 operator++() _GLIBCXX_NOEXCEPT
830 operator++(int) _GLIBCXX_NOEXCEPT
831 { return __normal_iterator(_M_current
++); }
833 // Bidirectional iterator requirements
835 operator--() _GLIBCXX_NOEXCEPT
842 operator--(int) _GLIBCXX_NOEXCEPT
843 { return __normal_iterator(_M_current
--); }
845 // Random access iterator requirements
847 operator[](difference_type __n
) const _GLIBCXX_NOEXCEPT
848 { return _M_current
[__n
]; }
851 operator+=(difference_type __n
) _GLIBCXX_NOEXCEPT
852 { _M_current
+= __n
; return *this; }
855 operator+(difference_type __n
) const _GLIBCXX_NOEXCEPT
856 { return __normal_iterator(_M_current
+ __n
); }
859 operator-=(difference_type __n
) _GLIBCXX_NOEXCEPT
860 { _M_current
-= __n
; return *this; }
863 operator-(difference_type __n
) const _GLIBCXX_NOEXCEPT
864 { return __normal_iterator(_M_current
- __n
); }
867 base() const _GLIBCXX_NOEXCEPT
868 { return _M_current
; }
871 // Note: In what follows, the left- and right-hand-side iterators are
872 // allowed to vary in types (conceptually in cv-qualification) so that
873 // comparison between cv-qualified and non-cv-qualified iterators be
874 // valid. However, the greedy and unfriendly operators in std::rel_ops
875 // will make overload resolution ambiguous (when in scope) if we don't
876 // provide overloads whose operands are of the same type. Can someone
877 // remind me what generic programming is about? -- Gaby
879 // Forward iterator requirements
880 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
882 operator==(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
883 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
885 { return __lhs
.base() == __rhs
.base(); }
887 template<typename _Iterator
, typename _Container
>
889 operator==(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
890 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
892 { return __lhs
.base() == __rhs
.base(); }
894 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
896 operator!=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
897 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
899 { return __lhs
.base() != __rhs
.base(); }
901 template<typename _Iterator
, typename _Container
>
903 operator!=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
904 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
906 { return __lhs
.base() != __rhs
.base(); }
908 // Random access iterator requirements
909 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
911 operator<(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
912 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
914 { return __lhs
.base() < __rhs
.base(); }
916 template<typename _Iterator
, typename _Container
>
918 operator<(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
919 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
921 { return __lhs
.base() < __rhs
.base(); }
923 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
925 operator>(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
926 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
928 { return __lhs
.base() > __rhs
.base(); }
930 template<typename _Iterator
, typename _Container
>
932 operator>(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
933 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
935 { return __lhs
.base() > __rhs
.base(); }
937 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
939 operator<=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
940 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
942 { return __lhs
.base() <= __rhs
.base(); }
944 template<typename _Iterator
, typename _Container
>
946 operator<=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
947 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
949 { return __lhs
.base() <= __rhs
.base(); }
951 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
953 operator>=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
954 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
956 { return __lhs
.base() >= __rhs
.base(); }
958 template<typename _Iterator
, typename _Container
>
960 operator>=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
961 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
963 { return __lhs
.base() >= __rhs
.base(); }
965 // _GLIBCXX_RESOLVE_LIB_DEFECTS
966 // According to the resolution of DR179 not only the various comparison
967 // operators but also operator- must accept mixed iterator/const_iterator
969 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
970 #if __cplusplus >= 201103L
973 operator-(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
974 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
) noexcept
975 -> decltype(__lhs
.base() - __rhs
.base())
977 inline typename __normal_iterator
<_IteratorL
, _Container
>::difference_type
978 operator-(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
979 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
981 { return __lhs
.base() - __rhs
.base(); }
983 template<typename _Iterator
, typename _Container
>
984 inline typename __normal_iterator
<_Iterator
, _Container
>::difference_type
985 operator-(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
986 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
988 { return __lhs
.base() - __rhs
.base(); }
990 template<typename _Iterator
, typename _Container
>
991 inline __normal_iterator
<_Iterator
, _Container
>
992 operator+(typename __normal_iterator
<_Iterator
, _Container
>::difference_type
993 __n
, const __normal_iterator
<_Iterator
, _Container
>& __i
)
995 { return __normal_iterator
<_Iterator
, _Container
>(__i
.base() + __n
); }
997 _GLIBCXX_END_NAMESPACE_VERSION
1000 namespace std
_GLIBCXX_VISIBILITY(default)
1002 _GLIBCXX_BEGIN_NAMESPACE_VERSION
1004 template<typename _Iterator
, typename _Container
>
1006 __niter_base(__gnu_cxx::__normal_iterator
<_Iterator
, _Container
> __it
)
1007 { return __it
.base(); }
1009 #if __cplusplus >= 201103L
1012 * @addtogroup iterators
1016 // 24.4.3 Move iterators
1018 * Class template move_iterator is an iterator adapter with the same
1019 * behavior as the underlying iterator except that its dereference
1020 * operator implicitly converts the value returned by the underlying
1021 * iterator's dereference operator to an rvalue reference. Some
1022 * generic algorithms can be called with move iterators to replace
1023 * copying with moving.
1025 template<typename _Iterator
>
1029 _Iterator _M_current
;
1031 typedef iterator_traits
<_Iterator
> __traits_type
;
1032 typedef typename
__traits_type::reference __base_ref
;
1035 typedef _Iterator iterator_type
;
1036 typedef typename
__traits_type::iterator_category iterator_category
;
1037 typedef typename
__traits_type::value_type value_type
;
1038 typedef typename
__traits_type::difference_type difference_type
;
1040 typedef _Iterator pointer
;
1041 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1042 // 2106. move_iterator wrapping iterators returning prvalues
1043 typedef typename conditional
<is_reference
<__base_ref
>::value
,
1044 typename remove_reference
<__base_ref
>::type
&&,
1045 __base_ref
>::type reference
;
1047 _GLIBCXX17_CONSTEXPR
1051 explicit _GLIBCXX17_CONSTEXPR
1052 move_iterator(iterator_type __i
)
1053 : _M_current(__i
) { }
1055 template<typename _Iter
>
1056 _GLIBCXX17_CONSTEXPR
1057 move_iterator(const move_iterator
<_Iter
>& __i
)
1058 : _M_current(__i
.base()) { }
1060 _GLIBCXX17_CONSTEXPR iterator_type
1062 { return _M_current
; }
1064 _GLIBCXX17_CONSTEXPR reference
1066 { return static_cast<reference
>(*_M_current
); }
1068 _GLIBCXX17_CONSTEXPR pointer
1070 { return _M_current
; }
1072 _GLIBCXX17_CONSTEXPR move_iterator
&
1079 _GLIBCXX17_CONSTEXPR move_iterator
1082 move_iterator __tmp
= *this;
1087 _GLIBCXX17_CONSTEXPR move_iterator
&
1094 _GLIBCXX17_CONSTEXPR move_iterator
1097 move_iterator __tmp
= *this;
1102 _GLIBCXX17_CONSTEXPR move_iterator
1103 operator+(difference_type __n
) const
1104 { return move_iterator(_M_current
+ __n
); }
1106 _GLIBCXX17_CONSTEXPR move_iterator
&
1107 operator+=(difference_type __n
)
1113 _GLIBCXX17_CONSTEXPR move_iterator
1114 operator-(difference_type __n
) const
1115 { return move_iterator(_M_current
- __n
); }
1117 _GLIBCXX17_CONSTEXPR move_iterator
&
1118 operator-=(difference_type __n
)
1124 _GLIBCXX17_CONSTEXPR reference
1125 operator[](difference_type __n
) const
1126 { return std::move(_M_current
[__n
]); }
1129 // Note: See __normal_iterator operators note from Gaby to understand
1130 // why there are always 2 versions for most of the move_iterator
1132 template<typename _IteratorL
, typename _IteratorR
>
1133 inline _GLIBCXX17_CONSTEXPR
bool
1134 operator==(const move_iterator
<_IteratorL
>& __x
,
1135 const move_iterator
<_IteratorR
>& __y
)
1136 { return __x
.base() == __y
.base(); }
1138 template<typename _Iterator
>
1139 inline _GLIBCXX17_CONSTEXPR
bool
1140 operator==(const move_iterator
<_Iterator
>& __x
,
1141 const move_iterator
<_Iterator
>& __y
)
1142 { return __x
.base() == __y
.base(); }
1144 template<typename _IteratorL
, typename _IteratorR
>
1145 inline _GLIBCXX17_CONSTEXPR
bool
1146 operator!=(const move_iterator
<_IteratorL
>& __x
,
1147 const move_iterator
<_IteratorR
>& __y
)
1148 { return !(__x
== __y
); }
1150 template<typename _Iterator
>
1151 inline _GLIBCXX17_CONSTEXPR
bool
1152 operator!=(const move_iterator
<_Iterator
>& __x
,
1153 const move_iterator
<_Iterator
>& __y
)
1154 { return !(__x
== __y
); }
1156 template<typename _IteratorL
, typename _IteratorR
>
1157 inline _GLIBCXX17_CONSTEXPR
bool
1158 operator<(const move_iterator
<_IteratorL
>& __x
,
1159 const move_iterator
<_IteratorR
>& __y
)
1160 { return __x
.base() < __y
.base(); }
1162 template<typename _Iterator
>
1163 inline _GLIBCXX17_CONSTEXPR
bool
1164 operator<(const move_iterator
<_Iterator
>& __x
,
1165 const move_iterator
<_Iterator
>& __y
)
1166 { return __x
.base() < __y
.base(); }
1168 template<typename _IteratorL
, typename _IteratorR
>
1169 inline _GLIBCXX17_CONSTEXPR
bool
1170 operator<=(const move_iterator
<_IteratorL
>& __x
,
1171 const move_iterator
<_IteratorR
>& __y
)
1172 { return !(__y
< __x
); }
1174 template<typename _Iterator
>
1175 inline _GLIBCXX17_CONSTEXPR
bool
1176 operator<=(const move_iterator
<_Iterator
>& __x
,
1177 const move_iterator
<_Iterator
>& __y
)
1178 { return !(__y
< __x
); }
1180 template<typename _IteratorL
, typename _IteratorR
>
1181 inline _GLIBCXX17_CONSTEXPR
bool
1182 operator>(const move_iterator
<_IteratorL
>& __x
,
1183 const move_iterator
<_IteratorR
>& __y
)
1184 { return __y
< __x
; }
1186 template<typename _Iterator
>
1187 inline _GLIBCXX17_CONSTEXPR
bool
1188 operator>(const move_iterator
<_Iterator
>& __x
,
1189 const move_iterator
<_Iterator
>& __y
)
1190 { return __y
< __x
; }
1192 template<typename _IteratorL
, typename _IteratorR
>
1193 inline _GLIBCXX17_CONSTEXPR
bool
1194 operator>=(const move_iterator
<_IteratorL
>& __x
,
1195 const move_iterator
<_IteratorR
>& __y
)
1196 { return !(__x
< __y
); }
1198 template<typename _Iterator
>
1199 inline _GLIBCXX17_CONSTEXPR
bool
1200 operator>=(const move_iterator
<_Iterator
>& __x
,
1201 const move_iterator
<_Iterator
>& __y
)
1202 { return !(__x
< __y
); }
1205 template<typename _IteratorL
, typename _IteratorR
>
1206 inline _GLIBCXX17_CONSTEXPR
auto
1207 operator-(const move_iterator
<_IteratorL
>& __x
,
1208 const move_iterator
<_IteratorR
>& __y
)
1209 -> decltype(__x
.base() - __y
.base())
1210 { return __x
.base() - __y
.base(); }
1212 template<typename _Iterator
>
1213 inline _GLIBCXX17_CONSTEXPR move_iterator
<_Iterator
>
1214 operator+(typename move_iterator
<_Iterator
>::difference_type __n
,
1215 const move_iterator
<_Iterator
>& __x
)
1216 { return __x
+ __n
; }
1218 template<typename _Iterator
>
1219 inline _GLIBCXX17_CONSTEXPR move_iterator
<_Iterator
>
1220 make_move_iterator(_Iterator __i
)
1221 { return move_iterator
<_Iterator
>(__i
); }
1223 template<typename _Iterator
, typename _ReturnType
1224 = typename conditional
<__move_if_noexcept_cond
1225 <typename iterator_traits
<_Iterator
>::value_type
>::value
,
1226 _Iterator
, move_iterator
<_Iterator
>>::type
>
1227 inline _GLIBCXX17_CONSTEXPR _ReturnType
1228 __make_move_if_noexcept_iterator(_Iterator __i
)
1229 { return _ReturnType(__i
); }
1231 // Overload for pointers that matches std::move_if_noexcept more closely,
1232 // returning a constant iterator when we don't want to move.
1233 template<typename _Tp
, typename _ReturnType
1234 = typename conditional
<__move_if_noexcept_cond
<_Tp
>::value
,
1235 const _Tp
*, move_iterator
<_Tp
*>>::type
>
1236 inline _GLIBCXX17_CONSTEXPR _ReturnType
1237 __make_move_if_noexcept_iterator(_Tp
* __i
)
1238 { return _ReturnType(__i
); }
1240 // @} group iterators
1242 template<typename _Iterator
>
1244 __niter_base(move_iterator
<_Iterator
> __it
)
1245 -> decltype(make_move_iterator(__niter_base(__it
.base())))
1246 { return make_move_iterator(__niter_base(__it
.base())); }
1248 template<typename _Iterator
>
1249 struct __is_move_iterator
<move_iterator
<_Iterator
> >
1251 enum { __value
= 1 };
1252 typedef __true_type __type
;
1255 template<typename _Iterator
>
1257 __miter_base(move_iterator
<_Iterator
> __it
)
1258 -> decltype(__miter_base(__it
.base()))
1259 { return __miter_base(__it
.base()); }
1261 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter)
1262 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \
1263 std::__make_move_if_noexcept_iterator(_Iter)
1265 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
1266 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter)
1269 #if __cpp_deduction_guides >= 201606
1270 // These helper traits are used for deduction guides
1271 // of associative containers.
1272 template<typename _InputIterator
>
1273 using __iter_key_t
= remove_const_t
<
1274 typename iterator_traits
<_InputIterator
>::value_type::first_type
>;
1276 template<typename _InputIterator
>
1277 using __iter_val_t
=
1278 typename iterator_traits
<_InputIterator
>::value_type::second_type
;
1280 template<typename _T1
, typename _T2
>
1283 template<typename _InputIterator
>
1284 using __iter_to_alloc_t
=
1285 pair
<add_const_t
<__iter_key_t
<_InputIterator
>>,
1286 __iter_val_t
<_InputIterator
>>;
1290 _GLIBCXX_END_NAMESPACE_VERSION
1293 #ifdef _GLIBCXX_DEBUG
1294 # include <debug/stl_iterator.h>