1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
30 * Hewlett-Packard Company
32 * Permission to use, copy, modify, distribute and sell this software
33 * and its documentation for any purpose is hereby granted without fee,
34 * provided that the above copyright notice appear in all copies and
35 * that both that copyright notice and this permission notice appear
36 * in supporting documentation. Hewlett-Packard Company makes no
37 * representations about the suitability of this software for any
38 * purpose. It is provided "as is" without express or implied warranty.
41 * Copyright (c) 1996-1998
42 * Silicon Graphics Computer Systems, Inc.
44 * Permission to use, copy, modify, distribute and sell this software
45 * and its documentation for any purpose is hereby granted without fee,
46 * provided that the above copyright notice appear in all copies and
47 * that both that copyright notice and this permission notice appear
48 * in supporting documentation. Silicon Graphics makes no
49 * representations about the suitability of this software for any
50 * purpose. It is provided "as is" without express or implied warranty.
53 /** @file bits/stl_iterator.h
54 * This is an internal header file, included by other library headers.
55 * Do not attempt to use it directly. @headername{iterator}
57 * This file implements reverse_iterator, back_insert_iterator,
58 * front_insert_iterator, insert_iterator, __normal_iterator, and their
59 * supporting functions and overloaded operators.
62 #ifndef _STL_ITERATOR_H
63 #define _STL_ITERATOR_H 1
65 #include <bits/cpp_type_traits.h>
66 #include <ext/type_traits.h>
67 #include <bits/move.h>
69 namespace std
_GLIBCXX_VISIBILITY(default)
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
74 * @addtogroup iterators
78 // 24.4.1 Reverse iterators
80 * Bidirectional and random access iterators have corresponding reverse
81 * %iterator adaptors that iterate through the data structure in the
82 * opposite direction. They have the same signatures as the corresponding
83 * iterators. The fundamental relation between a reverse %iterator and its
84 * corresponding %iterator @c i is established by the identity:
86 * &*(reverse_iterator(i)) == &*(i - 1)
89 * <em>This mapping is dictated by the fact that while there is always a
90 * pointer past the end of an array, there might not be a valid pointer
91 * before the beginning of an array.</em> [24.4.1]/1,2
93 * Reverse iterators can be tricky and surprising at first. Their
94 * semantics make sense, however, and the trickiness is a side effect of
95 * the requirement that the iterators must be safe.
97 template<typename _Iterator
>
98 class reverse_iterator
99 : public iterator
<typename iterator_traits
<_Iterator
>::iterator_category
,
100 typename iterator_traits
<_Iterator
>::value_type
,
101 typename iterator_traits
<_Iterator
>::difference_type
,
102 typename iterator_traits
<_Iterator
>::pointer
,
103 typename iterator_traits
<_Iterator
>::reference
>
108 typedef iterator_traits
<_Iterator
> __traits_type
;
111 typedef _Iterator iterator_type
;
112 typedef typename
__traits_type::difference_type difference_type
;
113 typedef typename
__traits_type::pointer pointer
;
114 typedef typename
__traits_type::reference reference
;
117 * The default constructor default-initializes member @p current.
118 * If it is a pointer, that means it is zero-initialized.
120 // _GLIBCXX_RESOLVE_LIB_DEFECTS
121 // 235 No specification of default ctor for reverse_iterator
122 reverse_iterator() : current() { }
125 * This %iterator will move in the opposite direction that @p x does.
128 reverse_iterator(iterator_type __x
) : current(__x
) { }
131 * The copy constructor is normal.
133 reverse_iterator(const reverse_iterator
& __x
)
134 : current(__x
.current
) { }
137 * A reverse_iterator across other types can be copied in the normal
140 template<typename _Iter
>
141 reverse_iterator(const reverse_iterator
<_Iter
>& __x
)
142 : current(__x
.base()) { }
145 * @return @c current, the %iterator used for underlying work.
159 _Iterator __tmp
= current
;
170 { return &(operator*()); }
192 reverse_iterator __tmp
= *this;
217 reverse_iterator __tmp
= *this;
228 operator+(difference_type __n
) const
229 { return reverse_iterator(current
- __n
); }
237 operator+=(difference_type __n
)
249 operator-(difference_type __n
) const
250 { return reverse_iterator(current
+ __n
); }
258 operator-=(difference_type __n
)
270 operator[](difference_type __n
) const
271 { return *(*this + __n
); }
276 * @param __x A %reverse_iterator.
277 * @param __y A %reverse_iterator.
278 * @return A simple bool.
280 * Reverse iterators forward many operations to their underlying base()
281 * iterators. Others are implemented in terms of one another.
284 template<typename _Iterator
>
286 operator==(const reverse_iterator
<_Iterator
>& __x
,
287 const reverse_iterator
<_Iterator
>& __y
)
288 { return __x
.base() == __y
.base(); }
290 template<typename _Iterator
>
292 operator<(const reverse_iterator
<_Iterator
>& __x
,
293 const reverse_iterator
<_Iterator
>& __y
)
294 { return __y
.base() < __x
.base(); }
296 template<typename _Iterator
>
298 operator!=(const reverse_iterator
<_Iterator
>& __x
,
299 const reverse_iterator
<_Iterator
>& __y
)
300 { return !(__x
== __y
); }
302 template<typename _Iterator
>
304 operator>(const reverse_iterator
<_Iterator
>& __x
,
305 const reverse_iterator
<_Iterator
>& __y
)
306 { return __y
< __x
; }
308 template<typename _Iterator
>
310 operator<=(const reverse_iterator
<_Iterator
>& __x
,
311 const reverse_iterator
<_Iterator
>& __y
)
312 { return !(__y
< __x
); }
314 template<typename _Iterator
>
316 operator>=(const reverse_iterator
<_Iterator
>& __x
,
317 const reverse_iterator
<_Iterator
>& __y
)
318 { return !(__x
< __y
); }
320 template<typename _Iterator
>
321 inline typename reverse_iterator
<_Iterator
>::difference_type
322 operator-(const reverse_iterator
<_Iterator
>& __x
,
323 const reverse_iterator
<_Iterator
>& __y
)
324 { return __y
.base() - __x
.base(); }
326 template<typename _Iterator
>
327 inline reverse_iterator
<_Iterator
>
328 operator+(typename reverse_iterator
<_Iterator
>::difference_type __n
,
329 const reverse_iterator
<_Iterator
>& __x
)
330 { return reverse_iterator
<_Iterator
>(__x
.base() - __n
); }
332 // _GLIBCXX_RESOLVE_LIB_DEFECTS
333 // DR 280. Comparison of reverse_iterator to const reverse_iterator.
334 template<typename _IteratorL
, typename _IteratorR
>
336 operator==(const reverse_iterator
<_IteratorL
>& __x
,
337 const reverse_iterator
<_IteratorR
>& __y
)
338 { return __x
.base() == __y
.base(); }
340 template<typename _IteratorL
, typename _IteratorR
>
342 operator<(const reverse_iterator
<_IteratorL
>& __x
,
343 const reverse_iterator
<_IteratorR
>& __y
)
344 { return __y
.base() < __x
.base(); }
346 template<typename _IteratorL
, typename _IteratorR
>
348 operator!=(const reverse_iterator
<_IteratorL
>& __x
,
349 const reverse_iterator
<_IteratorR
>& __y
)
350 { return !(__x
== __y
); }
352 template<typename _IteratorL
, typename _IteratorR
>
354 operator>(const reverse_iterator
<_IteratorL
>& __x
,
355 const reverse_iterator
<_IteratorR
>& __y
)
356 { return __y
< __x
; }
358 template<typename _IteratorL
, typename _IteratorR
>
360 operator<=(const reverse_iterator
<_IteratorL
>& __x
,
361 const reverse_iterator
<_IteratorR
>& __y
)
362 { return !(__y
< __x
); }
364 template<typename _IteratorL
, typename _IteratorR
>
366 operator>=(const reverse_iterator
<_IteratorL
>& __x
,
367 const reverse_iterator
<_IteratorR
>& __y
)
368 { return !(__x
< __y
); }
370 template<typename _IteratorL
, typename _IteratorR
>
371 #ifdef __GXX_EXPERIMENTAL_CXX0X__
374 operator-(const reverse_iterator
<_IteratorL
>& __x
,
375 const reverse_iterator
<_IteratorR
>& __y
)
376 -> decltype(__y
.base() - __x
.base())
378 inline typename reverse_iterator
<_IteratorL
>::difference_type
379 operator-(const reverse_iterator
<_IteratorL
>& __x
,
380 const reverse_iterator
<_IteratorR
>& __y
)
382 { return __y
.base() - __x
.base(); }
385 // 24.4.2.2.1 back_insert_iterator
387 * @brief Turns assignment into insertion.
389 * These are output iterators, constructed from a container-of-T.
390 * Assigning a T to the iterator appends it to the container using
393 * Tip: Using the back_inserter function to create these iterators can
396 template<typename _Container
>
397 class back_insert_iterator
398 : public iterator
<output_iterator_tag
, void, void, void, void>
401 _Container
* container
;
404 /// A nested typedef for the type of whatever container you used.
405 typedef _Container container_type
;
407 /// The only way to create this %iterator is with a container.
409 back_insert_iterator(_Container
& __x
) : container(&__x
) { }
412 * @param __value An instance of whatever type
413 * container_type::const_reference is; presumably a
414 * reference-to-const T for container<T>.
415 * @return This %iterator, for chained operations.
417 * This kind of %iterator doesn't really have a @a position in the
418 * container (you can think of the position as being permanently at
419 * the end, if you like). Assigning a value to the %iterator will
420 * always append the value to the end of the container.
422 #ifndef __GXX_EXPERIMENTAL_CXX0X__
423 back_insert_iterator
&
424 operator=(typename
_Container::const_reference __value
)
426 container
->push_back(__value
);
430 back_insert_iterator
&
431 operator=(const typename
_Container::value_type
& __value
)
433 container
->push_back(__value
);
437 back_insert_iterator
&
438 operator=(typename
_Container::value_type
&& __value
)
440 container
->push_back(std::move(__value
));
445 /// Simply returns *this.
446 back_insert_iterator
&
450 /// Simply returns *this. (This %iterator does not @a move.)
451 back_insert_iterator
&
455 /// Simply returns *this. (This %iterator does not @a move.)
462 * @param __x A container of arbitrary type.
463 * @return An instance of back_insert_iterator working on @p __x.
465 * This wrapper function helps in creating back_insert_iterator instances.
466 * Typing the name of the %iterator requires knowing the precise full
467 * type of the container, which can be tedious and impedes generic
468 * programming. Using this function lets you take advantage of automatic
469 * template parameter deduction, making the compiler match the correct
472 template<typename _Container
>
473 inline back_insert_iterator
<_Container
>
474 back_inserter(_Container
& __x
)
475 { return back_insert_iterator
<_Container
>(__x
); }
478 * @brief Turns assignment into insertion.
480 * These are output iterators, constructed from a container-of-T.
481 * Assigning a T to the iterator prepends it to the container using
484 * Tip: Using the front_inserter function to create these iterators can
487 template<typename _Container
>
488 class front_insert_iterator
489 : public iterator
<output_iterator_tag
, void, void, void, void>
492 _Container
* container
;
495 /// A nested typedef for the type of whatever container you used.
496 typedef _Container container_type
;
498 /// The only way to create this %iterator is with a container.
499 explicit front_insert_iterator(_Container
& __x
) : container(&__x
) { }
502 * @param __value An instance of whatever type
503 * container_type::const_reference is; presumably a
504 * reference-to-const T for container<T>.
505 * @return This %iterator, for chained operations.
507 * This kind of %iterator doesn't really have a @a position in the
508 * container (you can think of the position as being permanently at
509 * the front, if you like). Assigning a value to the %iterator will
510 * always prepend the value to the front of the container.
512 #ifndef __GXX_EXPERIMENTAL_CXX0X__
513 front_insert_iterator
&
514 operator=(typename
_Container::const_reference __value
)
516 container
->push_front(__value
);
520 front_insert_iterator
&
521 operator=(const typename
_Container::value_type
& __value
)
523 container
->push_front(__value
);
527 front_insert_iterator
&
528 operator=(typename
_Container::value_type
&& __value
)
530 container
->push_front(std::move(__value
));
535 /// Simply returns *this.
536 front_insert_iterator
&
540 /// Simply returns *this. (This %iterator does not @a move.)
541 front_insert_iterator
&
545 /// Simply returns *this. (This %iterator does not @a move.)
546 front_insert_iterator
552 * @param __x A container of arbitrary type.
553 * @return An instance of front_insert_iterator working on @p x.
555 * This wrapper function helps in creating front_insert_iterator instances.
556 * Typing the name of the %iterator requires knowing the precise full
557 * type of the container, which can be tedious and impedes generic
558 * programming. Using this function lets you take advantage of automatic
559 * template parameter deduction, making the compiler match the correct
562 template<typename _Container
>
563 inline front_insert_iterator
<_Container
>
564 front_inserter(_Container
& __x
)
565 { return front_insert_iterator
<_Container
>(__x
); }
568 * @brief Turns assignment into insertion.
570 * These are output iterators, constructed from a container-of-T.
571 * Assigning a T to the iterator inserts it in the container at the
572 * %iterator's position, rather than overwriting the value at that
575 * (Sequences will actually insert a @e copy of the value before the
576 * %iterator's position.)
578 * Tip: Using the inserter function to create these iterators can
581 template<typename _Container
>
582 class insert_iterator
583 : public iterator
<output_iterator_tag
, void, void, void, void>
586 _Container
* container
;
587 typename
_Container::iterator iter
;
590 /// A nested typedef for the type of whatever container you used.
591 typedef _Container container_type
;
594 * The only way to create this %iterator is with a container and an
595 * initial position (a normal %iterator into the container).
597 insert_iterator(_Container
& __x
, typename
_Container::iterator __i
)
598 : container(&__x
), iter(__i
) {}
601 * @param __value An instance of whatever type
602 * container_type::const_reference is; presumably a
603 * reference-to-const T for container<T>.
604 * @return This %iterator, for chained operations.
606 * This kind of %iterator maintains its own position in the
607 * container. Assigning a value to the %iterator will insert the
608 * value into the container at the place before the %iterator.
610 * The position is maintained such that subsequent assignments will
611 * insert values immediately after one another. For example,
613 * // vector v contains A and Z
615 * insert_iterator i (v, ++v.begin());
620 * // vector v contains A, 1, 2, 3, and Z
623 #ifndef __GXX_EXPERIMENTAL_CXX0X__
625 operator=(typename
_Container::const_reference __value
)
627 iter
= container
->insert(iter
, __value
);
633 operator=(const typename
_Container::value_type
& __value
)
635 iter
= container
->insert(iter
, __value
);
641 operator=(typename
_Container::value_type
&& __value
)
643 iter
= container
->insert(iter
, std::move(__value
));
649 /// Simply returns *this.
654 /// Simply returns *this. (This %iterator does not @a move.)
659 /// Simply returns *this. (This %iterator does not @a move.)
666 * @param __x A container of arbitrary type.
667 * @return An instance of insert_iterator working on @p __x.
669 * This wrapper function helps in creating insert_iterator instances.
670 * Typing the name of the %iterator requires knowing the precise full
671 * type of the container, which can be tedious and impedes generic
672 * programming. Using this function lets you take advantage of automatic
673 * template parameter deduction, making the compiler match the correct
676 template<typename _Container
, typename _Iterator
>
677 inline insert_iterator
<_Container
>
678 inserter(_Container
& __x
, _Iterator __i
)
680 return insert_iterator
<_Container
>(__x
,
681 typename
_Container::iterator(__i
));
684 // @} group iterators
686 _GLIBCXX_END_NAMESPACE_VERSION
689 namespace __gnu_cxx
_GLIBCXX_VISIBILITY(default)
691 _GLIBCXX_BEGIN_NAMESPACE_VERSION
693 // This iterator adapter is @a normal in the sense that it does not
694 // change the semantics of any of the operators of its iterator
695 // parameter. Its primary purpose is to convert an iterator that is
696 // not a class, e.g. a pointer, into an iterator that is a class.
697 // The _Container parameter exists solely so that different containers
698 // using this template can instantiate different types, even if the
699 // _Iterator parameter is the same.
700 using std::iterator_traits
;
702 template<typename _Iterator
, typename _Container
>
703 class __normal_iterator
706 _Iterator _M_current
;
708 typedef iterator_traits
<_Iterator
> __traits_type
;
711 typedef _Iterator iterator_type
;
712 typedef typename
__traits_type::iterator_category iterator_category
;
713 typedef typename
__traits_type::value_type value_type
;
714 typedef typename
__traits_type::difference_type difference_type
;
715 typedef typename
__traits_type::reference reference
;
716 typedef typename
__traits_type::pointer pointer
;
718 _GLIBCXX_CONSTEXPR
__normal_iterator() : _M_current(_Iterator()) { }
721 __normal_iterator(const _Iterator
& __i
) : _M_current(__i
) { }
723 // Allow iterator to const_iterator conversion
724 template<typename _Iter
>
725 __normal_iterator(const __normal_iterator
<_Iter
,
726 typename __enable_if
<
727 (std::__are_same
<_Iter
, typename
_Container::pointer
>::__value
),
728 _Container
>::__type
>& __i
)
729 : _M_current(__i
.base()) { }
731 // Forward iterator requirements
734 { return *_M_current
; }
738 { return _M_current
; }
749 { return __normal_iterator(_M_current
++); }
751 // Bidirectional iterator requirements
761 { return __normal_iterator(_M_current
--); }
763 // Random access iterator requirements
765 operator[](const difference_type
& __n
) const
766 { return _M_current
[__n
]; }
769 operator+=(const difference_type
& __n
)
770 { _M_current
+= __n
; return *this; }
773 operator+(const difference_type
& __n
) const
774 { return __normal_iterator(_M_current
+ __n
); }
777 operator-=(const difference_type
& __n
)
778 { _M_current
-= __n
; return *this; }
781 operator-(const difference_type
& __n
) const
782 { return __normal_iterator(_M_current
- __n
); }
786 { return _M_current
; }
789 // Note: In what follows, the left- and right-hand-side iterators are
790 // allowed to vary in types (conceptually in cv-qualification) so that
791 // comparison between cv-qualified and non-cv-qualified iterators be
792 // valid. However, the greedy and unfriendly operators in std::rel_ops
793 // will make overload resolution ambiguous (when in scope) if we don't
794 // provide overloads whose operands are of the same type. Can someone
795 // remind me what generic programming is about? -- Gaby
797 // Forward iterator requirements
798 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
800 operator==(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
801 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
802 { return __lhs
.base() == __rhs
.base(); }
804 template<typename _Iterator
, typename _Container
>
806 operator==(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
807 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
808 { return __lhs
.base() == __rhs
.base(); }
810 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
812 operator!=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
813 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
814 { return __lhs
.base() != __rhs
.base(); }
816 template<typename _Iterator
, typename _Container
>
818 operator!=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
819 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
820 { return __lhs
.base() != __rhs
.base(); }
822 // Random access iterator requirements
823 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
825 operator<(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
826 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
827 { return __lhs
.base() < __rhs
.base(); }
829 template<typename _Iterator
, typename _Container
>
831 operator<(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
832 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
833 { return __lhs
.base() < __rhs
.base(); }
835 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
837 operator>(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
838 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
839 { return __lhs
.base() > __rhs
.base(); }
841 template<typename _Iterator
, typename _Container
>
843 operator>(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
844 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
845 { return __lhs
.base() > __rhs
.base(); }
847 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
849 operator<=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
850 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
851 { return __lhs
.base() <= __rhs
.base(); }
853 template<typename _Iterator
, typename _Container
>
855 operator<=(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
856 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
857 { return __lhs
.base() <= __rhs
.base(); }
859 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
861 operator>=(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
862 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
)
869 { return __lhs
.base() >= __rhs
.base(); }
871 // _GLIBCXX_RESOLVE_LIB_DEFECTS
872 // According to the resolution of DR179 not only the various comparison
873 // operators but also operator- must accept mixed iterator/const_iterator
875 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
876 #ifdef __GXX_EXPERIMENTAL_CXX0X__
879 operator-(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
880 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
881 -> decltype(__lhs
.base() - __rhs
.base())
883 inline typename __normal_iterator
<_IteratorL
, _Container
>::difference_type
884 operator-(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
885 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
887 { return __lhs
.base() - __rhs
.base(); }
889 template<typename _Iterator
, typename _Container
>
890 inline typename __normal_iterator
<_Iterator
, _Container
>::difference_type
891 operator-(const __normal_iterator
<_Iterator
, _Container
>& __lhs
,
892 const __normal_iterator
<_Iterator
, _Container
>& __rhs
)
893 { return __lhs
.base() - __rhs
.base(); }
895 template<typename _Iterator
, typename _Container
>
896 inline __normal_iterator
<_Iterator
, _Container
>
897 operator+(typename __normal_iterator
<_Iterator
, _Container
>::difference_type
898 __n
, const __normal_iterator
<_Iterator
, _Container
>& __i
)
899 { return __normal_iterator
<_Iterator
, _Container
>(__i
.base() + __n
); }
901 _GLIBCXX_END_NAMESPACE_VERSION
904 #ifdef __GXX_EXPERIMENTAL_CXX0X__
906 namespace std
_GLIBCXX_VISIBILITY(default)
908 _GLIBCXX_BEGIN_NAMESPACE_VERSION
911 * @addtogroup iterators
915 // 24.4.3 Move iterators
917 * Class template move_iterator is an iterator adapter with the same
918 * behavior as the underlying iterator except that its dereference
919 * operator implicitly converts the value returned by the underlying
920 * iterator's dereference operator to an rvalue reference. Some
921 * generic algorithms can be called with move iterators to replace
922 * copying with moving.
924 template<typename _Iterator
>
928 _Iterator _M_current
;
930 typedef iterator_traits
<_Iterator
> __traits_type
;
933 typedef _Iterator iterator_type
;
934 typedef typename
__traits_type::iterator_category iterator_category
;
935 typedef typename
__traits_type::value_type value_type
;
936 typedef typename
__traits_type::difference_type difference_type
;
938 typedef _Iterator pointer
;
939 typedef value_type
&& reference
;
945 move_iterator(iterator_type __i
)
946 : _M_current(__i
) { }
948 template<typename _Iter
>
949 move_iterator(const move_iterator
<_Iter
>& __i
)
950 : _M_current(__i
.base()) { }
954 { return _M_current
; }
958 { return std::move(*_M_current
); }
962 { return _M_current
; }
974 move_iterator __tmp
= *this;
989 move_iterator __tmp
= *this;
995 operator+(difference_type __n
) const
996 { return move_iterator(_M_current
+ __n
); }
999 operator+=(difference_type __n
)
1006 operator-(difference_type __n
) const
1007 { return move_iterator(_M_current
- __n
); }
1010 operator-=(difference_type __n
)
1017 operator[](difference_type __n
) const
1018 { return std::move(_M_current
[__n
]); }
1021 // Note: See __normal_iterator operators note from Gaby to understand
1022 // why there are always 2 versions for most of the move_iterator
1024 template<typename _IteratorL
, typename _IteratorR
>
1026 operator==(const move_iterator
<_IteratorL
>& __x
,
1027 const move_iterator
<_IteratorR
>& __y
)
1028 { return __x
.base() == __y
.base(); }
1030 template<typename _Iterator
>
1032 operator==(const move_iterator
<_Iterator
>& __x
,
1033 const move_iterator
<_Iterator
>& __y
)
1034 { return __x
.base() == __y
.base(); }
1036 template<typename _IteratorL
, typename _IteratorR
>
1038 operator!=(const move_iterator
<_IteratorL
>& __x
,
1039 const move_iterator
<_IteratorR
>& __y
)
1040 { return !(__x
== __y
); }
1042 template<typename _Iterator
>
1044 operator!=(const move_iterator
<_Iterator
>& __x
,
1045 const move_iterator
<_Iterator
>& __y
)
1046 { return !(__x
== __y
); }
1048 template<typename _IteratorL
, typename _IteratorR
>
1050 operator<(const move_iterator
<_IteratorL
>& __x
,
1051 const move_iterator
<_IteratorR
>& __y
)
1052 { return __x
.base() < __y
.base(); }
1054 template<typename _Iterator
>
1056 operator<(const move_iterator
<_Iterator
>& __x
,
1057 const move_iterator
<_Iterator
>& __y
)
1058 { return __x
.base() < __y
.base(); }
1060 template<typename _IteratorL
, typename _IteratorR
>
1062 operator<=(const move_iterator
<_IteratorL
>& __x
,
1063 const move_iterator
<_IteratorR
>& __y
)
1064 { return !(__y
< __x
); }
1066 template<typename _Iterator
>
1068 operator<=(const move_iterator
<_Iterator
>& __x
,
1069 const move_iterator
<_Iterator
>& __y
)
1070 { return !(__y
< __x
); }
1072 template<typename _IteratorL
, typename _IteratorR
>
1074 operator>(const move_iterator
<_IteratorL
>& __x
,
1075 const move_iterator
<_IteratorR
>& __y
)
1076 { return __y
< __x
; }
1078 template<typename _Iterator
>
1080 operator>(const move_iterator
<_Iterator
>& __x
,
1081 const move_iterator
<_Iterator
>& __y
)
1082 { return __y
< __x
; }
1084 template<typename _IteratorL
, typename _IteratorR
>
1086 operator>=(const move_iterator
<_IteratorL
>& __x
,
1087 const move_iterator
<_IteratorR
>& __y
)
1088 { return !(__x
< __y
); }
1090 template<typename _Iterator
>
1092 operator>=(const move_iterator
<_Iterator
>& __x
,
1093 const move_iterator
<_Iterator
>& __y
)
1094 { return !(__x
< __y
); }
1097 template<typename _IteratorL
, typename _IteratorR
>
1099 operator-(const move_iterator
<_IteratorL
>& __x
,
1100 const move_iterator
<_IteratorR
>& __y
)
1101 -> decltype(__x
.base() - __y
.base())
1102 { return __x
.base() - __y
.base(); }
1104 template<typename _Iterator
>
1106 operator-(const move_iterator
<_Iterator
>& __x
,
1107 const move_iterator
<_Iterator
>& __y
)
1108 -> decltype(__x
.base() - __y
.base())
1109 { return __x
.base() - __y
.base(); }
1111 template<typename _Iterator
>
1112 inline move_iterator
<_Iterator
>
1113 operator+(typename move_iterator
<_Iterator
>::difference_type __n
,
1114 const move_iterator
<_Iterator
>& __x
)
1115 { return __x
+ __n
; }
1117 template<typename _Iterator
>
1118 inline move_iterator
<_Iterator
>
1119 make_move_iterator(_Iterator __i
)
1120 { return move_iterator
<_Iterator
>(__i
); }
1122 template<typename _Iterator
, typename _ReturnType
1123 = typename conditional
<__move_if_noexcept_cond
1124 <typename iterator_traits
<_Iterator
>::value_type
>::value
,
1125 _Iterator
, move_iterator
<_Iterator
>>::type
>
1127 __make_move_if_noexcept_iterator(_Iterator __i
)
1128 { return _ReturnType(__i
); }
1130 // @} group iterators
1132 _GLIBCXX_END_NAMESPACE_VERSION
1135 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter)
1136 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \
1137 std::__make_move_if_noexcept_iterator(_Iter)
1139 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
1140 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter)
1141 #endif // __GXX_EXPERIMENTAL_CXX0X__