2013-01-18 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libstdc++-v3 / include / bits / stl_iterator.h
blob14371bc82037c25e87ed1bbe0e7922a672864156
1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 // 2010, 2011, 2012
5 // Free Software Foundation, Inc.
6 //
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)
11 // any later version.
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/>.
29 * Copyright (c) 1994
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
73 /**
74 * @addtogroup iterators
75 * @{
78 // 24.4.1 Reverse iterators
79 /**
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:
85 * @code
86 * &*(reverse_iterator(i)) == &*(i - 1)
87 * @endcode
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>
105 protected:
106 _Iterator current;
108 typedef iterator_traits<_Iterator> __traits_type;
110 public:
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 value-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.
127 explicit
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 if the
138 * underlying %iterator can be converted to the type of @c current.
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.
147 iterator_type
148 base() const
149 { return current; }
152 * @return A reference to the value at @c --current
154 * This requires that @c --current is dereferenceable.
156 * @warning This implementation requires that for an iterator of the
157 * underlying iterator type, @c x, a reference obtained by
158 * @c *x remains valid after @c x has been modified or
159 * destroyed. This is a bug: http://gcc.gnu.org/PR51823
161 reference
162 operator*() const
164 _Iterator __tmp = current;
165 return *--__tmp;
169 * @return A pointer to the value at @c --current
171 * This requires that @c --current is dereferenceable.
173 pointer
174 operator->() const
175 { return &(operator*()); }
178 * @return @c *this
180 * Decrements the underlying iterator.
182 reverse_iterator&
183 operator++()
185 --current;
186 return *this;
190 * @return The original value of @c *this
192 * Decrements the underlying iterator.
194 reverse_iterator
195 operator++(int)
197 reverse_iterator __tmp = *this;
198 --current;
199 return __tmp;
203 * @return @c *this
205 * Increments the underlying iterator.
207 reverse_iterator&
208 operator--()
210 ++current;
211 return *this;
215 * @return A reverse_iterator with the previous value of @c *this
217 * Increments the underlying iterator.
219 reverse_iterator
220 operator--(int)
222 reverse_iterator __tmp = *this;
223 ++current;
224 return __tmp;
228 * @return A reverse_iterator that refers to @c current - @a __n
230 * The underlying iterator must be a Random Access Iterator.
232 reverse_iterator
233 operator+(difference_type __n) const
234 { return reverse_iterator(current - __n); }
237 * @return *this
239 * Moves the underlying iterator backwards @a __n steps.
240 * The underlying iterator must be a Random Access Iterator.
242 reverse_iterator&
243 operator+=(difference_type __n)
245 current -= __n;
246 return *this;
250 * @return A reverse_iterator that refers to @c current - @a __n
252 * The underlying iterator must be a Random Access Iterator.
254 reverse_iterator
255 operator-(difference_type __n) const
256 { return reverse_iterator(current + __n); }
259 * @return *this
261 * Moves the underlying iterator forwards @a __n steps.
262 * The underlying iterator must be a Random Access Iterator.
264 reverse_iterator&
265 operator-=(difference_type __n)
267 current += __n;
268 return *this;
272 * @return The value at @c current - @a __n - 1
274 * The underlying iterator must be a Random Access Iterator.
276 reference
277 operator[](difference_type __n) const
278 { return *(*this + __n); }
281 //@{
283 * @param __x A %reverse_iterator.
284 * @param __y A %reverse_iterator.
285 * @return A simple bool.
287 * Reverse iterators forward many operations to their underlying base()
288 * iterators. Others are implemented in terms of one another.
291 template<typename _Iterator>
292 inline bool
293 operator==(const reverse_iterator<_Iterator>& __x,
294 const reverse_iterator<_Iterator>& __y)
295 { return __x.base() == __y.base(); }
297 template<typename _Iterator>
298 inline bool
299 operator<(const reverse_iterator<_Iterator>& __x,
300 const reverse_iterator<_Iterator>& __y)
301 { return __y.base() < __x.base(); }
303 template<typename _Iterator>
304 inline bool
305 operator!=(const reverse_iterator<_Iterator>& __x,
306 const reverse_iterator<_Iterator>& __y)
307 { return !(__x == __y); }
309 template<typename _Iterator>
310 inline bool
311 operator>(const reverse_iterator<_Iterator>& __x,
312 const reverse_iterator<_Iterator>& __y)
313 { return __y < __x; }
315 template<typename _Iterator>
316 inline bool
317 operator<=(const reverse_iterator<_Iterator>& __x,
318 const reverse_iterator<_Iterator>& __y)
319 { return !(__y < __x); }
321 template<typename _Iterator>
322 inline bool
323 operator>=(const reverse_iterator<_Iterator>& __x,
324 const reverse_iterator<_Iterator>& __y)
325 { return !(__x < __y); }
327 template<typename _Iterator>
328 inline typename reverse_iterator<_Iterator>::difference_type
329 operator-(const reverse_iterator<_Iterator>& __x,
330 const reverse_iterator<_Iterator>& __y)
331 { return __y.base() - __x.base(); }
333 template<typename _Iterator>
334 inline reverse_iterator<_Iterator>
335 operator+(typename reverse_iterator<_Iterator>::difference_type __n,
336 const reverse_iterator<_Iterator>& __x)
337 { return reverse_iterator<_Iterator>(__x.base() - __n); }
339 // _GLIBCXX_RESOLVE_LIB_DEFECTS
340 // DR 280. Comparison of reverse_iterator to const reverse_iterator.
341 template<typename _IteratorL, typename _IteratorR>
342 inline bool
343 operator==(const reverse_iterator<_IteratorL>& __x,
344 const reverse_iterator<_IteratorR>& __y)
345 { return __x.base() == __y.base(); }
347 template<typename _IteratorL, typename _IteratorR>
348 inline bool
349 operator<(const reverse_iterator<_IteratorL>& __x,
350 const reverse_iterator<_IteratorR>& __y)
351 { return __y.base() < __x.base(); }
353 template<typename _IteratorL, typename _IteratorR>
354 inline bool
355 operator!=(const reverse_iterator<_IteratorL>& __x,
356 const reverse_iterator<_IteratorR>& __y)
357 { return !(__x == __y); }
359 template<typename _IteratorL, typename _IteratorR>
360 inline bool
361 operator>(const reverse_iterator<_IteratorL>& __x,
362 const reverse_iterator<_IteratorR>& __y)
363 { return __y < __x; }
365 template<typename _IteratorL, typename _IteratorR>
366 inline bool
367 operator<=(const reverse_iterator<_IteratorL>& __x,
368 const reverse_iterator<_IteratorR>& __y)
369 { return !(__y < __x); }
371 template<typename _IteratorL, typename _IteratorR>
372 inline bool
373 operator>=(const reverse_iterator<_IteratorL>& __x,
374 const reverse_iterator<_IteratorR>& __y)
375 { return !(__x < __y); }
377 template<typename _IteratorL, typename _IteratorR>
378 #if __cplusplus >= 201103L
379 // DR 685.
380 inline auto
381 operator-(const reverse_iterator<_IteratorL>& __x,
382 const reverse_iterator<_IteratorR>& __y)
383 -> decltype(__y.base() - __x.base())
384 #else
385 inline typename reverse_iterator<_IteratorL>::difference_type
386 operator-(const reverse_iterator<_IteratorL>& __x,
387 const reverse_iterator<_IteratorR>& __y)
388 #endif
389 { return __y.base() - __x.base(); }
390 //@}
392 // 24.4.2.2.1 back_insert_iterator
394 * @brief Turns assignment into insertion.
396 * These are output iterators, constructed from a container-of-T.
397 * Assigning a T to the iterator appends it to the container using
398 * push_back.
400 * Tip: Using the back_inserter function to create these iterators can
401 * save typing.
403 template<typename _Container>
404 class back_insert_iterator
405 : public iterator<output_iterator_tag, void, void, void, void>
407 protected:
408 _Container* container;
410 public:
411 /// A nested typedef for the type of whatever container you used.
412 typedef _Container container_type;
414 /// The only way to create this %iterator is with a container.
415 explicit
416 back_insert_iterator(_Container& __x) : container(&__x) { }
419 * @param __value An instance of whatever type
420 * container_type::const_reference is; presumably a
421 * reference-to-const T for container<T>.
422 * @return This %iterator, for chained operations.
424 * This kind of %iterator doesn't really have a @a position in the
425 * container (you can think of the position as being permanently at
426 * the end, if you like). Assigning a value to the %iterator will
427 * always append the value to the end of the container.
429 #if __cplusplus < 201103L
430 back_insert_iterator&
431 operator=(typename _Container::const_reference __value)
433 container->push_back(__value);
434 return *this;
436 #else
437 back_insert_iterator&
438 operator=(const typename _Container::value_type& __value)
440 container->push_back(__value);
441 return *this;
444 back_insert_iterator&
445 operator=(typename _Container::value_type&& __value)
447 container->push_back(std::move(__value));
448 return *this;
450 #endif
452 /// Simply returns *this.
453 back_insert_iterator&
454 operator*()
455 { return *this; }
457 /// Simply returns *this. (This %iterator does not @a move.)
458 back_insert_iterator&
459 operator++()
460 { return *this; }
462 /// Simply returns *this. (This %iterator does not @a move.)
463 back_insert_iterator
464 operator++(int)
465 { return *this; }
469 * @param __x A container of arbitrary type.
470 * @return An instance of back_insert_iterator working on @p __x.
472 * This wrapper function helps in creating back_insert_iterator instances.
473 * Typing the name of the %iterator requires knowing the precise full
474 * type of the container, which can be tedious and impedes generic
475 * programming. Using this function lets you take advantage of automatic
476 * template parameter deduction, making the compiler match the correct
477 * types for you.
479 template<typename _Container>
480 inline back_insert_iterator<_Container>
481 back_inserter(_Container& __x)
482 { return back_insert_iterator<_Container>(__x); }
485 * @brief Turns assignment into insertion.
487 * These are output iterators, constructed from a container-of-T.
488 * Assigning a T to the iterator prepends it to the container using
489 * push_front.
491 * Tip: Using the front_inserter function to create these iterators can
492 * save typing.
494 template<typename _Container>
495 class front_insert_iterator
496 : public iterator<output_iterator_tag, void, void, void, void>
498 protected:
499 _Container* container;
501 public:
502 /// A nested typedef for the type of whatever container you used.
503 typedef _Container container_type;
505 /// The only way to create this %iterator is with a container.
506 explicit front_insert_iterator(_Container& __x) : container(&__x) { }
509 * @param __value An instance of whatever type
510 * container_type::const_reference is; presumably a
511 * reference-to-const T for container<T>.
512 * @return This %iterator, for chained operations.
514 * This kind of %iterator doesn't really have a @a position in the
515 * container (you can think of the position as being permanently at
516 * the front, if you like). Assigning a value to the %iterator will
517 * always prepend the value to the front of the container.
519 #if __cplusplus < 201103L
520 front_insert_iterator&
521 operator=(typename _Container::const_reference __value)
523 container->push_front(__value);
524 return *this;
526 #else
527 front_insert_iterator&
528 operator=(const typename _Container::value_type& __value)
530 container->push_front(__value);
531 return *this;
534 front_insert_iterator&
535 operator=(typename _Container::value_type&& __value)
537 container->push_front(std::move(__value));
538 return *this;
540 #endif
542 /// Simply returns *this.
543 front_insert_iterator&
544 operator*()
545 { return *this; }
547 /// Simply returns *this. (This %iterator does not @a move.)
548 front_insert_iterator&
549 operator++()
550 { return *this; }
552 /// Simply returns *this. (This %iterator does not @a move.)
553 front_insert_iterator
554 operator++(int)
555 { return *this; }
559 * @param __x A container of arbitrary type.
560 * @return An instance of front_insert_iterator working on @p x.
562 * This wrapper function helps in creating front_insert_iterator instances.
563 * Typing the name of the %iterator requires knowing the precise full
564 * type of the container, which can be tedious and impedes generic
565 * programming. Using this function lets you take advantage of automatic
566 * template parameter deduction, making the compiler match the correct
567 * types for you.
569 template<typename _Container>
570 inline front_insert_iterator<_Container>
571 front_inserter(_Container& __x)
572 { return front_insert_iterator<_Container>(__x); }
575 * @brief Turns assignment into insertion.
577 * These are output iterators, constructed from a container-of-T.
578 * Assigning a T to the iterator inserts it in the container at the
579 * %iterator's position, rather than overwriting the value at that
580 * position.
582 * (Sequences will actually insert a @e copy of the value before the
583 * %iterator's position.)
585 * Tip: Using the inserter function to create these iterators can
586 * save typing.
588 template<typename _Container>
589 class insert_iterator
590 : public iterator<output_iterator_tag, void, void, void, void>
592 protected:
593 _Container* container;
594 typename _Container::iterator iter;
596 public:
597 /// A nested typedef for the type of whatever container you used.
598 typedef _Container container_type;
601 * The only way to create this %iterator is with a container and an
602 * initial position (a normal %iterator into the container).
604 insert_iterator(_Container& __x, typename _Container::iterator __i)
605 : container(&__x), iter(__i) {}
608 * @param __value An instance of whatever type
609 * container_type::const_reference is; presumably a
610 * reference-to-const T for container<T>.
611 * @return This %iterator, for chained operations.
613 * This kind of %iterator maintains its own position in the
614 * container. Assigning a value to the %iterator will insert the
615 * value into the container at the place before the %iterator.
617 * The position is maintained such that subsequent assignments will
618 * insert values immediately after one another. For example,
619 * @code
620 * // vector v contains A and Z
622 * insert_iterator i (v, ++v.begin());
623 * i = 1;
624 * i = 2;
625 * i = 3;
627 * // vector v contains A, 1, 2, 3, and Z
628 * @endcode
630 #if __cplusplus < 201103L
631 insert_iterator&
632 operator=(typename _Container::const_reference __value)
634 iter = container->insert(iter, __value);
635 ++iter;
636 return *this;
638 #else
639 insert_iterator&
640 operator=(const typename _Container::value_type& __value)
642 iter = container->insert(iter, __value);
643 ++iter;
644 return *this;
647 insert_iterator&
648 operator=(typename _Container::value_type&& __value)
650 iter = container->insert(iter, std::move(__value));
651 ++iter;
652 return *this;
654 #endif
656 /// Simply returns *this.
657 insert_iterator&
658 operator*()
659 { return *this; }
661 /// Simply returns *this. (This %iterator does not @a move.)
662 insert_iterator&
663 operator++()
664 { return *this; }
666 /// Simply returns *this. (This %iterator does not @a move.)
667 insert_iterator&
668 operator++(int)
669 { return *this; }
673 * @param __x A container of arbitrary type.
674 * @return An instance of insert_iterator working on @p __x.
676 * This wrapper function helps in creating insert_iterator instances.
677 * Typing the name of the %iterator requires knowing the precise full
678 * type of the container, which can be tedious and impedes generic
679 * programming. Using this function lets you take advantage of automatic
680 * template parameter deduction, making the compiler match the correct
681 * types for you.
683 template<typename _Container, typename _Iterator>
684 inline insert_iterator<_Container>
685 inserter(_Container& __x, _Iterator __i)
687 return insert_iterator<_Container>(__x,
688 typename _Container::iterator(__i));
691 // @} group iterators
693 _GLIBCXX_END_NAMESPACE_VERSION
694 } // namespace
696 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
698 _GLIBCXX_BEGIN_NAMESPACE_VERSION
700 // This iterator adapter is @a normal in the sense that it does not
701 // change the semantics of any of the operators of its iterator
702 // parameter. Its primary purpose is to convert an iterator that is
703 // not a class, e.g. a pointer, into an iterator that is a class.
704 // The _Container parameter exists solely so that different containers
705 // using this template can instantiate different types, even if the
706 // _Iterator parameter is the same.
707 using std::iterator_traits;
708 using std::iterator;
709 template<typename _Iterator, typename _Container>
710 class __normal_iterator
712 protected:
713 _Iterator _M_current;
715 typedef iterator_traits<_Iterator> __traits_type;
717 public:
718 typedef _Iterator iterator_type;
719 typedef typename __traits_type::iterator_category iterator_category;
720 typedef typename __traits_type::value_type value_type;
721 typedef typename __traits_type::difference_type difference_type;
722 typedef typename __traits_type::reference reference;
723 typedef typename __traits_type::pointer pointer;
725 _GLIBCXX_CONSTEXPR __normal_iterator() : _M_current(_Iterator()) { }
727 explicit
728 __normal_iterator(const _Iterator& __i) : _M_current(__i) { }
730 // Allow iterator to const_iterator conversion
731 template<typename _Iter>
732 __normal_iterator(const __normal_iterator<_Iter,
733 typename __enable_if<
734 (std::__are_same<_Iter, typename _Container::pointer>::__value),
735 _Container>::__type>& __i)
736 : _M_current(__i.base()) { }
738 // Forward iterator requirements
739 reference
740 operator*() const
741 { return *_M_current; }
743 pointer
744 operator->() const
745 { return _M_current; }
747 __normal_iterator&
748 operator++()
750 ++_M_current;
751 return *this;
754 __normal_iterator
755 operator++(int)
756 { return __normal_iterator(_M_current++); }
758 // Bidirectional iterator requirements
759 __normal_iterator&
760 operator--()
762 --_M_current;
763 return *this;
766 __normal_iterator
767 operator--(int)
768 { return __normal_iterator(_M_current--); }
770 // Random access iterator requirements
771 reference
772 operator[](const difference_type& __n) const
773 { return _M_current[__n]; }
775 __normal_iterator&
776 operator+=(const difference_type& __n)
777 { _M_current += __n; return *this; }
779 __normal_iterator
780 operator+(const difference_type& __n) const
781 { return __normal_iterator(_M_current + __n); }
783 __normal_iterator&
784 operator-=(const difference_type& __n)
785 { _M_current -= __n; return *this; }
787 __normal_iterator
788 operator-(const difference_type& __n) const
789 { return __normal_iterator(_M_current - __n); }
791 const _Iterator&
792 base() const
793 { return _M_current; }
796 // Note: In what follows, the left- and right-hand-side iterators are
797 // allowed to vary in types (conceptually in cv-qualification) so that
798 // comparison between cv-qualified and non-cv-qualified iterators be
799 // valid. However, the greedy and unfriendly operators in std::rel_ops
800 // will make overload resolution ambiguous (when in scope) if we don't
801 // provide overloads whose operands are of the same type. Can someone
802 // remind me what generic programming is about? -- Gaby
804 // Forward iterator requirements
805 template<typename _IteratorL, typename _IteratorR, typename _Container>
806 inline bool
807 operator==(const __normal_iterator<_IteratorL, _Container>& __lhs,
808 const __normal_iterator<_IteratorR, _Container>& __rhs)
809 { return __lhs.base() == __rhs.base(); }
811 template<typename _Iterator, typename _Container>
812 inline bool
813 operator==(const __normal_iterator<_Iterator, _Container>& __lhs,
814 const __normal_iterator<_Iterator, _Container>& __rhs)
815 { return __lhs.base() == __rhs.base(); }
817 template<typename _IteratorL, typename _IteratorR, typename _Container>
818 inline bool
819 operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs,
820 const __normal_iterator<_IteratorR, _Container>& __rhs)
821 { return __lhs.base() != __rhs.base(); }
823 template<typename _Iterator, typename _Container>
824 inline bool
825 operator!=(const __normal_iterator<_Iterator, _Container>& __lhs,
826 const __normal_iterator<_Iterator, _Container>& __rhs)
827 { return __lhs.base() != __rhs.base(); }
829 // Random access iterator requirements
830 template<typename _IteratorL, typename _IteratorR, typename _Container>
831 inline bool
832 operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
833 const __normal_iterator<_IteratorR, _Container>& __rhs)
834 { return __lhs.base() < __rhs.base(); }
836 template<typename _Iterator, typename _Container>
837 inline bool
838 operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
839 const __normal_iterator<_Iterator, _Container>& __rhs)
840 { return __lhs.base() < __rhs.base(); }
842 template<typename _IteratorL, typename _IteratorR, typename _Container>
843 inline bool
844 operator>(const __normal_iterator<_IteratorL, _Container>& __lhs,
845 const __normal_iterator<_IteratorR, _Container>& __rhs)
846 { return __lhs.base() > __rhs.base(); }
848 template<typename _Iterator, typename _Container>
849 inline bool
850 operator>(const __normal_iterator<_Iterator, _Container>& __lhs,
851 const __normal_iterator<_Iterator, _Container>& __rhs)
852 { return __lhs.base() > __rhs.base(); }
854 template<typename _IteratorL, typename _IteratorR, typename _Container>
855 inline bool
856 operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs,
857 const __normal_iterator<_IteratorR, _Container>& __rhs)
858 { return __lhs.base() <= __rhs.base(); }
860 template<typename _Iterator, typename _Container>
861 inline bool
862 operator<=(const __normal_iterator<_Iterator, _Container>& __lhs,
863 const __normal_iterator<_Iterator, _Container>& __rhs)
864 { return __lhs.base() <= __rhs.base(); }
866 template<typename _IteratorL, typename _IteratorR, typename _Container>
867 inline bool
868 operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs,
869 const __normal_iterator<_IteratorR, _Container>& __rhs)
870 { return __lhs.base() >= __rhs.base(); }
872 template<typename _Iterator, typename _Container>
873 inline bool
874 operator>=(const __normal_iterator<_Iterator, _Container>& __lhs,
875 const __normal_iterator<_Iterator, _Container>& __rhs)
876 { return __lhs.base() >= __rhs.base(); }
878 // _GLIBCXX_RESOLVE_LIB_DEFECTS
879 // According to the resolution of DR179 not only the various comparison
880 // operators but also operator- must accept mixed iterator/const_iterator
881 // parameters.
882 template<typename _IteratorL, typename _IteratorR, typename _Container>
883 #if __cplusplus >= 201103L
884 // DR 685.
885 inline auto
886 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
887 const __normal_iterator<_IteratorR, _Container>& __rhs)
888 -> decltype(__lhs.base() - __rhs.base())
889 #else
890 inline typename __normal_iterator<_IteratorL, _Container>::difference_type
891 operator-(const __normal_iterator<_IteratorL, _Container>& __lhs,
892 const __normal_iterator<_IteratorR, _Container>& __rhs)
893 #endif
894 { return __lhs.base() - __rhs.base(); }
896 template<typename _Iterator, typename _Container>
897 inline typename __normal_iterator<_Iterator, _Container>::difference_type
898 operator-(const __normal_iterator<_Iterator, _Container>& __lhs,
899 const __normal_iterator<_Iterator, _Container>& __rhs)
900 { return __lhs.base() - __rhs.base(); }
902 template<typename _Iterator, typename _Container>
903 inline __normal_iterator<_Iterator, _Container>
904 operator+(typename __normal_iterator<_Iterator, _Container>::difference_type
905 __n, const __normal_iterator<_Iterator, _Container>& __i)
906 { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); }
908 _GLIBCXX_END_NAMESPACE_VERSION
909 } // namespace
911 #if __cplusplus >= 201103L
913 namespace std _GLIBCXX_VISIBILITY(default)
915 _GLIBCXX_BEGIN_NAMESPACE_VERSION
918 * @addtogroup iterators
919 * @{
922 // 24.4.3 Move iterators
924 * Class template move_iterator is an iterator adapter with the same
925 * behavior as the underlying iterator except that its dereference
926 * operator implicitly converts the value returned by the underlying
927 * iterator's dereference operator to an rvalue reference. Some
928 * generic algorithms can be called with move iterators to replace
929 * copying with moving.
931 template<typename _Iterator>
932 class move_iterator
934 protected:
935 _Iterator _M_current;
937 typedef iterator_traits<_Iterator> __traits_type;
939 public:
940 typedef _Iterator iterator_type;
941 typedef typename __traits_type::iterator_category iterator_category;
942 typedef typename __traits_type::value_type value_type;
943 typedef typename __traits_type::difference_type difference_type;
944 // NB: DR 680.
945 typedef _Iterator pointer;
946 typedef value_type&& reference;
948 move_iterator()
949 : _M_current() { }
951 explicit
952 move_iterator(iterator_type __i)
953 : _M_current(__i) { }
955 template<typename _Iter>
956 move_iterator(const move_iterator<_Iter>& __i)
957 : _M_current(__i.base()) { }
959 iterator_type
960 base() const
961 { return _M_current; }
963 reference
964 operator*() const
965 { return std::move(*_M_current); }
967 pointer
968 operator->() const
969 { return _M_current; }
971 move_iterator&
972 operator++()
974 ++_M_current;
975 return *this;
978 move_iterator
979 operator++(int)
981 move_iterator __tmp = *this;
982 ++_M_current;
983 return __tmp;
986 move_iterator&
987 operator--()
989 --_M_current;
990 return *this;
993 move_iterator
994 operator--(int)
996 move_iterator __tmp = *this;
997 --_M_current;
998 return __tmp;
1001 move_iterator
1002 operator+(difference_type __n) const
1003 { return move_iterator(_M_current + __n); }
1005 move_iterator&
1006 operator+=(difference_type __n)
1008 _M_current += __n;
1009 return *this;
1012 move_iterator
1013 operator-(difference_type __n) const
1014 { return move_iterator(_M_current - __n); }
1016 move_iterator&
1017 operator-=(difference_type __n)
1019 _M_current -= __n;
1020 return *this;
1023 reference
1024 operator[](difference_type __n) const
1025 { return std::move(_M_current[__n]); }
1028 // Note: See __normal_iterator operators note from Gaby to understand
1029 // why there are always 2 versions for most of the move_iterator
1030 // operators.
1031 template<typename _IteratorL, typename _IteratorR>
1032 inline bool
1033 operator==(const move_iterator<_IteratorL>& __x,
1034 const move_iterator<_IteratorR>& __y)
1035 { return __x.base() == __y.base(); }
1037 template<typename _Iterator>
1038 inline bool
1039 operator==(const move_iterator<_Iterator>& __x,
1040 const move_iterator<_Iterator>& __y)
1041 { return __x.base() == __y.base(); }
1043 template<typename _IteratorL, typename _IteratorR>
1044 inline bool
1045 operator!=(const move_iterator<_IteratorL>& __x,
1046 const move_iterator<_IteratorR>& __y)
1047 { return !(__x == __y); }
1049 template<typename _Iterator>
1050 inline bool
1051 operator!=(const move_iterator<_Iterator>& __x,
1052 const move_iterator<_Iterator>& __y)
1053 { return !(__x == __y); }
1055 template<typename _IteratorL, typename _IteratorR>
1056 inline bool
1057 operator<(const move_iterator<_IteratorL>& __x,
1058 const move_iterator<_IteratorR>& __y)
1059 { return __x.base() < __y.base(); }
1061 template<typename _Iterator>
1062 inline bool
1063 operator<(const move_iterator<_Iterator>& __x,
1064 const move_iterator<_Iterator>& __y)
1065 { return __x.base() < __y.base(); }
1067 template<typename _IteratorL, typename _IteratorR>
1068 inline bool
1069 operator<=(const move_iterator<_IteratorL>& __x,
1070 const move_iterator<_IteratorR>& __y)
1071 { return !(__y < __x); }
1073 template<typename _Iterator>
1074 inline bool
1075 operator<=(const move_iterator<_Iterator>& __x,
1076 const move_iterator<_Iterator>& __y)
1077 { return !(__y < __x); }
1079 template<typename _IteratorL, typename _IteratorR>
1080 inline bool
1081 operator>(const move_iterator<_IteratorL>& __x,
1082 const move_iterator<_IteratorR>& __y)
1083 { return __y < __x; }
1085 template<typename _Iterator>
1086 inline bool
1087 operator>(const move_iterator<_Iterator>& __x,
1088 const move_iterator<_Iterator>& __y)
1089 { return __y < __x; }
1091 template<typename _IteratorL, typename _IteratorR>
1092 inline bool
1093 operator>=(const move_iterator<_IteratorL>& __x,
1094 const move_iterator<_IteratorR>& __y)
1095 { return !(__x < __y); }
1097 template<typename _Iterator>
1098 inline bool
1099 operator>=(const move_iterator<_Iterator>& __x,
1100 const move_iterator<_Iterator>& __y)
1101 { return !(__x < __y); }
1103 // DR 685.
1104 template<typename _IteratorL, typename _IteratorR>
1105 inline auto
1106 operator-(const move_iterator<_IteratorL>& __x,
1107 const move_iterator<_IteratorR>& __y)
1108 -> decltype(__x.base() - __y.base())
1109 { return __x.base() - __y.base(); }
1111 template<typename _Iterator>
1112 inline auto
1113 operator-(const move_iterator<_Iterator>& __x,
1114 const move_iterator<_Iterator>& __y)
1115 -> decltype(__x.base() - __y.base())
1116 { return __x.base() - __y.base(); }
1118 template<typename _Iterator>
1119 inline move_iterator<_Iterator>
1120 operator+(typename move_iterator<_Iterator>::difference_type __n,
1121 const move_iterator<_Iterator>& __x)
1122 { return __x + __n; }
1124 template<typename _Iterator>
1125 inline move_iterator<_Iterator>
1126 make_move_iterator(_Iterator __i)
1127 { return move_iterator<_Iterator>(__i); }
1129 template<typename _Iterator, typename _ReturnType
1130 = typename conditional<__move_if_noexcept_cond
1131 <typename iterator_traits<_Iterator>::value_type>::value,
1132 _Iterator, move_iterator<_Iterator>>::type>
1133 inline _ReturnType
1134 __make_move_if_noexcept_iterator(_Iterator __i)
1135 { return _ReturnType(__i); }
1137 // @} group iterators
1139 _GLIBCXX_END_NAMESPACE_VERSION
1140 } // namespace
1142 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) std::make_move_iterator(_Iter)
1143 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) \
1144 std::__make_move_if_noexcept_iterator(_Iter)
1145 #else
1146 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
1147 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter)
1148 #endif // C++11
1150 #endif