1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001-2013 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>
67 namespace std _GLIBCXX_VISIBILITY(default)
69 _GLIBCXX_BEGIN_NAMESPACE_VERSION
72 * @addtogroup iterators
76 // 24.4.1 Reverse iterators
78 * Bidirectional and random access iterators have corresponding reverse
79 * %iterator adaptors that iterate through the data structure in the
80 * opposite direction. They have the same signatures as the corresponding
81 * iterators. The fundamental relation between a reverse %iterator and its
82 * corresponding %iterator @c i is established by the identity:
84 * &*(reverse_iterator(i)) == &*(i - 1)
87 * <em>This mapping is dictated by the fact that while there is always a
88 * pointer past the end of an array, there might not be a valid pointer
89 * before the beginning of an array.</em> [24.4.1]/1,2
91 * Reverse iterators can be tricky and surprising at first. Their
92 * semantics make sense, however, and the trickiness is a side effect of
93 * the requirement that the iterators must be safe.
95 template<typename _Iterator>
96 class reverse_iterator
97 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
98 typename iterator_traits<_Iterator>::value_type,
99 typename iterator_traits<_Iterator>::difference_type,
100 typename iterator_traits<_Iterator>::pointer,
101 typename iterator_traits<_Iterator>::reference>
106 typedef iterator_traits<_Iterator> __traits_type;
109 typedef _Iterator iterator_type;
110 typedef typename __traits_type::difference_type difference_type;
111 typedef typename __traits_type::pointer pointer;
112 typedef typename __traits_type::reference reference;
115 * The default constructor value-initializes member @p current.
116 * If it is a pointer, that means it is zero-initialized.
118 // _GLIBCXX_RESOLVE_LIB_DEFECTS
119 // 235 No specification of default ctor for reverse_iterator
120 reverse_iterator() : current() { }
123 * This %iterator will move in the opposite direction that @p x does.
126 reverse_iterator(iterator_type __x) : current(__x) { }
129 * The copy constructor is normal.
131 reverse_iterator(const reverse_iterator& __x)
132 : current(__x.current) { }
135 * A %reverse_iterator across other types can be copied if the
136 * underlying %iterator can be converted to the type of @c current.
138 template<typename _Iter>
139 reverse_iterator(const reverse_iterator<_Iter>& __x)
140 : current(__x.base()) { }
143 * @return @c current, the %iterator used for underlying work.
150 * @return A reference to the value at @c --current
152 * This requires that @c --current is dereferenceable.
154 * @warning This implementation requires that for an iterator of the
155 * underlying iterator type, @c x, a reference obtained by
156 * @c *x remains valid after @c x has been modified or
157 * destroyed. This is a bug: http://gcc.gnu.org/PR51823
162 _Iterator __tmp = current;
167 * @return A pointer to the value at @c --current
169 * This requires that @c --current is dereferenceable.
173 { return &(operator*()); }
178 * Decrements the underlying iterator.
188 * @return The original value of @c *this
190 * Decrements the underlying iterator.
195 reverse_iterator __tmp = *this;
203 * Increments the underlying iterator.
213 * @return A reverse_iterator with the previous value of @c *this
215 * Increments the underlying iterator.
220 reverse_iterator __tmp = *this;
226 * @return A reverse_iterator that refers to @c current - @a __n
228 * The underlying iterator must be a Random Access Iterator.
231 operator+(difference_type __n) const
232 { return reverse_iterator(current - __n); }
237 * Moves the underlying iterator backwards @a __n steps.
238 * The underlying iterator must be a Random Access Iterator.
241 operator+=(difference_type __n)
248 * @return A reverse_iterator that refers to @c current - @a __n
250 * The underlying iterator must be a Random Access Iterator.
253 operator-(difference_type __n) const
254 { return reverse_iterator(current + __n); }
259 * Moves the underlying iterator forwards @a __n steps.
260 * The underlying iterator must be a Random Access Iterator.
263 operator-=(difference_type __n)
270 * @return The value at @c current - @a __n - 1
272 * The underlying iterator must be a Random Access Iterator.
275 operator[](difference_type __n) const
276 { return *(*this + __n); }
281 * @param __x A %reverse_iterator.
282 * @param __y A %reverse_iterator.
283 * @return A simple bool.
285 * Reverse iterators forward many operations to their underlying base()
286 * iterators. Others are implemented in terms of one another.
289 template<typename _Iterator>
291 operator==(const reverse_iterator<_Iterator>& __x,
292 const reverse_iterator<_Iterator>& __y)
293 { return __x.base() == __y.base(); }
295 template<typename _Iterator>
297 operator<(const reverse_iterator<_Iterator>& __x,
298 const reverse_iterator<_Iterator>& __y)
299 { return __y.base() < __x.base(); }
301 template<typename _Iterator>
303 operator!=(const reverse_iterator<_Iterator>& __x,
304 const reverse_iterator<_Iterator>& __y)
305 { return !(__x == __y); }
307 template<typename _Iterator>
309 operator>(const reverse_iterator<_Iterator>& __x,
310 const reverse_iterator<_Iterator>& __y)
311 { return __y < __x; }
313 template<typename _Iterator>
315 operator<=(const reverse_iterator<_Iterator>& __x,
316 const reverse_iterator<_Iterator>& __y)
317 { return !(__y < __x); }
319 template<typename _Iterator>
321 operator>=(const reverse_iterator<_Iterator>& __x,
322 const reverse_iterator<_Iterator>& __y)
323 { return !(__x < __y); }
325 template<typename _Iterator>
326 inline typename reverse_iterator<_Iterator>::difference_type
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 reverse_iterator<_Iterator>
333 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
334 const reverse_iterator<_Iterator>& __x)
335 { return reverse_iterator<_Iterator>(__x.base() - __n); }
337 // _GLIBCXX_RESOLVE_LIB_DEFECTS
338 // DR 280. Comparison of reverse_iterator to const reverse_iterator.
339 template<typename _IteratorL, typename _IteratorR>
341 operator==(const reverse_iterator<_IteratorL>& __x,
342 const reverse_iterator<_IteratorR>& __y)
343 { return __x.base() == __y.base(); }
345 template<typename _IteratorL, typename _IteratorR>
347 operator<(const reverse_iterator<_IteratorL>& __x,
348 const reverse_iterator<_IteratorR>& __y)
349 { return __y.base() < __x.base(); }
351 template<typename _IteratorL, typename _IteratorR>
353 operator!=(const reverse_iterator<_IteratorL>& __x,
354 const reverse_iterator<_IteratorR>& __y)
355 { return !(__x == __y); }
357 template<typename _IteratorL, typename _IteratorR>
359 operator>(const reverse_iterator<_IteratorL>& __x,
360 const reverse_iterator<_IteratorR>& __y)
361 { return __y < __x; }
363 template<typename _IteratorL, typename _IteratorR>
365 operator<=(const reverse_iterator<_IteratorL>& __x,
366 const reverse_iterator<_IteratorR>& __y)
367 { return !(__y < __x); }
369 template<typename _IteratorL, typename _IteratorR>
371 operator>=(const reverse_iterator<_IteratorL>& __x,
372 const reverse_iterator<_IteratorR>& __y)
373 { return !(__x < __y); }
375 template<typename _IteratorL, typename _IteratorR>
376 #if __cplusplus >= 201103L
379 operator-(const reverse_iterator<_IteratorL>& __x,
380 const reverse_iterator<_IteratorR>& __y)
381 -> decltype(__y.base() - __x.base())
383 inline typename reverse_iterator<_IteratorL>::difference_type
384 operator-(const reverse_iterator<_IteratorL>& __x,
385 const reverse_iterator<_IteratorR>& __y)
387 { return __y.base() - __x.base(); }
390 // 24.4.2.2.1 back_insert_iterator
392 * @brief Turns assignment into insertion.
394 * These are output iterators, constructed from a container-of-T.
395 * Assigning a T to the iterator appends it to the container using
398 * Tip: Using the back_inserter function to create these iterators can
401 template<typename _Container>
402 class back_insert_iterator
403 : public iterator<output_iterator_tag, void, void, void, void>
406 _Container* container;
409 /// A nested typedef for the type of whatever container you used.
410 typedef _Container container_type;
412 /// The only way to create this %iterator is with a container.
414 back_insert_iterator(_Container& __x) : container(&__x) { }
417 * @param __value An instance of whatever type
418 * container_type::const_reference is; presumably a
419 * reference-to-const T for container<T>.
420 * @return This %iterator, for chained operations.
422 * This kind of %iterator doesn't really have a @a position in the
423 * container (you can think of the position as being permanently at
424 * the end, if you like). Assigning a value to the %iterator will
425 * always append the value to the end of the container.
427 #if __cplusplus < 201103L
428 back_insert_iterator&
429 operator=(typename _Container::const_reference __value)
431 container->push_back(__value);
435 back_insert_iterator&
436 operator=(const typename _Container::value_type& __value)
438 container->push_back(__value);
442 back_insert_iterator&
443 operator=(typename _Container::value_type&& __value)
445 container->push_back(std::move(__value));
450 /// Simply returns *this.
451 back_insert_iterator&
455 /// Simply returns *this. (This %iterator does not @a move.)
456 back_insert_iterator&
460 /// Simply returns *this. (This %iterator does not @a move.)
467 * @param __x A container of arbitrary type.
468 * @return An instance of back_insert_iterator working on @p __x.
470 * This wrapper function helps in creating back_insert_iterator instances.
471 * Typing the name of the %iterator requires knowing the precise full
472 * type of the container, which can be tedious and impedes generic
473 * programming. Using this function lets you take advantage of automatic
474 * template parameter deduction, making the compiler match the correct
477 template<typename _Container>
478 inline back_insert_iterator<_Container>
479 back_inserter(_Container& __x)
480 { return back_insert_iterator<_Container>(__x); }
483 * @brief Turns assignment into insertion.
485 * These are output iterators, constructed from a container-of-T.
486 * Assigning a T to the iterator prepends it to the container using
489 * Tip: Using the front_inserter function to create these iterators can
492 template<typename _Container>
493 class front_insert_iterator
494 : public iterator<output_iterator_tag, void, void, void, void>
497 _Container* container;
500 /// A nested typedef for the type of whatever container you used.
501 typedef _Container container_type;
503 /// The only way to create this %iterator is with a container.
504 explicit front_insert_iterator(_Container& __x) : container(&__x) { }
507 * @param __value An instance of whatever type
508 * container_type::const_reference is; presumably a
509 * reference-to-const T for container<T>.
510 * @return This %iterator, for chained operations.
512 * This kind of %iterator doesn't really have a @a position in the
513 * container (you can think of the position as being permanently at
514 * the front, if you like). Assigning a value to the %iterator will
515 * always prepend the value to the front of the container.
517 #if __cplusplus < 201103L
518 front_insert_iterator&
519 operator=(typename _Container::const_reference __value)
521 container->push_front(__value);
525 front_insert_iterator&
526 operator=(const typename _Container::value_type& __value)
528 container->push_front(__value);
532 front_insert_iterator&
533 operator=(typename _Container::value_type&& __value)
535 container->push_front(std::move(__value));
540 /// Simply returns *this.
541 front_insert_iterator&
545 /// Simply returns *this. (This %iterator does not @a move.)
546 front_insert_iterator&
550 /// Simply returns *this. (This %iterator does not @a move.)
551 front_insert_iterator
557 * @param __x A container of arbitrary type.
558 * @return An instance of front_insert_iterator working on @p x.
560 * This wrapper function helps in creating front_insert_iterator instances.
561 * Typing the name of the %iterator requires knowing the precise full
562 * type of the container, which can be tedious and impedes generic
563 * programming. Using this function lets you take advantage of automatic
564 * template parameter deduction, making the compiler match the correct
567 template<typename _Container>
568 inline front_insert_iterator<_Container>
569 front_inserter(_Container& __x)
570 { return front_insert_iterator<_Container>(__x); }
573 * @brief Turns assignment into insertion.
575 * These are output iterators, constructed from a container-of-T.
576 * Assigning a T to the iterator inserts it in the container at the
577 * %iterator's position, rather than overwriting the value at that
580 * (Sequences will actually insert a @e copy of the value before the
581 * %iterator's position.)
583 * Tip: Using the inserter function to create these iterators can
586 template<typename _Container>
587 class insert_iterator
588 : public iterator<output_iterator_tag, void, void, void, void>
591 _Container* container;
592 typename _Container::iterator iter;
595 /// A nested typedef for the type of whatever container you used.
596 typedef _Container container_type;
599 * The only way to create this %iterator is with a container and an
600 * initial position (a normal %iterator into the container).
602 insert_iterator(_Container& __x, typename _Container::iterator __i)
603 : container(&__x), iter(__i) {}
606 * @param __value An instance of whatever type
607 * container_type::const_reference is; presumably a
608 * reference-to-const T for container<T>.
609 * @return This %iterator, for chained operations.
611 * This kind of %iterator maintains its own position in the
612 * container. Assigning a value to the %iterator will insert the
613 * value into the container at the place before the %iterator.
615 * The position is maintained such that subsequent assignments will
616 * insert values immediately after one another. For example,
618 * // vector v contains A and Z
620 * insert_iterator i (v, ++v.begin());
625 * // vector v contains A, 1, 2, 3, and Z
628 #if __cplusplus < 201103L
630 operator=(typename _Container::const_reference __value)
632 iter = container->insert(iter, __value);
638 operator=(const typename _Container::value_type& __value)
640 iter = container->insert(iter, __value);
646 operator=(typename _Container::value_type&& __value)
648 iter = container->insert(iter, std::move(__value));
654 /// Simply returns *this.
659 /// Simply returns *this. (This %iterator does not @a move.)
664 /// Simply returns *this. (This %iterator does not @a move.)
671 * @param __x A container of arbitrary type.
672 * @return An instance of insert_iterator working on @p __x.
674 * This wrapper function helps in creating insert_iterator instances.
675 * Typing the name of the %iterator requires knowing the precise full
676 * type of the container, which can be tedious and impedes generic
677 * programming. Using this function lets you take advantage of automatic
678 * template parameter deduction, making the compiler match the correct
681 template<typename _Container, typename _Iterator>
682 inline insert_iterator<_Container>
683 inserter(_Container& __x, _Iterator __i)
685 return insert_iterator<_Container>(__x,
686 typename _Container::iterator(__i));
689 // @} group iterators
691 _GLIBCXX_END_NAMESPACE_VERSION
694 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
696 _GLIBCXX_BEGIN_NAMESPACE_VERSION
698 // This iterator adapter is @a normal in the sense that it does not
699 // change the semantics of any of the operators of its iterator
700 // parameter. Its primary purpose is to convert an iterator that is
701 // not a class, e.g. a pointer, into an iterator that is a class.
702 // The _Container parameter exists solely so that different containers
703 // using this template can instantiate different types, even if the
704 // _Iterator parameter is the same.
705 using std::iterator_traits;
707 template<typename _Iterator, typename _Container>
708 class __normal_iterator
711 _Iterator _M_current;
713 typedef iterator_traits<_Iterator> __traits_type;
716 typedef _Iterator iterator_type;
717 typedef typename __traits_type::iterator_category iterator_category;
718 typedef typename __traits_type::value_type value_type;
719 typedef typename __traits_type::difference_type difference_type;
720 typedef typename __traits_type::reference reference;
721 typedef typename __traits_type::pointer pointer;
723 _GLIBCXX_CONSTEXPR __normal_iterator() : _M_current(_Iterator()) { }
726 __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
728 // Allow iterator to const_iterator conversion
729 template<typename _Iter>
730 __normal_iterator(const __normal_iterator<_Iter,
731 typename __enable_if<
732 (std::__are_same<_Iter, typename _Container::pointer>::__value),
733 _Container>::__type>& __i)
734 : _M_current(__i.base()) { }
736 // Forward iterator requirements
739 { return *_M_current; }
743 { return _M_current; }
754 { return __normal_iterator(_M_current++); }
756 // Bidirectional iterator requirements
766 { return __normal_iterator(_M_current--); }
768 // Random access iterator requirements
770 operator[](const difference_type& __n) const
771 { return _M_current[__n]; }
774 operator+=(const difference_type& __n)
775 { _M_current += __n; return *this; }
778 operator+(const difference_type& __n) const
779 { return __normal_iterator(_M_current + __n); }
782 operator-=(const difference_type& __n)
783 { _M_current -= __n; return *this; }
786 operator-(const difference_type& __n) const
787 { return __normal_iterator(_M_current - __n); }
791 { return _M_current; }
794 // Note: In what follows, the left- and right-hand-side iterators are
795 // allowed to vary in types (conceptually in cv-qualification) so that
796 // comparison between cv-qualified and non-cv-qualified iterators be
797 // valid. However, the greedy and unfriendly operators in std::rel_ops
798 // will make overload resolution ambiguous (when in scope) if we don't
799 // provide overloads whose operands are of the same type. Can someone
800 // remind me what generic programming is about? -- Gaby
802 // Forward iterator requirements
803 template<typename _IteratorL, typename _IteratorR, typename _Container>
805 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
806 const __normal_iterator<_IteratorR, _Container>& __rhs)
807 { return __lhs.base() == __rhs.base(); }
809 template<typename _Iterator, typename _Container>
811 operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
812 const __normal_iterator<_Iterator, _Container>& __rhs)
813 { return __lhs.base() == __rhs.base(); }
815 template<typename _IteratorL, typename _IteratorR, typename _Container>
817 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
818 const __normal_iterator<_IteratorR, _Container>& __rhs)
819 { return __lhs.base() != __rhs.base(); }
821 template<typename _Iterator, typename _Container>
823 operator!=(const __normal_iterator<_Iterator, _Container>& __lhs,
824 const __normal_iterator<_Iterator, _Container>& __rhs)
825 { return __lhs.base() != __rhs.base(); }
827 // Random access iterator requirements
828 template<typename _IteratorL, typename _IteratorR, typename _Container>
830 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
831 const __normal_iterator<_IteratorR, _Container>& __rhs)
832 { return __lhs.base() < __rhs.base(); }
834 template<typename _Iterator, typename _Container>
836 operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
837 const __normal_iterator<_Iterator, _Container>& __rhs)
838 { return __lhs.base() < __rhs.base(); }
840 template<typename _IteratorL, typename _IteratorR, typename _Container>
842 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
843 const __normal_iterator<_IteratorR, _Container>& __rhs)
844 { return __lhs.base() > __rhs.base(); }
846 template<typename _Iterator, typename _Container>
848 operator>(const __normal_iterator<_Iterator, _Container>& __lhs,
849 const __normal_iterator<_Iterator, _Container>& __rhs)
850 { return __lhs.base() > __rhs.base(); }
852 template<typename _IteratorL, typename _IteratorR, typename _Container>
854 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
855 const __normal_iterator<_IteratorR, _Container>& __rhs)
856 { return __lhs.base() <= __rhs.base(); }
858 template<typename _Iterator, typename _Container>
860 operator<=(const __normal_iterator<_Iterator, _Container>& __lhs,
861 const __normal_iterator<_Iterator, _Container>& __rhs)
862 { return __lhs.base() <= __rhs.base(); }
864 template<typename _IteratorL, typename _IteratorR, typename _Container>
866 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
867 const __normal_iterator<_IteratorR, _Container>& __rhs)
868 { return __lhs.base() >= __rhs.base(); }
870 template<typename _Iterator, typename _Container>
872 operator>=(const __normal_iterator<_Iterator, _Container>& __lhs,
873 const __normal_iterator<_Iterator, _Container>& __rhs)
874 { return __lhs.base() >= __rhs.base(); }
876 // _GLIBCXX_RESOLVE_LIB_DEFECTS
877 // According to the resolution of DR179 not only the various comparison
878 // operators but also operator- must accept mixed iterator/const_iterator
880 template<typename _IteratorL, typename _IteratorR, typename _Container>
881 #if __cplusplus >= 201103L
884 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
885 const __normal_iterator<_IteratorR, _Container>& __rhs)
886 -> decltype(__lhs.base() - __rhs.base())
888 inline typename __normal_iterator<_IteratorL, _Container>::difference_type
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>
895 inline typename __normal_iterator<_Iterator, _Container>::difference_type
896 operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
897 const __normal_iterator<_Iterator, _Container>& __rhs)
898 { return __lhs.base() - __rhs.base(); }
900 template<typename _Iterator, typename _Container>
901 inline __normal_iterator<_Iterator, _Container>
902 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
903 __n, const __normal_iterator<_Iterator, _Container>& __i)
904 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
906 _GLIBCXX_END_NAMESPACE_VERSION
909 #if __cplusplus >= 201103L
911 namespace std _GLIBCXX_VISIBILITY(default)
913 _GLIBCXX_BEGIN_NAMESPACE_VERSION
916 * @addtogroup iterators
920 // 24.4.3 Move iterators
922 * Class template move_iterator is an iterator adapter with the same
923 * behavior as the underlying iterator except that its dereference
924 * operator implicitly converts the value returned by the underlying
925 * iterator's dereference operator to an rvalue reference. Some
926 * generic algorithms can be called with move iterators to replace
927 * copying with moving.
929 template<typename _Iterator>
933 _Iterator _M_current;
935 typedef iterator_traits<_Iterator> __traits_type;
938 typedef _Iterator iterator_type;
939 typedef typename __traits_type::iterator_category iterator_category;
940 typedef typename __traits_type::value_type value_type;
941 typedef typename __traits_type::difference_type difference_type;
943 typedef _Iterator pointer;
944 typedef value_type&& reference;
950 move_iterator(iterator_type __i)
951 : _M_current(__i) { }
953 template<typename _Iter>
954 move_iterator(const move_iterator<_Iter>& __i)
955 : _M_current(__i.base()) { }
959 { return _M_current; }
963 { return std::move(*_M_current); }
967 { return _M_current; }
979 move_iterator __tmp = *this;
994 move_iterator __tmp = *this;
1000 operator+(difference_type __n) const
1001 { return move_iterator(_M_current + __n); }
1004 operator+=(difference_type __n)
1011 operator-(difference_type __n) const
1012 { return move_iterator(_M_current - __n); }
1015 operator-=(difference_type __n)
1022 operator[](difference_type __n) const
1023 { return std::move(_M_current[__n]); }
1026 // Note: See __normal_iterator operators note from Gaby to understand
1027 // why there are always 2 versions for most of the move_iterator
1029 template<typename _IteratorL, typename _IteratorR>
1031 operator==(const move_iterator<_IteratorL>& __x,
1032 const move_iterator<_IteratorR>& __y)
1033 { return __x.base() == __y.base(); }
1035 template<typename _Iterator>
1037 operator==(const move_iterator<_Iterator>& __x,
1038 const move_iterator<_Iterator>& __y)
1039 { return __x.base() == __y.base(); }
1041 template<typename _IteratorL, typename _IteratorR>
1043 operator!=(const move_iterator<_IteratorL>& __x,
1044 const move_iterator<_IteratorR>& __y)
1045 { return !(__x == __y); }
1047 template<typename _Iterator>
1049 operator!=(const move_iterator<_Iterator>& __x,
1050 const move_iterator<_Iterator>& __y)
1051 { return !(__x == __y); }
1053 template<typename _IteratorL, typename _IteratorR>
1055 operator<(const move_iterator<_IteratorL>& __x,
1056 const move_iterator<_IteratorR>& __y)
1057 { return __x.base() < __y.base(); }
1059 template<typename _Iterator>
1061 operator<(const move_iterator<_Iterator>& __x,
1062 const move_iterator<_Iterator>& __y)
1063 { return __x.base() < __y.base(); }
1065 template<typename _IteratorL, typename _IteratorR>
1067 operator<=(const move_iterator<_IteratorL>& __x,
1068 const move_iterator<_IteratorR>& __y)
1069 { return !(__y < __x); }
1071 template<typename _Iterator>
1073 operator<=(const move_iterator<_Iterator>& __x,
1074 const move_iterator<_Iterator>& __y)
1075 { return !(__y < __x); }
1077 template<typename _IteratorL, typename _IteratorR>
1079 operator>(const move_iterator<_IteratorL>& __x,
1080 const move_iterator<_IteratorR>& __y)
1081 { return __y < __x; }
1083 template<typename _Iterator>
1085 operator>(const move_iterator<_Iterator>& __x,
1086 const move_iterator<_Iterator>& __y)
1087 { return __y < __x; }
1089 template<typename _IteratorL, typename _IteratorR>
1091 operator>=(const move_iterator<_IteratorL>& __x,
1092 const move_iterator<_IteratorR>& __y)
1093 { return !(__x < __y); }
1095 template<typename _Iterator>
1097 operator>=(const move_iterator<_Iterator>& __x,
1098 const move_iterator<_Iterator>& __y)
1099 { return !(__x < __y); }
1102 template<typename _IteratorL, typename _IteratorR>
1104 operator-(const move_iterator<_IteratorL>& __x,
1105 const move_iterator<_IteratorR>& __y)
1106 -> decltype(__x.base() - __y.base())
1107 { return __x.base() - __y.base(); }
1109 template<typename _Iterator>
1111 operator-(const move_iterator<_Iterator>& __x,
1112 const move_iterator<_Iterator>& __y)
1113 -> decltype(__x.base() - __y.base())
1114 { return __x.base() - __y.base(); }
1116 template<typename _Iterator>
1117 inline move_iterator<_Iterator>
1118 operator+(typename move_iterator<_Iterator>::difference_type __n,
1119 const move_iterator<_Iterator>& __x)
1120 { return __x + __n; }
1122 template<typename _Iterator>
1123 inline move_iterator<_Iterator>
1124 make_move_iterator(_Iterator __i)
1125 { return move_iterator<_Iterator>(__i); }
1127 template<typename _Iterator, typename _ReturnType
1128 = typename conditional<__move_if_noexcept_cond
1129 <typename iterator_traits<_Iterator>::value_type>::value,
1130 _Iterator, move_iterator<_Iterator>>::type>
1132 __make_move_if_noexcept_iterator(_Iterator __i)
1133 { return _ReturnType(__i); }
1135 // @} group iterators
1137 _GLIBCXX_END_NAMESPACE_VERSION
1140 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter)
1141 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \
1142 std::__make_move_if_noexcept_iterator(_Iter)
1144 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
1145 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter)