1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001-2015 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 namespace std _GLIBCXX_VISIBILITY(default)
70 _GLIBCXX_BEGIN_NAMESPACE_VERSION
73 * @addtogroup iterators
77 // 24.4.1 Reverse iterators
79 * Bidirectional and random access iterators have corresponding reverse
80 * %iterator adaptors that iterate through the data structure in the
81 * opposite direction. They have the same signatures as the corresponding
82 * iterators. The fundamental relation between a reverse %iterator and its
83 * corresponding %iterator @c i is established by the identity:
85 * &*(reverse_iterator(i)) == &*(i - 1)
88 * <em>This mapping is dictated by the fact that while there is always a
89 * pointer past the end of an array, there might not be a valid pointer
90 * before the beginning of an array.</em> [24.4.1]/1,2
92 * Reverse iterators can be tricky and surprising at first. Their
93 * semantics make sense, however, and the trickiness is a side effect of
94 * the requirement that the iterators must be safe.
96 template<typename _Iterator>
97 class reverse_iterator
98 : public iterator<typename iterator_traits<_Iterator>::iterator_category,
99 typename iterator_traits<_Iterator>::value_type,
100 typename iterator_traits<_Iterator>::difference_type,
101 typename iterator_traits<_Iterator>::pointer,
102 typename iterator_traits<_Iterator>::reference>
107 typedef iterator_traits<_Iterator> __traits_type;
110 typedef _Iterator iterator_type;
111 typedef typename __traits_type::difference_type difference_type;
112 typedef typename __traits_type::pointer pointer;
113 typedef typename __traits_type::reference reference;
116 * The default constructor value-initializes member @p current.
117 * If it is a pointer, that means it is zero-initialized.
119 // _GLIBCXX_RESOLVE_LIB_DEFECTS
120 // 235 No specification of default ctor for reverse_iterator
121 reverse_iterator() : current() { }
124 * This %iterator will move in the opposite direction that @p x does.
127 reverse_iterator(iterator_type __x) : current(__x) { }
130 * The copy constructor is normal.
132 reverse_iterator(const reverse_iterator& __x)
133 : current(__x.current) { }
136 * A %reverse_iterator across other types can be copied if the
137 * underlying %iterator can be converted to the type of @c current.
139 template<typename _Iter>
140 reverse_iterator(const reverse_iterator<_Iter>& __x)
141 : current(__x.base()) { }
144 * @return @c current, the %iterator used for underlying work.
151 * @return A reference to the value at @c --current
153 * This requires that @c --current is dereferenceable.
155 * @warning This implementation requires that for an iterator of the
156 * underlying iterator type, @c x, a reference obtained by
157 * @c *x remains valid after @c x has been modified or
158 * destroyed. This is a bug: http://gcc.gnu.org/PR51823
163 _Iterator __tmp = current;
168 * @return A pointer to the value at @c --current
170 * This requires that @c --current is dereferenceable.
174 { return &(operator*()); }
179 * Decrements the underlying iterator.
189 * @return The original value of @c *this
191 * Decrements the underlying iterator.
196 reverse_iterator __tmp = *this;
204 * Increments the underlying iterator.
214 * @return A reverse_iterator with the previous value of @c *this
216 * Increments the underlying iterator.
221 reverse_iterator __tmp = *this;
227 * @return A reverse_iterator that refers to @c current - @a __n
229 * The underlying iterator must be a Random Access Iterator.
232 operator+(difference_type __n) const
233 { return reverse_iterator(current - __n); }
238 * Moves the underlying iterator backwards @a __n steps.
239 * The underlying iterator must be a Random Access Iterator.
242 operator+=(difference_type __n)
249 * @return A reverse_iterator that refers to @c current - @a __n
251 * The underlying iterator must be a Random Access Iterator.
254 operator-(difference_type __n) const
255 { return reverse_iterator(current + __n); }
260 * Moves the underlying iterator forwards @a __n steps.
261 * The underlying iterator must be a Random Access Iterator.
264 operator-=(difference_type __n)
271 * @return The value at @c current - @a __n - 1
273 * The underlying iterator must be a Random Access Iterator.
276 operator[](difference_type __n) const
277 { return *(*this + __n); }
282 * @param __x A %reverse_iterator.
283 * @param __y A %reverse_iterator.
284 * @return A simple bool.
286 * Reverse iterators forward many operations to their underlying base()
287 * iterators. Others are implemented in terms of one another.
290 template<typename _Iterator>
292 operator==(const reverse_iterator<_Iterator>& __x,
293 const reverse_iterator<_Iterator>& __y)
294 { return __x.base() == __y.base(); }
296 template<typename _Iterator>
298 operator<(const reverse_iterator<_Iterator>& __x,
299 const reverse_iterator<_Iterator>& __y)
300 { return __y.base() < __x.base(); }
302 template<typename _Iterator>
304 operator!=(const reverse_iterator<_Iterator>& __x,
305 const reverse_iterator<_Iterator>& __y)
306 { return !(__x == __y); }
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 !(__y < __x); }
320 template<typename _Iterator>
322 operator>=(const reverse_iterator<_Iterator>& __x,
323 const reverse_iterator<_Iterator>& __y)
324 { return !(__x < __y); }
326 template<typename _Iterator>
327 #if __cplusplus < 201103L
328 inline typename reverse_iterator<_Iterator>::difference_type
329 operator-(const reverse_iterator<_Iterator>& __x,
330 const reverse_iterator<_Iterator>& __y)
333 operator-(const reverse_iterator<_Iterator>& __x,
334 const reverse_iterator<_Iterator>& __y)
335 -> decltype(__x.base() - __y.base())
337 { return __y.base() - __x.base(); }
339 template<typename _Iterator>
340 inline reverse_iterator<_Iterator>
341 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
342 const reverse_iterator<_Iterator>& __x)
343 { return reverse_iterator<_Iterator>(__x.base() - __n); }
345 // _GLIBCXX_RESOLVE_LIB_DEFECTS
346 // DR 280. Comparison of reverse_iterator to const reverse_iterator.
347 template<typename _IteratorL, typename _IteratorR>
349 operator==(const reverse_iterator<_IteratorL>& __x,
350 const reverse_iterator<_IteratorR>& __y)
351 { return __x.base() == __y.base(); }
353 template<typename _IteratorL, typename _IteratorR>
355 operator<(const reverse_iterator<_IteratorL>& __x,
356 const reverse_iterator<_IteratorR>& __y)
357 { return __y.base() < __x.base(); }
359 template<typename _IteratorL, typename _IteratorR>
361 operator!=(const reverse_iterator<_IteratorL>& __x,
362 const reverse_iterator<_IteratorR>& __y)
363 { return !(__x == __y); }
365 template<typename _IteratorL, typename _IteratorR>
367 operator>(const reverse_iterator<_IteratorL>& __x,
368 const reverse_iterator<_IteratorR>& __y)
369 { return __y < __x; }
371 template<typename _IteratorL, typename _IteratorR>
373 operator<=(const reverse_iterator<_IteratorL>& __x,
374 const reverse_iterator<_IteratorR>& __y)
375 { return !(__y < __x); }
377 template<typename _IteratorL, typename _IteratorR>
379 operator>=(const reverse_iterator<_IteratorL>& __x,
380 const reverse_iterator<_IteratorR>& __y)
381 { return !(__x < __y); }
383 template<typename _IteratorL, typename _IteratorR>
384 #if __cplusplus >= 201103L
387 operator-(const reverse_iterator<_IteratorL>& __x,
388 const reverse_iterator<_IteratorR>& __y)
389 -> decltype(__y.base() - __x.base())
391 inline typename reverse_iterator<_IteratorL>::difference_type
392 operator-(const reverse_iterator<_IteratorL>& __x,
393 const reverse_iterator<_IteratorR>& __y)
395 { return __y.base() - __x.base(); }
398 #if __cplusplus > 201103L
399 #define __cpp_lib_make_reverse_iterator 201402
401 // _GLIBCXX_RESOLVE_LIB_DEFECTS
402 // DR 2285. make_reverse_iterator
403 /// Generator function for reverse_iterator.
404 template<typename _Iterator>
405 inline reverse_iterator<_Iterator>
406 make_reverse_iterator(_Iterator __i)
407 { return reverse_iterator<_Iterator>(__i); }
410 // 24.4.2.2.1 back_insert_iterator
412 * @brief Turns assignment into insertion.
414 * These are output iterators, constructed from a container-of-T.
415 * Assigning a T to the iterator appends it to the container using
418 * Tip: Using the back_inserter function to create these iterators can
421 template<typename _Container>
422 class back_insert_iterator
423 : public iterator<output_iterator_tag, void, void, void, void>
426 _Container* container;
429 /// A nested typedef for the type of whatever container you used.
430 typedef _Container container_type;
432 /// The only way to create this %iterator is with a container.
434 back_insert_iterator(_Container& __x) : container(&__x) { }
437 * @param __value An instance of whatever type
438 * container_type::const_reference is; presumably a
439 * reference-to-const T for container<T>.
440 * @return This %iterator, for chained operations.
442 * This kind of %iterator doesn't really have a @a position in the
443 * container (you can think of the position as being permanently at
444 * the end, if you like). Assigning a value to the %iterator will
445 * always append the value to the end of the container.
447 #if __cplusplus < 201103L
448 back_insert_iterator&
449 operator=(typename _Container::const_reference __value)
451 container->push_back(__value);
455 back_insert_iterator&
456 operator=(const typename _Container::value_type& __value)
458 container->push_back(__value);
462 back_insert_iterator&
463 operator=(typename _Container::value_type&& __value)
465 container->push_back(std::move(__value));
470 /// Simply returns *this.
471 back_insert_iterator&
475 /// Simply returns *this. (This %iterator does not @a move.)
476 back_insert_iterator&
480 /// Simply returns *this. (This %iterator does not @a move.)
487 * @param __x A container of arbitrary type.
488 * @return An instance of back_insert_iterator working on @p __x.
490 * This wrapper function helps in creating back_insert_iterator instances.
491 * Typing the name of the %iterator requires knowing the precise full
492 * type of the container, which can be tedious and impedes generic
493 * programming. Using this function lets you take advantage of automatic
494 * template parameter deduction, making the compiler match the correct
497 template<typename _Container>
498 inline back_insert_iterator<_Container>
499 back_inserter(_Container& __x)
500 { return back_insert_iterator<_Container>(__x); }
503 * @brief Turns assignment into insertion.
505 * These are output iterators, constructed from a container-of-T.
506 * Assigning a T to the iterator prepends it to the container using
509 * Tip: Using the front_inserter function to create these iterators can
512 template<typename _Container>
513 class front_insert_iterator
514 : public iterator<output_iterator_tag, void, void, void, void>
517 _Container* container;
520 /// A nested typedef for the type of whatever container you used.
521 typedef _Container container_type;
523 /// The only way to create this %iterator is with a container.
524 explicit front_insert_iterator(_Container& __x) : container(&__x) { }
527 * @param __value An instance of whatever type
528 * container_type::const_reference is; presumably a
529 * reference-to-const T for container<T>.
530 * @return This %iterator, for chained operations.
532 * This kind of %iterator doesn't really have a @a position in the
533 * container (you can think of the position as being permanently at
534 * the front, if you like). Assigning a value to the %iterator will
535 * always prepend the value to the front of the container.
537 #if __cplusplus < 201103L
538 front_insert_iterator&
539 operator=(typename _Container::const_reference __value)
541 container->push_front(__value);
545 front_insert_iterator&
546 operator=(const typename _Container::value_type& __value)
548 container->push_front(__value);
552 front_insert_iterator&
553 operator=(typename _Container::value_type&& __value)
555 container->push_front(std::move(__value));
560 /// Simply returns *this.
561 front_insert_iterator&
565 /// Simply returns *this. (This %iterator does not @a move.)
566 front_insert_iterator&
570 /// Simply returns *this. (This %iterator does not @a move.)
571 front_insert_iterator
577 * @param __x A container of arbitrary type.
578 * @return An instance of front_insert_iterator working on @p x.
580 * This wrapper function helps in creating front_insert_iterator instances.
581 * Typing the name of the %iterator requires knowing the precise full
582 * type of the container, which can be tedious and impedes generic
583 * programming. Using this function lets you take advantage of automatic
584 * template parameter deduction, making the compiler match the correct
587 template<typename _Container>
588 inline front_insert_iterator<_Container>
589 front_inserter(_Container& __x)
590 { return front_insert_iterator<_Container>(__x); }
593 * @brief Turns assignment into insertion.
595 * These are output iterators, constructed from a container-of-T.
596 * Assigning a T to the iterator inserts it in the container at the
597 * %iterator's position, rather than overwriting the value at that
600 * (Sequences will actually insert a @e copy of the value before the
601 * %iterator's position.)
603 * Tip: Using the inserter function to create these iterators can
606 template<typename _Container>
607 class insert_iterator
608 : public iterator<output_iterator_tag, void, void, void, void>
611 _Container* container;
612 typename _Container::iterator iter;
615 /// A nested typedef for the type of whatever container you used.
616 typedef _Container container_type;
619 * The only way to create this %iterator is with a container and an
620 * initial position (a normal %iterator into the container).
622 insert_iterator(_Container& __x, typename _Container::iterator __i)
623 : container(&__x), iter(__i) {}
626 * @param __value An instance of whatever type
627 * container_type::const_reference is; presumably a
628 * reference-to-const T for container<T>.
629 * @return This %iterator, for chained operations.
631 * This kind of %iterator maintains its own position in the
632 * container. Assigning a value to the %iterator will insert the
633 * value into the container at the place before the %iterator.
635 * The position is maintained such that subsequent assignments will
636 * insert values immediately after one another. For example,
638 * // vector v contains A and Z
640 * insert_iterator i (v, ++v.begin());
645 * // vector v contains A, 1, 2, 3, and Z
648 #if __cplusplus < 201103L
650 operator=(typename _Container::const_reference __value)
652 iter = container->insert(iter, __value);
658 operator=(const typename _Container::value_type& __value)
660 iter = container->insert(iter, __value);
666 operator=(typename _Container::value_type&& __value)
668 iter = container->insert(iter, std::move(__value));
674 /// Simply returns *this.
679 /// Simply returns *this. (This %iterator does not @a move.)
684 /// Simply returns *this. (This %iterator does not @a move.)
691 * @param __x A container of arbitrary type.
692 * @return An instance of insert_iterator working on @p __x.
694 * This wrapper function helps in creating insert_iterator instances.
695 * Typing the name of the %iterator requires knowing the precise full
696 * type of the container, which can be tedious and impedes generic
697 * programming. Using this function lets you take advantage of automatic
698 * template parameter deduction, making the compiler match the correct
701 template<typename _Container, typename _Iterator>
702 inline insert_iterator<_Container>
703 inserter(_Container& __x, _Iterator __i)
705 return insert_iterator<_Container>(__x,
706 typename _Container::iterator(__i));
709 // @} group iterators
711 _GLIBCXX_END_NAMESPACE_VERSION
714 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
716 _GLIBCXX_BEGIN_NAMESPACE_VERSION
718 // This iterator adapter is @a normal in the sense that it does not
719 // change the semantics of any of the operators of its iterator
720 // parameter. Its primary purpose is to convert an iterator that is
721 // not a class, e.g. a pointer, into an iterator that is a class.
722 // The _Container parameter exists solely so that different containers
723 // using this template can instantiate different types, even if the
724 // _Iterator parameter is the same.
725 using std::iterator_traits;
727 template<typename _Iterator, typename _Container>
728 class __normal_iterator
731 _Iterator _M_current;
733 typedef iterator_traits<_Iterator> __traits_type;
736 typedef _Iterator iterator_type;
737 typedef typename __traits_type::iterator_category iterator_category;
738 typedef typename __traits_type::value_type value_type;
739 typedef typename __traits_type::difference_type difference_type;
740 typedef typename __traits_type::reference reference;
741 typedef typename __traits_type::pointer pointer;
743 _GLIBCXX_CONSTEXPR __normal_iterator() _GLIBCXX_NOEXCEPT
744 : _M_current(_Iterator()) { }
747 __normal_iterator(const _Iterator& __i) _GLIBCXX_NOEXCEPT
748 : _M_current(__i) { }
750 // Allow iterator to const_iterator conversion
751 template<typename _Iter>
752 __normal_iterator(const __normal_iterator<_Iter,
753 typename __enable_if<
754 (std::__are_same<_Iter, typename _Container::pointer>::__value),
755 _Container>::__type>& __i) _GLIBCXX_NOEXCEPT
756 : _M_current(__i.base()) { }
758 // Forward iterator requirements
760 operator*() const _GLIBCXX_NOEXCEPT
761 { return *_M_current; }
764 operator->() const _GLIBCXX_NOEXCEPT
765 { return _M_current; }
768 operator++() _GLIBCXX_NOEXCEPT
775 operator++(int) _GLIBCXX_NOEXCEPT
776 { return __normal_iterator(_M_current++); }
778 // Bidirectional iterator requirements
780 operator--() _GLIBCXX_NOEXCEPT
787 operator--(int) _GLIBCXX_NOEXCEPT
788 { return __normal_iterator(_M_current--); }
790 // Random access iterator requirements
792 operator[](difference_type __n) const _GLIBCXX_NOEXCEPT
793 { return _M_current[__n]; }
796 operator+=(difference_type __n) _GLIBCXX_NOEXCEPT
797 { _M_current += __n; return *this; }
800 operator+(difference_type __n) const _GLIBCXX_NOEXCEPT
801 { return __normal_iterator(_M_current + __n); }
804 operator-=(difference_type __n) _GLIBCXX_NOEXCEPT
805 { _M_current -= __n; return *this; }
808 operator-(difference_type __n) const _GLIBCXX_NOEXCEPT
809 { return __normal_iterator(_M_current - __n); }
812 base() const _GLIBCXX_NOEXCEPT
813 { return _M_current; }
816 // Note: In what follows, the left- and right-hand-side iterators are
817 // allowed to vary in types (conceptually in cv-qualification) so that
818 // comparison between cv-qualified and non-cv-qualified iterators be
819 // valid. However, the greedy and unfriendly operators in std::rel_ops
820 // will make overload resolution ambiguous (when in scope) if we don't
821 // provide overloads whose operands are of the same type. Can someone
822 // remind me what generic programming is about? -- Gaby
824 // Forward iterator requirements
825 template<typename _IteratorL, typename _IteratorR, typename _Container>
827 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
828 const __normal_iterator<_IteratorR, _Container>& __rhs)
830 { return __lhs.base() == __rhs.base(); }
832 template<typename _Iterator, typename _Container>
834 operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
835 const __normal_iterator<_Iterator, _Container>& __rhs)
837 { return __lhs.base() == __rhs.base(); }
839 template<typename _IteratorL, typename _IteratorR, typename _Container>
841 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
842 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)
851 { return __lhs.base() != __rhs.base(); }
853 // Random access iterator requirements
854 template<typename _IteratorL, typename _IteratorR, typename _Container>
856 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
857 const __normal_iterator<_IteratorR, _Container>& __rhs)
859 { return __lhs.base() < __rhs.base(); }
861 template<typename _Iterator, typename _Container>
863 operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
864 const __normal_iterator<_Iterator, _Container>& __rhs)
866 { return __lhs.base() < __rhs.base(); }
868 template<typename _IteratorL, typename _IteratorR, typename _Container>
870 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
871 const __normal_iterator<_IteratorR, _Container>& __rhs)
873 { return __lhs.base() > __rhs.base(); }
875 template<typename _Iterator, typename _Container>
877 operator>(const __normal_iterator<_Iterator, _Container>& __lhs,
878 const __normal_iterator<_Iterator, _Container>& __rhs)
880 { return __lhs.base() > __rhs.base(); }
882 template<typename _IteratorL, typename _IteratorR, typename _Container>
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>
891 operator<=(const __normal_iterator<_Iterator, _Container>& __lhs,
892 const __normal_iterator<_Iterator, _Container>& __rhs)
894 { return __lhs.base() <= __rhs.base(); }
896 template<typename _IteratorL, typename _IteratorR, typename _Container>
898 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
899 const __normal_iterator<_IteratorR, _Container>& __rhs)
901 { return __lhs.base() >= __rhs.base(); }
903 template<typename _Iterator, typename _Container>
905 operator>=(const __normal_iterator<_Iterator, _Container>& __lhs,
906 const __normal_iterator<_Iterator, _Container>& __rhs)
908 { return __lhs.base() >= __rhs.base(); }
910 // _GLIBCXX_RESOLVE_LIB_DEFECTS
911 // According to the resolution of DR179 not only the various comparison
912 // operators but also operator- must accept mixed iterator/const_iterator
914 template<typename _IteratorL, typename _IteratorR, typename _Container>
915 #if __cplusplus >= 201103L
918 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
919 const __normal_iterator<_IteratorR, _Container>& __rhs) noexcept
920 -> decltype(__lhs.base() - __rhs.base())
922 inline typename __normal_iterator<_IteratorL, _Container>::difference_type
923 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
924 const __normal_iterator<_IteratorR, _Container>& __rhs)
926 { return __lhs.base() - __rhs.base(); }
928 template<typename _Iterator, typename _Container>
929 inline typename __normal_iterator<_Iterator, _Container>::difference_type
930 operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
931 const __normal_iterator<_Iterator, _Container>& __rhs)
933 { return __lhs.base() - __rhs.base(); }
935 template<typename _Iterator, typename _Container>
936 inline __normal_iterator<_Iterator, _Container>
937 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
938 __n, const __normal_iterator<_Iterator, _Container>& __i)
940 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
942 _GLIBCXX_END_NAMESPACE_VERSION
945 #if __cplusplus >= 201103L
947 namespace std _GLIBCXX_VISIBILITY(default)
949 _GLIBCXX_BEGIN_NAMESPACE_VERSION
952 * @addtogroup iterators
956 // 24.4.3 Move iterators
958 * Class template move_iterator is an iterator adapter with the same
959 * behavior as the underlying iterator except that its dereference
960 * operator implicitly converts the value returned by the underlying
961 * iterator's dereference operator to an rvalue reference. Some
962 * generic algorithms can be called with move iterators to replace
963 * copying with moving.
965 template<typename _Iterator>
969 _Iterator _M_current;
971 typedef iterator_traits<_Iterator> __traits_type;
972 typedef typename __traits_type::reference __base_ref;
975 typedef _Iterator iterator_type;
976 typedef typename __traits_type::iterator_category iterator_category;
977 typedef typename __traits_type::value_type value_type;
978 typedef typename __traits_type::difference_type difference_type;
980 typedef _Iterator pointer;
981 // _GLIBCXX_RESOLVE_LIB_DEFECTS
982 // 2106. move_iterator wrapping iterators returning prvalues
983 typedef typename conditional<is_reference<__base_ref>::value,
984 typename remove_reference<__base_ref>::type&&,
985 __base_ref>::type reference;
991 move_iterator(iterator_type __i)
992 : _M_current(__i) { }
994 template<typename _Iter>
995 move_iterator(const move_iterator<_Iter>& __i)
996 : _M_current(__i.base()) { }
1000 { return _M_current; }
1004 { return static_cast<reference>(*_M_current); }
1008 { return _M_current; }
1020 move_iterator __tmp = *this;
1035 move_iterator __tmp = *this;
1041 operator+(difference_type __n) const
1042 { return move_iterator(_M_current + __n); }
1045 operator+=(difference_type __n)
1052 operator-(difference_type __n) const
1053 { return move_iterator(_M_current - __n); }
1056 operator-=(difference_type __n)
1063 operator[](difference_type __n) const
1064 { return std::move(_M_current[__n]); }
1067 // Note: See __normal_iterator operators note from Gaby to understand
1068 // why there are always 2 versions for most of the move_iterator
1070 template<typename _IteratorL, typename _IteratorR>
1072 operator==(const move_iterator<_IteratorL>& __x,
1073 const move_iterator<_IteratorR>& __y)
1074 { return __x.base() == __y.base(); }
1076 template<typename _Iterator>
1078 operator==(const move_iterator<_Iterator>& __x,
1079 const move_iterator<_Iterator>& __y)
1080 { return __x.base() == __y.base(); }
1082 template<typename _IteratorL, typename _IteratorR>
1084 operator!=(const move_iterator<_IteratorL>& __x,
1085 const move_iterator<_IteratorR>& __y)
1086 { return !(__x == __y); }
1088 template<typename _Iterator>
1090 operator!=(const move_iterator<_Iterator>& __x,
1091 const move_iterator<_Iterator>& __y)
1092 { return !(__x == __y); }
1094 template<typename _IteratorL, typename _IteratorR>
1096 operator<(const move_iterator<_IteratorL>& __x,
1097 const move_iterator<_IteratorR>& __y)
1098 { return __x.base() < __y.base(); }
1100 template<typename _Iterator>
1102 operator<(const move_iterator<_Iterator>& __x,
1103 const move_iterator<_Iterator>& __y)
1104 { return __x.base() < __y.base(); }
1106 template<typename _IteratorL, typename _IteratorR>
1108 operator<=(const move_iterator<_IteratorL>& __x,
1109 const move_iterator<_IteratorR>& __y)
1110 { return !(__y < __x); }
1112 template<typename _Iterator>
1114 operator<=(const move_iterator<_Iterator>& __x,
1115 const move_iterator<_Iterator>& __y)
1116 { return !(__y < __x); }
1118 template<typename _IteratorL, typename _IteratorR>
1120 operator>(const move_iterator<_IteratorL>& __x,
1121 const move_iterator<_IteratorR>& __y)
1122 { return __y < __x; }
1124 template<typename _Iterator>
1126 operator>(const move_iterator<_Iterator>& __x,
1127 const move_iterator<_Iterator>& __y)
1128 { return __y < __x; }
1130 template<typename _IteratorL, typename _IteratorR>
1132 operator>=(const move_iterator<_IteratorL>& __x,
1133 const move_iterator<_IteratorR>& __y)
1134 { return !(__x < __y); }
1136 template<typename _Iterator>
1138 operator>=(const move_iterator<_Iterator>& __x,
1139 const move_iterator<_Iterator>& __y)
1140 { return !(__x < __y); }
1143 template<typename _IteratorL, typename _IteratorR>
1145 operator-(const move_iterator<_IteratorL>& __x,
1146 const move_iterator<_IteratorR>& __y)
1147 -> decltype(__x.base() - __y.base())
1148 { return __x.base() - __y.base(); }
1150 template<typename _Iterator>
1152 operator-(const move_iterator<_Iterator>& __x,
1153 const move_iterator<_Iterator>& __y)
1154 -> decltype(__x.base() - __y.base())
1155 { return __x.base() - __y.base(); }
1157 template<typename _Iterator>
1158 inline move_iterator<_Iterator>
1159 operator+(typename move_iterator<_Iterator>::difference_type __n,
1160 const move_iterator<_Iterator>& __x)
1161 { return __x + __n; }
1163 template<typename _Iterator>
1164 inline move_iterator<_Iterator>
1165 make_move_iterator(_Iterator __i)
1166 { return move_iterator<_Iterator>(__i); }
1168 template<typename _Iterator, typename _ReturnType
1169 = typename conditional<__move_if_noexcept_cond
1170 <typename iterator_traits<_Iterator>::value_type>::value,
1171 _Iterator, move_iterator<_Iterator>>::type>
1173 __make_move_if_noexcept_iterator(_Iterator __i)
1174 { return _ReturnType(__i); }
1176 // @} group iterators
1178 _GLIBCXX_END_NAMESPACE_VERSION
1181 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter)
1182 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \
1183 std::__make_move_if_noexcept_iterator(_Iter)
1185 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
1186 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter)