1 // Iterators -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
30 * Hewlett-Packard Company
32 * Permission to use, copy, modify, distribute and sell this software
33 * and its documentation for any purpose is hereby granted without fee,
34 * provided that the above copyright notice appear in all copies and
35 * that both that copyright notice and this permission notice appear
36 * in supporting documentation. Hewlett-Packard Company makes no
37 * representations about the suitability of this software for any
38 * purpose. It is provided "as is" without express or implied warranty.
41 * Copyright (c) 1996-1998
42 * Silicon Graphics Computer Systems, Inc.
44 * Permission to use, copy, modify, distribute and sell this software
45 * and its documentation for any purpose is hereby granted without fee,
46 * provided that the above copyright notice appear in all copies and
47 * that both that copyright notice and this permission notice appear
48 * in supporting documentation. Silicon Graphics makes no
49 * representations about the suitability of this software for any
50 * purpose. It is provided "as is" without express or implied warranty.
53 /** @file bits/stl_iterator.h
54 * This is an internal header file, included by other library headers.
55 * Do not attempt to use it directly. @headername{iterator}
57 * This file implements reverse_iterator, back_insert_iterator,
58 * front_insert_iterator, insert_iterator, __normal_iterator, and their
59 * supporting functions and overloaded operators.
62 #ifndef _STL_ITERATOR_H
63 #define _STL_ITERATOR_H 1
65 #include <bits/cpp_type_traits.h>
66 #include <ext/type_traits.h>
67 #include <bits/move.h>
69 namespace std
_GLIBCXX_VISIBILITY(default)
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
74 * @addtogroup iterators
78 // 24.4.1 Reverse iterators
80 * Bidirectional and random access iterators have corresponding reverse
81 * %iterator adaptors that iterate through the data structure in the
82 * opposite direction. They have the same signatures as the corresponding
83 * iterators. The fundamental relation between a reverse %iterator and its
84 * corresponding %iterator @c i is established by the identity:
86 * &*(reverse_iterator(i)) == &*(i - 1)
89 * <em>This mapping is dictated by the fact that while there is always a
90 * pointer past the end of an array, there might not be a valid pointer
91 * before the beginning of an array.</em> [24.4.1]/1,2
93 * Reverse iterators can be tricky and surprising at first. Their
94 * semantics make sense, however, and the trickiness is a side effect of
95 * the requirement that the iterators must be safe.
97 template<typename _Iterator
>
98 class reverse_iterator
99 : public iterator
<typename iterator_traits
<_Iterator
>::iterator_category
,
100 typename iterator_traits
<_Iterator
>::value_type
,
101 typename iterator_traits
<_Iterator
>::difference_type
,
102 typename iterator_traits
<_Iterator
>::pointer
,
103 typename iterator_traits
<_Iterator
>::reference
>
108 typedef iterator_traits
<_Iterator
> __traits_type
;
111 typedef _Iterator iterator_type
;
112 typedef typename
__traits_type::difference_type difference_type
;
113 typedef typename
__traits_type::pointer pointer
;
114 typedef typename
__traits_type::reference reference
;
117 * The default constructor 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.
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.
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
164 _Iterator __tmp
= current
;
169 * @return A pointer to the value at @c --current
171 * This requires that @c --current is dereferenceable.
175 { return &(operator*()); }
180 * Decrements the underlying iterator.
190 * @return The original value of @c *this
192 * Decrements the underlying iterator.
197 reverse_iterator __tmp
= *this;
205 * Increments the underlying iterator.
215 * @return A reverse_iterator with the previous value of @c *this
217 * Increments the underlying iterator.
222 reverse_iterator __tmp
= *this;
228 * @return A reverse_iterator that refers to @c current - @a __n
230 * The underlying iterator must be a Random Access Iterator.
233 operator+(difference_type __n
) const
234 { return reverse_iterator(current
- __n
); }
239 * Moves the underlying iterator backwards @a __n steps.
240 * The underlying iterator must be a Random Access Iterator.
243 operator+=(difference_type __n
)
250 * @return A reverse_iterator that refers to @c current - @a __n
252 * The underlying iterator must be a Random Access Iterator.
255 operator-(difference_type __n
) const
256 { return reverse_iterator(current
+ __n
); }
261 * Moves the underlying iterator forwards @a __n steps.
262 * The underlying iterator must be a Random Access Iterator.
265 operator-=(difference_type __n
)
272 * @return The value at @c current - @a __n - 1
274 * The underlying iterator must be a Random Access Iterator.
277 operator[](difference_type __n
) const
278 { return *(*this + __n
); }
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
>
293 operator==(const reverse_iterator
<_Iterator
>& __x
,
294 const reverse_iterator
<_Iterator
>& __y
)
295 { return __x
.base() == __y
.base(); }
297 template<typename _Iterator
>
299 operator<(const reverse_iterator
<_Iterator
>& __x
,
300 const reverse_iterator
<_Iterator
>& __y
)
301 { return __y
.base() < __x
.base(); }
303 template<typename _Iterator
>
305 operator!=(const reverse_iterator
<_Iterator
>& __x
,
306 const reverse_iterator
<_Iterator
>& __y
)
307 { return !(__x
== __y
); }
309 template<typename _Iterator
>
311 operator>(const reverse_iterator
<_Iterator
>& __x
,
312 const reverse_iterator
<_Iterator
>& __y
)
313 { return __y
< __x
; }
315 template<typename _Iterator
>
317 operator<=(const reverse_iterator
<_Iterator
>& __x
,
318 const reverse_iterator
<_Iterator
>& __y
)
319 { return !(__y
< __x
); }
321 template<typename _Iterator
>
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
>
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
>
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
>
355 operator!=(const reverse_iterator
<_IteratorL
>& __x
,
356 const reverse_iterator
<_IteratorR
>& __y
)
357 { return !(__x
== __y
); }
359 template<typename _IteratorL
, typename _IteratorR
>
361 operator>(const reverse_iterator
<_IteratorL
>& __x
,
362 const reverse_iterator
<_IteratorR
>& __y
)
363 { return __y
< __x
; }
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 !(__x
< __y
); }
377 template<typename _IteratorL
, typename _IteratorR
>
378 #ifdef __GXX_EXPERIMENTAL_CXX0X__
381 operator-(const reverse_iterator
<_IteratorL
>& __x
,
382 const reverse_iterator
<_IteratorR
>& __y
)
383 -> decltype(__y
.base() - __x
.base())
385 inline typename reverse_iterator
<_IteratorL
>::difference_type
386 operator-(const reverse_iterator
<_IteratorL
>& __x
,
387 const reverse_iterator
<_IteratorR
>& __y
)
389 { return __y
.base() - __x
.base(); }
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
400 * Tip: Using the back_inserter function to create these iterators can
403 template<typename _Container
>
404 class back_insert_iterator
405 : public iterator
<output_iterator_tag
, void, void, void, void>
408 _Container
* container
;
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.
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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
430 back_insert_iterator
&
431 operator=(typename
_Container::const_reference __value
)
433 container
->push_back(__value
);
437 back_insert_iterator
&
438 operator=(const typename
_Container::value_type
& __value
)
440 container
->push_back(__value
);
444 back_insert_iterator
&
445 operator=(typename
_Container::value_type
&& __value
)
447 container
->push_back(std::move(__value
));
452 /// Simply returns *this.
453 back_insert_iterator
&
457 /// Simply returns *this. (This %iterator does not @a move.)
458 back_insert_iterator
&
462 /// Simply returns *this. (This %iterator does not @a move.)
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
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
491 * Tip: Using the front_inserter function to create these iterators can
494 template<typename _Container
>
495 class front_insert_iterator
496 : public iterator
<output_iterator_tag
, void, void, void, void>
499 _Container
* container
;
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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
520 front_insert_iterator
&
521 operator=(typename
_Container::const_reference __value
)
523 container
->push_front(__value
);
527 front_insert_iterator
&
528 operator=(const typename
_Container::value_type
& __value
)
530 container
->push_front(__value
);
534 front_insert_iterator
&
535 operator=(typename
_Container::value_type
&& __value
)
537 container
->push_front(std::move(__value
));
542 /// Simply returns *this.
543 front_insert_iterator
&
547 /// Simply returns *this. (This %iterator does not @a move.)
548 front_insert_iterator
&
552 /// Simply returns *this. (This %iterator does not @a move.)
553 front_insert_iterator
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
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
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
588 template<typename _Container
>
589 class insert_iterator
590 : public iterator
<output_iterator_tag
, void, void, void, void>
593 _Container
* container
;
594 typename
_Container::iterator iter
;
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,
620 * // vector v contains A and Z
622 * insert_iterator i (v, ++v.begin());
627 * // vector v contains A, 1, 2, 3, and Z
630 #ifndef __GXX_EXPERIMENTAL_CXX0X__
632 operator=(typename
_Container::const_reference __value
)
634 iter
= container
->insert(iter
, __value
);
640 operator=(const typename
_Container::value_type
& __value
)
642 iter
= container
->insert(iter
, __value
);
648 operator=(typename
_Container::value_type
&& __value
)
650 iter
= container
->insert(iter
, std::move(__value
));
656 /// Simply returns *this.
661 /// Simply returns *this. (This %iterator does not @a move.)
666 /// Simply returns *this. (This %iterator does not @a move.)
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
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
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
;
709 template<typename _Iterator
, typename _Container
>
710 class __normal_iterator
713 _Iterator _M_current
;
715 typedef iterator_traits
<_Iterator
> __traits_type
;
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()) { }
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
741 { return *_M_current
; }
745 { return _M_current
; }
756 { return __normal_iterator(_M_current
++); }
758 // Bidirectional iterator requirements
768 { return __normal_iterator(_M_current
--); }
770 // Random access iterator requirements
772 operator[](const difference_type
& __n
) const
773 { return _M_current
[__n
]; }
776 operator+=(const difference_type
& __n
)
777 { _M_current
+= __n
; return *this; }
780 operator+(const difference_type
& __n
) const
781 { return __normal_iterator(_M_current
+ __n
); }
784 operator-=(const difference_type
& __n
)
785 { _M_current
-= __n
; return *this; }
788 operator-(const difference_type
& __n
) const
789 { return __normal_iterator(_M_current
- __n
); }
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
>
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
>
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
>
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
>
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
>
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
>
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
>
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
>
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
>
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
>
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
>
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
>
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
882 template<typename _IteratorL
, typename _IteratorR
, typename _Container
>
883 #ifdef __GXX_EXPERIMENTAL_CXX0X__
886 operator-(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
887 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
888 -> decltype(__lhs
.base() - __rhs
.base())
890 inline typename __normal_iterator
<_IteratorL
, _Container
>::difference_type
891 operator-(const __normal_iterator
<_IteratorL
, _Container
>& __lhs
,
892 const __normal_iterator
<_IteratorR
, _Container
>& __rhs
)
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
911 #ifdef __GXX_EXPERIMENTAL_CXX0X__
913 namespace std
_GLIBCXX_VISIBILITY(default)
915 _GLIBCXX_BEGIN_NAMESPACE_VERSION
918 * @addtogroup iterators
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
>
935 _Iterator _M_current
;
937 typedef iterator_traits
<_Iterator
> __traits_type
;
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
;
945 typedef _Iterator pointer
;
946 typedef value_type
&& reference
;
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()) { }
961 { return _M_current
; }
965 { return std::move(*_M_current
); }
969 { return _M_current
; }
981 move_iterator __tmp
= *this;
996 move_iterator __tmp
= *this;
1002 operator+(difference_type __n
) const
1003 { return move_iterator(_M_current
+ __n
); }
1006 operator+=(difference_type __n
)
1013 operator-(difference_type __n
) const
1014 { return move_iterator(_M_current
- __n
); }
1017 operator-=(difference_type __n
)
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
1031 template<typename _IteratorL
, typename _IteratorR
>
1033 operator==(const move_iterator
<_IteratorL
>& __x
,
1034 const move_iterator
<_IteratorR
>& __y
)
1035 { return __x
.base() == __y
.base(); }
1037 template<typename _Iterator
>
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
>
1045 operator!=(const move_iterator
<_IteratorL
>& __x
,
1046 const move_iterator
<_IteratorR
>& __y
)
1047 { return !(__x
== __y
); }
1049 template<typename _Iterator
>
1051 operator!=(const move_iterator
<_Iterator
>& __x
,
1052 const move_iterator
<_Iterator
>& __y
)
1053 { return !(__x
== __y
); }
1055 template<typename _IteratorL
, typename _IteratorR
>
1057 operator<(const move_iterator
<_IteratorL
>& __x
,
1058 const move_iterator
<_IteratorR
>& __y
)
1059 { return __x
.base() < __y
.base(); }
1061 template<typename _Iterator
>
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
>
1069 operator<=(const move_iterator
<_IteratorL
>& __x
,
1070 const move_iterator
<_IteratorR
>& __y
)
1071 { return !(__y
< __x
); }
1073 template<typename _Iterator
>
1075 operator<=(const move_iterator
<_Iterator
>& __x
,
1076 const move_iterator
<_Iterator
>& __y
)
1077 { return !(__y
< __x
); }
1079 template<typename _IteratorL
, typename _IteratorR
>
1081 operator>(const move_iterator
<_IteratorL
>& __x
,
1082 const move_iterator
<_IteratorR
>& __y
)
1083 { return __y
< __x
; }
1085 template<typename _Iterator
>
1087 operator>(const move_iterator
<_Iterator
>& __x
,
1088 const move_iterator
<_Iterator
>& __y
)
1089 { return __y
< __x
; }
1091 template<typename _IteratorL
, typename _IteratorR
>
1093 operator>=(const move_iterator
<_IteratorL
>& __x
,
1094 const move_iterator
<_IteratorR
>& __y
)
1095 { return !(__x
< __y
); }
1097 template<typename _Iterator
>
1099 operator>=(const move_iterator
<_Iterator
>& __x
,
1100 const move_iterator
<_Iterator
>& __y
)
1101 { return !(__x
< __y
); }
1104 template<typename _IteratorL
, typename _IteratorR
>
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
>
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
>
1134 __make_move_if_noexcept_iterator(_Iterator __i
)
1135 { return _ReturnType(__i
); }
1137 // @} group iterators
1139 _GLIBCXX_END_NAMESPACE_VERSION
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)
1146 #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter) (_Iter)
1147 #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter) (_Iter)
1148 #endif // __GXX_EXPERIMENTAL_CXX0X__