1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001-2017 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
) { }
142 * A %reverse_iterator across other types can be copied if the
143 * underlying %iterator can be converted to the type of @c current.
145 template<typename _Iter
>
147 reverse_iterator(const reverse_iterator
<_Iter
>& __x
)
148 : current(__x
.base()) { }
151 * @return @c current, the %iterator used for underlying work.
153 _GLIBCXX17_CONSTEXPR iterator_type
158 * @return A reference to the value at @c --current
160 * This requires that @c --current is dereferenceable.
162 * @warning This implementation requires that for an iterator of the
163 * underlying iterator type, @c x, a reference obtained by
164 * @c *x remains valid after @c x has been modified or
165 * destroyed. This is a bug: http://gcc.gnu.org/PR51823
167 _GLIBCXX17_CONSTEXPR reference
170 _Iterator __tmp
= current
;
175 * @return A pointer to the value at @c --current
177 * This requires that @c --current is dereferenceable.
179 _GLIBCXX17_CONSTEXPR pointer
181 { return &(operator*()); }
186 * Decrements the underlying iterator.
188 _GLIBCXX17_CONSTEXPR reverse_iterator
&
196 * @return The original value of @c *this
198 * Decrements the underlying iterator.
200 _GLIBCXX17_CONSTEXPR reverse_iterator
203 reverse_iterator __tmp
= *this;
211 * Increments the underlying iterator.
213 _GLIBCXX17_CONSTEXPR reverse_iterator
&
221 * @return A reverse_iterator with the previous value of @c *this
223 * Increments the underlying iterator.
225 _GLIBCXX17_CONSTEXPR reverse_iterator
228 reverse_iterator __tmp
= *this;
234 * @return A reverse_iterator that refers to @c current - @a __n
236 * The underlying iterator must be a Random Access Iterator.
238 _GLIBCXX17_CONSTEXPR reverse_iterator
239 operator+(difference_type __n
) const
240 { return reverse_iterator(current
- __n
); }
245 * Moves the underlying iterator backwards @a __n steps.
246 * The underlying iterator must be a Random Access Iterator.
248 _GLIBCXX17_CONSTEXPR reverse_iterator
&
249 operator+=(difference_type __n
)
256 * @return A reverse_iterator that refers to @c current - @a __n
258 * The underlying iterator must be a Random Access Iterator.
260 _GLIBCXX17_CONSTEXPR reverse_iterator
261 operator-(difference_type __n
) const
262 { return reverse_iterator(current
+ __n
); }
267 * Moves the underlying iterator forwards @a __n steps.
268 * The underlying iterator must be a Random Access Iterator.
270 _GLIBCXX17_CONSTEXPR reverse_iterator
&
271 operator-=(difference_type __n
)
278 * @return The value at @c current - @a __n - 1
280 * The underlying iterator must be a Random Access Iterator.
282 _GLIBCXX17_CONSTEXPR reference
283 operator[](difference_type __n
) const
284 { return *(*this + __n
); }
289 * @param __x A %reverse_iterator.
290 * @param __y A %reverse_iterator.
291 * @return A simple bool.
293 * Reverse iterators forward many operations to their underlying base()
294 * iterators. Others are implemented in terms of one another.
297 template<typename _Iterator
>
298 inline _GLIBCXX17_CONSTEXPR
bool
299 operator==(const reverse_iterator
<_Iterator
>& __x
,
300 const reverse_iterator
<_Iterator
>& __y
)
301 { return __x
.base() == __y
.base(); }
303 template<typename _Iterator
>
304 inline _GLIBCXX17_CONSTEXPR
bool
305 operator<(const reverse_iterator
<_Iterator
>& __x
,
306 const reverse_iterator
<_Iterator
>& __y
)
307 { return __y
.base() < __x
.base(); }
309 template<typename _Iterator
>
310 inline _GLIBCXX17_CONSTEXPR
bool
311 operator!=(const reverse_iterator
<_Iterator
>& __x
,
312 const reverse_iterator
<_Iterator
>& __y
)
313 { return !(__x
== __y
); }
315 template<typename _Iterator
>
316 inline _GLIBCXX17_CONSTEXPR
bool
317 operator>(const reverse_iterator
<_Iterator
>& __x
,
318 const reverse_iterator
<_Iterator
>& __y
)
319 { return __y
< __x
; }
321 template<typename _Iterator
>
322 inline _GLIBCXX17_CONSTEXPR
bool
323 operator<=(const reverse_iterator
<_Iterator
>& __x
,
324 const reverse_iterator
<_Iterator
>& __y
)
325 { return !(__y
< __x
); }
327 template<typename _Iterator
>
328 inline _GLIBCXX17_CONSTEXPR
bool
329 operator>=(const reverse_iterator
<_Iterator
>& __x
,
330 const reverse_iterator
<_Iterator
>& __y
)
331 { return !(__x
< __y
); }
333 // _GLIBCXX_RESOLVE_LIB_DEFECTS
334 // DR 280. Comparison of reverse_iterator to const reverse_iterator.
335 template<typename _IteratorL
, typename _IteratorR
>
336 inline _GLIBCXX17_CONSTEXPR
bool
337 operator==(const reverse_iterator
<_IteratorL
>& __x
,
338 const reverse_iterator
<_IteratorR
>& __y
)
339 { return __x
.base() == __y
.base(); }
341 template<typename _IteratorL
, typename _IteratorR
>
342 inline _GLIBCXX17_CONSTEXPR
bool
343 operator<(const reverse_iterator
<_IteratorL
>& __x
,
344 const reverse_iterator
<_IteratorR
>& __y
)
345 { return __y
.base() < __x
.base(); }
347 template<typename _IteratorL
, typename _IteratorR
>
348 inline _GLIBCXX17_CONSTEXPR
bool
349 operator!=(const reverse_iterator
<_IteratorL
>& __x
,
350 const reverse_iterator
<_IteratorR
>& __y
)
351 { return !(__x
== __y
); }
353 template<typename _IteratorL
, typename _IteratorR
>
354 inline _GLIBCXX17_CONSTEXPR
bool
355 operator>(const reverse_iterator
<_IteratorL
>& __x
,
356 const reverse_iterator
<_IteratorR
>& __y
)
357 { return __y
< __x
; }
359 template<typename _IteratorL
, typename _IteratorR
>
360 inline _GLIBCXX17_CONSTEXPR
bool
361 operator<=(const reverse_iterator
<_IteratorL
>& __x
,
362 const reverse_iterator
<_IteratorR
>& __y
)
363 { return !(__y
< __x
); }
365 template<typename _IteratorL
, typename _IteratorR
>
366 inline _GLIBCXX17_CONSTEXPR
bool
367 operator>=(const reverse_iterator
<_IteratorL
>& __x
,
368 const reverse_iterator
<_IteratorR
>& __y
)
369 { return !(__x
< __y
); }
372 #if __cplusplus < 201103L
373 template<typename _Iterator
>
374 inline typename reverse_iterator
<_Iterator
>::difference_type
375 operator-(const reverse_iterator
<_Iterator
>& __x
,
376 const reverse_iterator
<_Iterator
>& __y
)
377 { return __y
.base() - __x
.base(); }
379 template<typename _IteratorL
, typename _IteratorR
>
380 inline typename reverse_iterator
<_IteratorL
>::difference_type
381 operator-(const reverse_iterator
<_IteratorL
>& __x
,
382 const reverse_iterator
<_IteratorR
>& __y
)
383 { return __y
.base() - __x
.base(); }
385 // _GLIBCXX_RESOLVE_LIB_DEFECTS
386 // DR 685. reverse_iterator/move_iterator difference has invalid signatures
387 template<typename _IteratorL
, typename _IteratorR
>
388 inline _GLIBCXX17_CONSTEXPR
auto
389 operator-(const reverse_iterator
<_IteratorL
>& __x
,
390 const reverse_iterator
<_IteratorR
>& __y
)
391 -> decltype(__y
.base() - __x
.base())
392 { return __y
.base() - __x
.base(); }
395 template<typename _Iterator
>
396 inline _GLIBCXX17_CONSTEXPR reverse_iterator
<_Iterator
>
397 operator+(typename reverse_iterator
<_Iterator
>::difference_type __n
,
398 const reverse_iterator
<_Iterator
>& __x
)
399 { return reverse_iterator
<_Iterator
>(__x
.base() - __n
); }
401 #if __cplusplus >= 201103L
402 // Same as C++14 make_reverse_iterator but used in C++03 mode too.
403 template<typename _Iterator
>
404 inline _GLIBCXX17_CONSTEXPR reverse_iterator
<_Iterator
>
405 __make_reverse_iterator(_Iterator __i
)
406 { return reverse_iterator
<_Iterator
>(__i
); }
408 # if __cplusplus > 201103L
409 # define __cpp_lib_make_reverse_iterator 201402
411 // _GLIBCXX_RESOLVE_LIB_DEFECTS
412 // DR 2285. make_reverse_iterator
413 /// Generator function for reverse_iterator.
414 template<typename _Iterator
>
415 inline _GLIBCXX17_CONSTEXPR reverse_iterator
<_Iterator
>
416 make_reverse_iterator(_Iterator __i
)
417 { return reverse_iterator
<_Iterator
>(__i
); }
421 #if __cplusplus >= 201103L
422 template<typename _Iterator
>
424 __niter_base(reverse_iterator
<_Iterator
> __it
)
425 -> decltype(__make_reverse_iterator(__niter_base(__it
.base())))
426 { return __make_reverse_iterator(__niter_base(__it
.base())); }
428 template<typename _Iterator
>
429 struct __is_move_iterator
<reverse_iterator
<_Iterator
> >
430 : __is_move_iterator
<_Iterator
>
433 template<typename _Iterator
>
435 __miter_base(reverse_iterator
<_Iterator
> __it
)
436 -> decltype(__make_reverse_iterator(__miter_base(__it
.base())))
437 { return __make_reverse_iterator(__miter_base(__it
.base())); }
440 // 24.4.2.2.1 back_insert_iterator
442 * @brief Turns assignment into insertion.
444 * These are output iterators, constructed from a container-of-T.
445 * Assigning a T to the iterator appends it to the container using
448 * Tip: Using the back_inserter function to create these iterators can
451 template<typename _Container
>
452 class back_insert_iterator
453 : public iterator
<output_iterator_tag
, void, void, void, void>
456 _Container
* container
;
459 /// A nested typedef for the type of whatever container you used.
460 typedef _Container container_type
;
462 /// The only way to create this %iterator is with a container.
464 back_insert_iterator(_Container
& __x
)
465 : container(std::__addressof(__x
)) { }
468 * @param __value An instance of whatever type
469 * container_type::const_reference is; presumably a
470 * reference-to-const T for container<T>.
471 * @return This %iterator, for chained operations.
473 * This kind of %iterator doesn't really have a @a position in the
474 * container (you can think of the position as being permanently at
475 * the end, if you like). Assigning a value to the %iterator will
476 * always append the value to the end of the container.
478 #if __cplusplus < 201103L
479 back_insert_iterator
&
480 operator=(typename
_Container::const_reference __value
)
482 container
->push_back(__value
);
486 back_insert_iterator
&
487 operator=(const typename
_Container::value_type
& __value
)
489 container
->push_back(__value
);
493 back_insert_iterator
&
494 operator=(typename
_Container::value_type
&& __value
)
496 container
->push_back(std::move(__value
));
501 /// Simply returns *this.
502 back_insert_iterator
&
506 /// Simply returns *this. (This %iterator does not @a move.)
507 back_insert_iterator
&
511 /// Simply returns *this. (This %iterator does not @a move.)
518 * @param __x A container of arbitrary type.
519 * @return An instance of back_insert_iterator working on @p __x.
521 * This wrapper function helps in creating back_insert_iterator instances.
522 * Typing the name of the %iterator requires knowing the precise full
523 * type of the container, which can be tedious and impedes generic
524 * programming. Using this function lets you take advantage of automatic
525 * template parameter deduction, making the compiler match the correct
528 template<typename _Container
>
529 inline back_insert_iterator
<_Container
>
530 back_inserter(_Container
& __x
)
531 { return back_insert_iterator
<_Container
>(__x
); }
534 * @brief Turns assignment into insertion.
536 * These are output iterators, constructed from a container-of-T.
537 * Assigning a T to the iterator prepends it to the container using
540 * Tip: Using the front_inserter function to create these iterators can
543 template<typename _Container
>
544 class front_insert_iterator
545 : public iterator
<output_iterator_tag
, void, void, void, void>
548 _Container
* container
;
551 /// A nested typedef for the type of whatever container you used.
552 typedef _Container container_type
;
554 /// The only way to create this %iterator is with a container.
555 explicit front_insert_iterator(_Container
& __x
)
556 : container(std::__addressof(__x
)) { }
559 * @param __value An instance of whatever type
560 * container_type::const_reference is; presumably a
561 * reference-to-const T for container<T>.
562 * @return This %iterator, for chained operations.
564 * This kind of %iterator doesn't really have a @a position in the
565 * container (you can think of the position as being permanently at
566 * the front, if you like). Assigning a value to the %iterator will
567 * always prepend the value to the front of the container.
569 #if __cplusplus < 201103L
570 front_insert_iterator
&
571 operator=(typename
_Container::const_reference __value
)
573 container
->push_front(__value
);
577 front_insert_iterator
&
578 operator=(const typename
_Container::value_type
& __value
)
580 container
->push_front(__value
);
584 front_insert_iterator
&
585 operator=(typename
_Container::value_type
&& __value
)
587 container
->push_front(std::move(__value
));
592 /// Simply returns *this.
593 front_insert_iterator
&
597 /// Simply returns *this. (This %iterator does not @a move.)
598 front_insert_iterator
&
602 /// Simply returns *this. (This %iterator does not @a move.)
603 front_insert_iterator
609 * @param __x A container of arbitrary type.
610 * @return An instance of front_insert_iterator working on @p x.
612 * This wrapper function helps in creating front_insert_iterator instances.
613 * Typing the name of the %iterator requires knowing the precise full
614 * type of the container, which can be tedious and impedes generic
615 * programming. Using this function lets you take advantage of automatic
616 * template parameter deduction, making the compiler match the correct
619 template<typename _Container
>
620 inline front_insert_iterator
<_Container
>
621 front_inserter(_Container
& __x
)
622 { return front_insert_iterator
<_Container
>(__x
); }
625 * @brief Turns assignment into insertion.
627 * These are output iterators, constructed from a container-of-T.
628 * Assigning a T to the iterator inserts it in the container at the
629 * %iterator's position, rather than overwriting the value at that
632 * (Sequences will actually insert a @e copy of the value before the
633 * %iterator's position.)
635 * Tip: Using the inserter function to create these iterators can
638 template<typename _Container
>
639 class insert_iterator
640 : public iterator
<output_iterator_tag
, void, void, void, void>
643 _Container
* container
;
644 typename
_Container::iterator iter
;
647 /// A nested typedef for the type of whatever container you used.
648 typedef _Container container_type
;
651 * The only way to create this %iterator is with a container and an
652 * initial position (a normal %iterator into the container).
654 insert_iterator(_Container
& __x
, typename
_Container::iterator __i
)
655 : container(std::__addressof(__x
)), iter(__i
) {}
658 * @param __value An instance of whatever type
659 * container_type::const_reference is; presumably a
660 * reference-to-const T for container<T>.
661 * @return This %iterator, for chained operations.
663 * This kind of %iterator maintains its own position in the
664 * container. Assigning a value to the %iterator will insert the
665 * value into the container at the place before the %iterator.
667 * The position is maintained such that subsequent assignments will
668 * insert values immediately after one another. For example,
670 * // vector v contains A and Z
672 * insert_iterator i (v, ++v.begin());
677 * // vector v contains A, 1, 2, 3, and Z
680 #if __cplusplus < 201103L
682 operator=(typename
_Container::const_reference __value
)
684 iter
= container
->insert(iter
, __value
);
690 operator=(const typename
_Container::value_type
& __value
)
692 iter
= container
->insert(iter
, __value
);
698 operator=(typename
_Container::value_type
&& __value
)
700 iter
= container
->insert(iter
, std::move(__value
));
706 /// Simply returns *this.
711 /// Simply returns *this. (This %iterator does not @a move.)
716 /// Simply returns *this. (This %iterator does not @a move.)
723 * @param __x A container of arbitrary type.
724 * @param __i An iterator into the container.
725 * @return An instance of insert_iterator working on @p __x.
727 * This wrapper function helps in creating insert_iterator instances.
728 * Typing the name of the %iterator requires knowing the precise full
729 * type of the container, which can be tedious and impedes generic
730 * programming. Using this function lets you take advantage of automatic
731 * template parameter deduction, making the compiler match the correct
734 template<typename _Container
, typename _Iterator
>
735 inline insert_iterator
<_Container
>
736 inserter(_Container
& __x
, _Iterator __i
)
738 return insert_iterator
<_Container
>(__x
,
739 typename
_Container::iterator(__i
));
742 // @} group iterators
744 _GLIBCXX_END_NAMESPACE_VERSION
747 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
749 _GLIBCXX_BEGIN_NAMESPACE_VERSION
751 // This iterator adapter is @a normal in the sense that it does not
752 // change the semantics of any of the operators of its iterator
753 // parameter. Its primary purpose is to convert an iterator that is
754 // not a class, e.g. a pointer, into an iterator that is a class.
755 // The _Container parameter exists solely so that different containers
756 // using this template can instantiate different types, even if the
757 // _Iterator parameter is the same.
758 using std::iterator_traits
;
760 template<typename _Iterator
, typename _Container
>
761 class __normal_iterator
764 _Iterator _M_current
;
766 typedef iterator_traits
<_Iterator
> __traits_type
;
769 typedef _Iterator iterator_type
;
770 typedef typename
__traits_type::iterator_category iterator_category
;
771 typedef typename
__traits_type::value_type value_type
;
772 typedef typename
__traits_type::difference_type difference_type
;
773 typedef typename
__traits_type::reference reference
;
774 typedef typename
__traits_type::pointer pointer
;
776 _GLIBCXX_CONSTEXPR
__normal_iterator() _GLIBCXX_NOEXCEPT
777 : _M_current(_Iterator()) { }
780 __normal_iterator(const _Iterator
& __i
) _GLIBCXX_NOEXCEPT
781 : _M_current(__i
) { }
783 // Allow iterator to const_iterator conversion
784 template<typename _Iter
>
785 __normal_iterator(const __normal_iterator
<_Iter
,
786 typename __enable_if
<
787 (std::__are_same
<_Iter
, typename
_Container::pointer
>::__value
),
788 _Container
>::__type
>& __i
) _GLIBCXX_NOEXCEPT
789 : _M_current(__i
.base()) { }
791 // Forward iterator requirements
793 operator*() const _GLIBCXX_NOEXCEPT
794 { return *_M_current
; }
797 operator->() const _GLIBCXX_NOEXCEPT
798 { return _M_current
; }
801 operator++() _GLIBCXX_NOEXCEPT
808 operator++(int) _GLIBCXX_NOEXCEPT
809 { return __normal_iterator(_M_current
++); }
811 // Bidirectional iterator requirements
813 operator--() _GLIBCXX_NOEXCEPT
820 operator--(int) _GLIBCXX_NOEXCEPT
821 { return __normal_iterator(_M_current
--); }
823 // Random access iterator requirements
825 operator[](difference_type __n
) const _GLIBCXX_NOEXCEPT
826 { return _M_current
[__n
]; }
829 operator+=(difference_type __n
) _GLIBCXX_NOEXCEPT
830 { _M_current
+= __n
; return *this; }
833 operator+(difference_type __n
) const _GLIBCXX_NOEXCEPT
834 { return __normal_iterator(_M_current
+ __n
); }
837 operator-=(difference_type __n
) _GLIBCXX_NOEXCEPT
838 { _M_current
-= __n
; return *this; }
841 operator-(difference_type __n
) const _GLIBCXX_NOEXCEPT
842 { return __normal_iterator(_M_current
- __n
); }
845 base() const _GLIBCXX_NOEXCEPT
846 { return _M_current
; }
849 // Note: In what follows, the left- and right-hand-side iterators are
850 // allowed to vary in types (conceptually in cv-qualification) so that
851 // comparison between cv-qualified and non-cv-qualified iterators be
852 // valid. However, the greedy and unfriendly operators in std::rel_ops
853 // will make overload resolution ambiguous (when in scope) if we don't
854 // provide overloads whose operands are of the same type. Can someone
855 // remind me what generic programming is about? -- Gaby
857 // Forward iterator requirements
858 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
860 operator==(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
861 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
863 { return __lhs
.base() == __rhs
.base(); }
865 template<typename _Iterator
, typename _Container
>
867 operator==(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
868 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
870 { return __lhs
.base() == __rhs
.base(); }
872 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
874 operator!=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
875 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
877 { return __lhs
.base() != __rhs
.base(); }
879 template<typename _Iterator
, typename _Container
>
881 operator!=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
882 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
884 { return __lhs
.base() != __rhs
.base(); }
886 // Random access iterator requirements
887 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
889 operator<(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
890 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
892 { return __lhs
.base() < __rhs
.base(); }
894 template<typename _Iterator
, typename _Container
>
896 operator<(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
897 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
899 { return __lhs
.base() < __rhs
.base(); }
901 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
903 operator>(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
904 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
906 { return __lhs
.base() > __rhs
.base(); }
908 template<typename _Iterator
, typename _Container
>
910 operator>(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
911 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
913 { return __lhs
.base() > __rhs
.base(); }
915 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
917 operator<=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
918 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
920 { return __lhs
.base() <= __rhs
.base(); }
922 template<typename _Iterator
, typename _Container
>
924 operator<=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
925 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
927 { return __lhs
.base() <= __rhs
.base(); }
929 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
931 operator>=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
932 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
934 { return __lhs
.base() >= __rhs
.base(); }
936 template<typename _Iterator
, typename _Container
>
938 operator>=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
939 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
941 { return __lhs
.base() >= __rhs
.base(); }
943 // _GLIBCXX_RESOLVE_LIB_DEFECTS
944 // According to the resolution of DR179 not only the various comparison
945 // operators but also operator- must accept mixed iterator/const_iterator
947 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
948 #if __cplusplus >= 201103L
951 operator-(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
952 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
) noexcept
953 -> decltype(__lhs
.base() - __rhs
.base())
955 inline typename __normal_iterator
<_IteratorL
, _Container
>::difference_type
956 operator-(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
957 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
959 { return __lhs
.base() - __rhs
.base(); }
961 template<typename _Iterator
, typename _Container
>
962 inline typename __normal_iterator
<_Iterator
, _Container
>::difference_type
963 operator-(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
964 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
966 { return __lhs
.base() - __rhs
.base(); }
968 template<typename _Iterator
, typename _Container
>
969 inline __normal_iterator
<_Iterator
, _Container
>
970 operator+(typename __normal_iterator
<_Iterator
, _Container
>::difference_type
971 __n
, const __normal_iterator
<_Iterator
, _Container
>& __i
)
973 { return __normal_iterator
<_Iterator
, _Container
>(__i
.base() + __n
); }
975 _GLIBCXX_END_NAMESPACE_VERSION
978 namespace std
_GLIBCXX_VISIBILITY(default)
980 _GLIBCXX_BEGIN_NAMESPACE_VERSION
982 template<typename _Iterator
, typename _Container
>
984 __niter_base(__gnu_cxx::__normal_iterator
<_Iterator
, _Container
> __it
)
985 { return __it
.base(); }
987 #if __cplusplus >= 201103L
990 * @addtogroup iterators
994 // 24.4.3 Move iterators
996 * Class template move_iterator is an iterator adapter with the same
997 * behavior as the underlying iterator except that its dereference
998 * operator implicitly converts the value returned by the underlying
999 * iterator's dereference operator to an rvalue reference. Some
1000 * generic algorithms can be called with move iterators to replace
1001 * copying with moving.
1003 template<typename _Iterator
>
1007 _Iterator _M_current
;
1009 typedef iterator_traits
<_Iterator
> __traits_type
;
1010 typedef typename
__traits_type::reference __base_ref
;
1013 typedef _Iterator iterator_type
;
1014 typedef typename
__traits_type::iterator_category iterator_category
;
1015 typedef typename
__traits_type::value_type value_type
;
1016 typedef typename
__traits_type::difference_type difference_type
;
1018 typedef _Iterator pointer
;
1019 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1020 // 2106. move_iterator wrapping iterators returning prvalues
1021 typedef typename conditional
<is_reference
<__base_ref
>::value
,
1022 typename remove_reference
<__base_ref
>::type
&&,
1023 __base_ref
>::type reference
;
1025 _GLIBCXX17_CONSTEXPR
1029 explicit _GLIBCXX17_CONSTEXPR
1030 move_iterator(iterator_type __i
)
1031 : _M_current(__i
) { }
1033 template<typename _Iter
>
1034 _GLIBCXX17_CONSTEXPR
1035 move_iterator(const move_iterator
<_Iter
>& __i
)
1036 : _M_current(__i
.base()) { }
1038 _GLIBCXX17_CONSTEXPR iterator_type
1040 { return _M_current
; }
1042 _GLIBCXX17_CONSTEXPR reference
1044 { return static_cast<reference
>(*_M_current
); }
1046 _GLIBCXX17_CONSTEXPR pointer
1048 { return _M_current
; }
1050 _GLIBCXX17_CONSTEXPR move_iterator
&
1057 _GLIBCXX17_CONSTEXPR move_iterator
1060 move_iterator __tmp
= *this;
1065 _GLIBCXX17_CONSTEXPR move_iterator
&
1072 _GLIBCXX17_CONSTEXPR move_iterator
1075 move_iterator __tmp
= *this;
1080 _GLIBCXX17_CONSTEXPR move_iterator
1081 operator+(difference_type __n
) const
1082 { return move_iterator(_M_current
+ __n
); }
1084 _GLIBCXX17_CONSTEXPR move_iterator
&
1085 operator+=(difference_type __n
)
1091 _GLIBCXX17_CONSTEXPR move_iterator
1092 operator-(difference_type __n
) const
1093 { return move_iterator(_M_current
- __n
); }
1095 _GLIBCXX17_CONSTEXPR move_iterator
&
1096 operator-=(difference_type __n
)
1102 _GLIBCXX17_CONSTEXPR reference
1103 operator[](difference_type __n
) const
1104 { return std::move(_M_current
[__n
]); }
1107 // Note: See __normal_iterator operators note from Gaby to understand
1108 // why there are always 2 versions for most of the move_iterator
1110 template<typename _IteratorL
, typename _IteratorR
>
1111 inline _GLIBCXX17_CONSTEXPR
bool
1112 operator==(const move_iterator
<_IteratorL
>& __x
,
1113 const move_iterator
<_IteratorR
>& __y
)
1114 { return __x
.base() == __y
.base(); }
1116 template<typename _Iterator
>
1117 inline _GLIBCXX17_CONSTEXPR
bool
1118 operator==(const move_iterator
<_Iterator
>& __x
,
1119 const move_iterator
<_Iterator
>& __y
)
1120 { return __x
.base() == __y
.base(); }
1122 template<typename _IteratorL
, typename _IteratorR
>
1123 inline _GLIBCXX17_CONSTEXPR
bool
1124 operator!=(const move_iterator
<_IteratorL
>& __x
,
1125 const move_iterator
<_IteratorR
>& __y
)
1126 { return !(__x
== __y
); }
1128 template<typename _Iterator
>
1129 inline _GLIBCXX17_CONSTEXPR
bool
1130 operator!=(const move_iterator
<_Iterator
>& __x
,
1131 const move_iterator
<_Iterator
>& __y
)
1132 { return !(__x
== __y
); }
1134 template<typename _IteratorL
, typename _IteratorR
>
1135 inline _GLIBCXX17_CONSTEXPR
bool
1136 operator<(const move_iterator
<_IteratorL
>& __x
,
1137 const move_iterator
<_IteratorR
>& __y
)
1138 { return __x
.base() < __y
.base(); }
1140 template<typename _Iterator
>
1141 inline _GLIBCXX17_CONSTEXPR
bool
1142 operator<(const move_iterator
<_Iterator
>& __x
,
1143 const move_iterator
<_Iterator
>& __y
)
1144 { return __x
.base() < __y
.base(); }
1146 template<typename _IteratorL
, typename _IteratorR
>
1147 inline _GLIBCXX17_CONSTEXPR
bool
1148 operator<=(const move_iterator
<_IteratorL
>& __x
,
1149 const move_iterator
<_IteratorR
>& __y
)
1150 { return !(__y
< __x
); }
1152 template<typename _Iterator
>
1153 inline _GLIBCXX17_CONSTEXPR
bool
1154 operator<=(const move_iterator
<_Iterator
>& __x
,
1155 const move_iterator
<_Iterator
>& __y
)
1156 { return !(__y
< __x
); }
1158 template<typename _IteratorL
, typename _IteratorR
>
1159 inline _GLIBCXX17_CONSTEXPR
bool
1160 operator>(const move_iterator
<_IteratorL
>& __x
,
1161 const move_iterator
<_IteratorR
>& __y
)
1162 { return __y
< __x
; }
1164 template<typename _Iterator
>
1165 inline _GLIBCXX17_CONSTEXPR
bool
1166 operator>(const move_iterator
<_Iterator
>& __x
,
1167 const move_iterator
<_Iterator
>& __y
)
1168 { return __y
< __x
; }
1170 template<typename _IteratorL
, typename _IteratorR
>
1171 inline _GLIBCXX17_CONSTEXPR
bool
1172 operator>=(const move_iterator
<_IteratorL
>& __x
,
1173 const move_iterator
<_IteratorR
>& __y
)
1174 { return !(__x
< __y
); }
1176 template<typename _Iterator
>
1177 inline _GLIBCXX17_CONSTEXPR
bool
1178 operator>=(const move_iterator
<_Iterator
>& __x
,
1179 const move_iterator
<_Iterator
>& __y
)
1180 { return !(__x
< __y
); }
1183 template<typename _IteratorL
, typename _IteratorR
>
1184 inline _GLIBCXX17_CONSTEXPR
auto
1185 operator-(const move_iterator
<_IteratorL
>& __x
,
1186 const move_iterator
<_IteratorR
>& __y
)
1187 -> decltype(__x
.base() - __y
.base())
1188 { return __x
.base() - __y
.base(); }
1190 template<typename _Iterator
>
1191 inline _GLIBCXX17_CONSTEXPR move_iterator
<_Iterator
>
1192 operator+(typename move_iterator
<_Iterator
>::difference_type __n
,
1193 const move_iterator
<_Iterator
>& __x
)
1194 { return __x
+ __n
; }
1196 template<typename _Iterator
>
1197 inline _GLIBCXX17_CONSTEXPR move_iterator
<_Iterator
>
1198 make_move_iterator(_Iterator __i
)
1199 { return move_iterator
<_Iterator
>(__i
); }
1201 template<typename _Iterator
, typename _ReturnType
1202 = typename conditional
<__move_if_noexcept_cond
1203 <typename iterator_traits
<_Iterator
>::value_type
>::value
,
1204 _Iterator
, move_iterator
<_Iterator
>>::type
>
1205 inline _GLIBCXX17_CONSTEXPR _ReturnType
1206 __make_move_if_noexcept_iterator(_Iterator __i
)
1207 { return _ReturnType(__i
); }
1209 // Overload for pointers that matches std::move_if_noexcept more closely,
1210 // returning a constant iterator when we don't want to move.
1211 template<typename _Tp
, typename _ReturnType
1212 = typename conditional
<__move_if_noexcept_cond
<_Tp
>::value
,
1213 const _Tp
*, move_iterator
<_Tp
*>>::type
>
1214 inline _GLIBCXX17_CONSTEXPR _ReturnType
1215 __make_move_if_noexcept_iterator(_Tp
* __i
)
1216 { return _ReturnType(__i
); }
1218 // @} group iterators
1220 template<typename _Iterator
>
1222 __niter_base(move_iterator
<_Iterator
> __it
)
1223 -> decltype(make_move_iterator(__niter_base(__it
.base())))
1224 { return make_move_iterator(__niter_base(__it
.base())); }
1226 template<typename _Iterator
>
1227 struct __is_move_iterator
<move_iterator
<_Iterator
> >
1229 enum { __value
= 1 };
1230 typedef __true_type __type
;
1233 template<typename _Iterator
>
1235 __miter_base(move_iterator
<_Iterator
> __it
)
1236 -> decltype(__miter_base(__it
.base()))
1237 { return __miter_base(__it
.base()); }
1239 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter)
1240 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \
1241 std::__make_move_if_noexcept_iterator(_Iter)
1243 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
1244 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter)
1247 _GLIBCXX_END_NAMESPACE_VERSION
1250 #ifdef _GLIBCXX_DEBUG
1251 # include <debug/stl_iterator.h>