1 // Raw memory manipulators -*- C++ -*-
3 // Copyright (C) 2001-2020 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,1997
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_uninitialized.h
52 * This is an internal header file, included by other library headers.
53 * Do not attempt to use it directly. @headername{memory}
56 #ifndef _STL_UNINITIALIZED_H
57 #define _STL_UNINITIALIZED_H 1
59 #if __cplusplus > 201402L
60 #include <bits/stl_pair.h>
63 #if __cplusplus >= 201103L
64 #include <type_traits>
67 #include <ext/alloc_traits.h>
69 namespace std
_GLIBCXX_VISIBILITY(default)
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
73 /** @addtogroup memory
77 /// @cond undocumented
79 template<bool _TrivialValueTypes
>
80 struct __uninitialized_copy
82 template<typename _InputIterator
, typename _ForwardIterator
>
83 static _ForwardIterator
84 __uninit_copy(_InputIterator __first
, _InputIterator __last
,
85 _ForwardIterator __result
)
87 _ForwardIterator __cur
= __result
;
90 for (; __first
!= __last
; ++__first
, (void)++__cur
)
91 std::_Construct(std::__addressof(*__cur
), *__first
);
96 std::_Destroy(__result
, __cur
);
97 __throw_exception_again
;
103 struct __uninitialized_copy
<true>
105 template<typename _InputIterator
, typename _ForwardIterator
>
106 static _ForwardIterator
107 __uninit_copy(_InputIterator __first
, _InputIterator __last
,
108 _ForwardIterator __result
)
109 { return std::copy(__first
, __last
, __result
); }
115 * @brief Copies the range [first,last) into result.
116 * @param __first An input iterator.
117 * @param __last An input iterator.
118 * @param __result An output iterator.
119 * @return __result + (__first - __last)
121 * Like copy(), but does not require an initialized output range.
123 template<typename _InputIterator
, typename _ForwardIterator
>
124 inline _ForwardIterator
125 uninitialized_copy(_InputIterator __first
, _InputIterator __last
,
126 _ForwardIterator __result
)
128 typedef typename iterator_traits
<_InputIterator
>::value_type
130 typedef typename iterator_traits
<_ForwardIterator
>::value_type
132 #if __cplusplus < 201103L
133 const bool __assignable
= true;
135 // Trivial types can have deleted copy constructor, but the std::copy
136 // optimization that uses memmove would happily "copy" them anyway.
137 static_assert(is_constructible
<_ValueType2
, decltype(*__first
)>::value
,
138 "result type must be constructible from value type of input range");
140 typedef typename iterator_traits
<_InputIterator
>::reference _RefType1
;
141 typedef typename iterator_traits
<_ForwardIterator
>::reference _RefType2
;
142 // Trivial types can have deleted assignment, so using std::copy
143 // would be ill-formed. Require assignability before using std::copy:
144 const bool __assignable
= is_assignable
<_RefType2
, _RefType1
>::value
;
147 return std::__uninitialized_copy
<__is_trivial(_ValueType1
)
148 && __is_trivial(_ValueType2
)
150 __uninit_copy(__first
, __last
, __result
);
153 /// @cond undocumented
155 template<bool _TrivialValueType
>
156 struct __uninitialized_fill
158 template<typename _ForwardIterator
, typename _Tp
>
160 __uninit_fill(_ForwardIterator __first
, _ForwardIterator __last
,
163 _ForwardIterator __cur
= __first
;
166 for (; __cur
!= __last
; ++__cur
)
167 std::_Construct(std::__addressof(*__cur
), __x
);
171 std::_Destroy(__first
, __cur
);
172 __throw_exception_again
;
178 struct __uninitialized_fill
<true>
180 template<typename _ForwardIterator
, typename _Tp
>
182 __uninit_fill(_ForwardIterator __first
, _ForwardIterator __last
,
184 { std::fill(__first
, __last
, __x
); }
190 * @brief Copies the value x into the range [first,last).
191 * @param __first An input iterator.
192 * @param __last An input iterator.
193 * @param __x The source value.
196 * Like fill(), but does not require an initialized output range.
198 template<typename _ForwardIterator
, typename _Tp
>
200 uninitialized_fill(_ForwardIterator __first
, _ForwardIterator __last
,
203 typedef typename iterator_traits
<_ForwardIterator
>::value_type
205 #if __cplusplus < 201103L
206 const bool __assignable
= true;
208 // Trivial types can have deleted copy constructor, but the std::fill
209 // optimization that uses memmove would happily "copy" them anyway.
210 static_assert(is_constructible
<_ValueType
, const _Tp
&>::value
,
211 "result type must be constructible from input type");
213 // Trivial types can have deleted assignment, so using std::fill
214 // would be ill-formed. Require assignability before using std::fill:
215 const bool __assignable
= is_copy_assignable
<_ValueType
>::value
;
218 std::__uninitialized_fill
<__is_trivial(_ValueType
) && __assignable
>::
219 __uninit_fill(__first
, __last
, __x
);
222 /// @cond undocumented
224 template<bool _TrivialValueType
>
225 struct __uninitialized_fill_n
227 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
228 static _ForwardIterator
229 __uninit_fill_n(_ForwardIterator __first
, _Size __n
,
232 _ForwardIterator __cur
= __first
;
235 for (; __n
> 0; --__n
, (void) ++__cur
)
236 std::_Construct(std::__addressof(*__cur
), __x
);
241 std::_Destroy(__first
, __cur
);
242 __throw_exception_again
;
248 struct __uninitialized_fill_n
<true>
250 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
251 static _ForwardIterator
252 __uninit_fill_n(_ForwardIterator __first
, _Size __n
,
254 { return std::fill_n(__first
, __n
, __x
); }
259 // _GLIBCXX_RESOLVE_LIB_DEFECTS
260 // DR 1339. uninitialized_fill_n should return the end of its range
262 * @brief Copies the value x into the range [first,first+n).
263 * @param __first An input iterator.
264 * @param __n The number of copies to make.
265 * @param __x The source value.
268 * Like fill_n(), but does not require an initialized output range.
270 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
271 inline _ForwardIterator
272 uninitialized_fill_n(_ForwardIterator __first
, _Size __n
, const _Tp
& __x
)
274 typedef typename iterator_traits
<_ForwardIterator
>::value_type
276 #if __cplusplus < 201103L
277 const bool __assignable
= true;
279 // Trivial types can have deleted copy constructor, but the std::fill
280 // optimization that uses memmove would happily "copy" them anyway.
281 static_assert(is_constructible
<_ValueType
, const _Tp
&>::value
,
282 "result type must be constructible from input type");
284 // Trivial types can have deleted assignment, so using std::fill
285 // would be ill-formed. Require assignability before using std::fill:
286 const bool __assignable
= is_copy_assignable
<_ValueType
>::value
;
288 return __uninitialized_fill_n
<__is_trivial(_ValueType
) && __assignable
>::
289 __uninit_fill_n(__first
, __n
, __x
);
292 /// @cond undocumented
294 // Extensions: versions of uninitialized_copy, uninitialized_fill,
295 // and uninitialized_fill_n that take an allocator parameter.
296 // We dispatch back to the standard versions when we're given the
297 // default allocator. For nondefault allocators we do not use
298 // any of the POD optimizations.
300 template<typename _InputIterator
, typename _ForwardIterator
,
303 __uninitialized_copy_a(_InputIterator __first
, _InputIterator __last
,
304 _ForwardIterator __result
, _Allocator
& __alloc
)
306 _ForwardIterator __cur
= __result
;
309 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
310 for (; __first
!= __last
; ++__first
, (void)++__cur
)
311 __traits::construct(__alloc
, std::__addressof(*__cur
), *__first
);
316 std::_Destroy(__result
, __cur
, __alloc
);
317 __throw_exception_again
;
321 template<typename _InputIterator
, typename _ForwardIterator
, typename _Tp
>
322 inline _ForwardIterator
323 __uninitialized_copy_a(_InputIterator __first
, _InputIterator __last
,
324 _ForwardIterator __result
, allocator
<_Tp
>&)
325 { return std::uninitialized_copy(__first
, __last
, __result
); }
327 template<typename _InputIterator
, typename _ForwardIterator
,
329 inline _ForwardIterator
330 __uninitialized_move_a(_InputIterator __first
, _InputIterator __last
,
331 _ForwardIterator __result
, _Allocator
& __alloc
)
333 return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
334 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
),
338 template<typename _InputIterator
, typename _ForwardIterator
,
340 inline _ForwardIterator
341 __uninitialized_move_if_noexcept_a(_InputIterator __first
,
342 _InputIterator __last
,
343 _ForwardIterator __result
,
346 return std::__uninitialized_copy_a
347 (_GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__first
),
348 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__last
), __result
, __alloc
);
351 template<typename _ForwardIterator
, typename _Tp
, typename _Allocator
>
353 __uninitialized_fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
354 const _Tp
& __x
, _Allocator
& __alloc
)
356 _ForwardIterator __cur
= __first
;
359 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
360 for (; __cur
!= __last
; ++__cur
)
361 __traits::construct(__alloc
, std::__addressof(*__cur
), __x
);
365 std::_Destroy(__first
, __cur
, __alloc
);
366 __throw_exception_again
;
370 template<typename _ForwardIterator
, typename _Tp
, typename _Tp2
>
372 __uninitialized_fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
373 const _Tp
& __x
, allocator
<_Tp2
>&)
374 { std::uninitialized_fill(__first
, __last
, __x
); }
376 template<typename _ForwardIterator
, typename _Size
, typename _Tp
,
379 __uninitialized_fill_n_a(_ForwardIterator __first
, _Size __n
,
380 const _Tp
& __x
, _Allocator
& __alloc
)
382 _ForwardIterator __cur
= __first
;
385 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
386 for (; __n
> 0; --__n
, (void) ++__cur
)
387 __traits::construct(__alloc
, std::__addressof(*__cur
), __x
);
392 std::_Destroy(__first
, __cur
, __alloc
);
393 __throw_exception_again
;
397 template<typename _ForwardIterator
, typename _Size
, typename _Tp
,
399 inline _ForwardIterator
400 __uninitialized_fill_n_a(_ForwardIterator __first
, _Size __n
,
401 const _Tp
& __x
, allocator
<_Tp2
>&)
402 { return std::uninitialized_fill_n(__first
, __n
, __x
); }
405 // Extensions: __uninitialized_copy_move, __uninitialized_move_copy,
406 // __uninitialized_fill_move, __uninitialized_move_fill.
407 // All of these algorithms take a user-supplied allocator, which is used
408 // for construction and destruction.
410 // __uninitialized_copy_move
411 // Copies [first1, last1) into [result, result + (last1 - first1)), and
412 // move [first2, last2) into
413 // [result, result + (last1 - first1) + (last2 - first2)).
414 template<typename _InputIterator1
, typename _InputIterator2
,
415 typename _ForwardIterator
, typename _Allocator
>
416 inline _ForwardIterator
417 __uninitialized_copy_move(_InputIterator1 __first1
,
418 _InputIterator1 __last1
,
419 _InputIterator2 __first2
,
420 _InputIterator2 __last2
,
421 _ForwardIterator __result
,
424 _ForwardIterator __mid
= std::__uninitialized_copy_a(__first1
, __last1
,
429 return std::__uninitialized_move_a(__first2
, __last2
, __mid
, __alloc
);
433 std::_Destroy(__result
, __mid
, __alloc
);
434 __throw_exception_again
;
438 // __uninitialized_move_copy
439 // Moves [first1, last1) into [result, result + (last1 - first1)), and
440 // copies [first2, last2) into
441 // [result, result + (last1 - first1) + (last2 - first2)).
442 template<typename _InputIterator1
, typename _InputIterator2
,
443 typename _ForwardIterator
, typename _Allocator
>
444 inline _ForwardIterator
445 __uninitialized_move_copy(_InputIterator1 __first1
,
446 _InputIterator1 __last1
,
447 _InputIterator2 __first2
,
448 _InputIterator2 __last2
,
449 _ForwardIterator __result
,
452 _ForwardIterator __mid
= std::__uninitialized_move_a(__first1
, __last1
,
457 return std::__uninitialized_copy_a(__first2
, __last2
, __mid
, __alloc
);
461 std::_Destroy(__result
, __mid
, __alloc
);
462 __throw_exception_again
;
466 // __uninitialized_fill_move
467 // Fills [result, mid) with x, and moves [first, last) into
468 // [mid, mid + (last - first)).
469 template<typename _ForwardIterator
, typename _Tp
, typename _InputIterator
,
471 inline _ForwardIterator
472 __uninitialized_fill_move(_ForwardIterator __result
, _ForwardIterator __mid
,
473 const _Tp
& __x
, _InputIterator __first
,
474 _InputIterator __last
, _Allocator
& __alloc
)
476 std::__uninitialized_fill_a(__result
, __mid
, __x
, __alloc
);
479 return std::__uninitialized_move_a(__first
, __last
, __mid
, __alloc
);
483 std::_Destroy(__result
, __mid
, __alloc
);
484 __throw_exception_again
;
488 // __uninitialized_move_fill
489 // Moves [first1, last1) into [first2, first2 + (last1 - first1)), and
490 // fills [first2 + (last1 - first1), last2) with x.
491 template<typename _InputIterator
, typename _ForwardIterator
, typename _Tp
,
494 __uninitialized_move_fill(_InputIterator __first1
, _InputIterator __last1
,
495 _ForwardIterator __first2
,
496 _ForwardIterator __last2
, const _Tp
& __x
,
499 _ForwardIterator __mid2
= std::__uninitialized_move_a(__first1
, __last1
,
504 std::__uninitialized_fill_a(__mid2
, __last2
, __x
, __alloc
);
508 std::_Destroy(__first2
, __mid2
, __alloc
);
509 __throw_exception_again
;
515 #if __cplusplus >= 201103L
516 /// @cond undocumented
518 // Extensions: __uninitialized_default, __uninitialized_default_n,
519 // __uninitialized_default_a, __uninitialized_default_n_a.
521 template<bool _TrivialValueType
>
522 struct __uninitialized_default_1
524 template<typename _ForwardIterator
>
526 __uninit_default(_ForwardIterator __first
, _ForwardIterator __last
)
528 _ForwardIterator __cur
= __first
;
531 for (; __cur
!= __last
; ++__cur
)
532 std::_Construct(std::__addressof(*__cur
));
536 std::_Destroy(__first
, __cur
);
537 __throw_exception_again
;
543 struct __uninitialized_default_1
<true>
545 template<typename _ForwardIterator
>
547 __uninit_default(_ForwardIterator __first
, _ForwardIterator __last
)
549 typedef typename iterator_traits
<_ForwardIterator
>::value_type
552 std::fill(__first
, __last
, _ValueType());
556 template<bool _TrivialValueType
>
557 struct __uninitialized_default_n_1
559 template<typename _ForwardIterator
, typename _Size
>
560 static _ForwardIterator
561 __uninit_default_n(_ForwardIterator __first
, _Size __n
)
563 _ForwardIterator __cur
= __first
;
566 for (; __n
> 0; --__n
, (void) ++__cur
)
567 std::_Construct(std::__addressof(*__cur
));
572 std::_Destroy(__first
, __cur
);
573 __throw_exception_again
;
579 struct __uninitialized_default_n_1
<true>
581 template<typename _ForwardIterator
, typename _Size
>
582 static _ForwardIterator
583 __uninit_default_n(_ForwardIterator __first
, _Size __n
)
585 typedef typename iterator_traits
<_ForwardIterator
>::value_type
588 return std::fill_n(__first
, __n
, _ValueType());
592 // __uninitialized_default
593 // Fills [first, last) with std::distance(first, last) default
594 // constructed value_types(s).
595 template<typename _ForwardIterator
>
597 __uninitialized_default(_ForwardIterator __first
,
598 _ForwardIterator __last
)
600 typedef typename iterator_traits
<_ForwardIterator
>::value_type
602 // trivial types can have deleted assignment
603 const bool __assignable
= is_copy_assignable
<_ValueType
>::value
;
605 std::__uninitialized_default_1
<__is_trivial(_ValueType
)
607 __uninit_default(__first
, __last
);
610 // __uninitialized_default_n
611 // Fills [first, first + n) with n default constructed value_type(s).
612 template<typename _ForwardIterator
, typename _Size
>
613 inline _ForwardIterator
614 __uninitialized_default_n(_ForwardIterator __first
, _Size __n
)
616 typedef typename iterator_traits
<_ForwardIterator
>::value_type
618 // trivial types can have deleted assignment
619 const bool __assignable
= is_copy_assignable
<_ValueType
>::value
;
621 return __uninitialized_default_n_1
<__is_trivial(_ValueType
)
623 __uninit_default_n(__first
, __n
);
627 // __uninitialized_default_a
628 // Fills [first, last) with std::distance(first, last) default
629 // constructed value_types(s), constructed with the allocator alloc.
630 template<typename _ForwardIterator
, typename _Allocator
>
632 __uninitialized_default_a(_ForwardIterator __first
,
633 _ForwardIterator __last
,
636 _ForwardIterator __cur
= __first
;
639 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
640 for (; __cur
!= __last
; ++__cur
)
641 __traits::construct(__alloc
, std::__addressof(*__cur
));
645 std::_Destroy(__first
, __cur
, __alloc
);
646 __throw_exception_again
;
650 template<typename _ForwardIterator
, typename _Tp
>
652 __uninitialized_default_a(_ForwardIterator __first
,
653 _ForwardIterator __last
,
655 { std::__uninitialized_default(__first
, __last
); }
658 // __uninitialized_default_n_a
659 // Fills [first, first + n) with n default constructed value_types(s),
660 // constructed with the allocator alloc.
661 template<typename _ForwardIterator
, typename _Size
, typename _Allocator
>
663 __uninitialized_default_n_a(_ForwardIterator __first
, _Size __n
,
666 _ForwardIterator __cur
= __first
;
669 typedef __gnu_cxx::__alloc_traits
<_Allocator
> __traits
;
670 for (; __n
> 0; --__n
, (void) ++__cur
)
671 __traits::construct(__alloc
, std::__addressof(*__cur
));
676 std::_Destroy(__first
, __cur
, __alloc
);
677 __throw_exception_again
;
681 template<typename _ForwardIterator
, typename _Size
, typename _Tp
>
682 inline _ForwardIterator
683 __uninitialized_default_n_a(_ForwardIterator __first
, _Size __n
,
685 { return std::__uninitialized_default_n(__first
, __n
); }
687 template<bool _TrivialValueType
>
688 struct __uninitialized_default_novalue_1
690 template<typename _ForwardIterator
>
692 __uninit_default_novalue(_ForwardIterator __first
,
693 _ForwardIterator __last
)
695 _ForwardIterator __cur
= __first
;
698 for (; __cur
!= __last
; ++__cur
)
699 std::_Construct_novalue(std::__addressof(*__cur
));
703 std::_Destroy(__first
, __cur
);
704 __throw_exception_again
;
710 struct __uninitialized_default_novalue_1
<true>
712 template<typename _ForwardIterator
>
714 __uninit_default_novalue(_ForwardIterator __first
,
715 _ForwardIterator __last
)
720 template<bool _TrivialValueType
>
721 struct __uninitialized_default_novalue_n_1
723 template<typename _ForwardIterator
, typename _Size
>
724 static _ForwardIterator
725 __uninit_default_novalue_n(_ForwardIterator __first
, _Size __n
)
727 _ForwardIterator __cur
= __first
;
730 for (; __n
> 0; --__n
, (void) ++__cur
)
731 std::_Construct_novalue(std::__addressof(*__cur
));
736 std::_Destroy(__first
, __cur
);
737 __throw_exception_again
;
743 struct __uninitialized_default_novalue_n_1
<true>
745 template<typename _ForwardIterator
, typename _Size
>
746 static _ForwardIterator
747 __uninit_default_novalue_n(_ForwardIterator __first
, _Size __n
)
748 { return std::next(__first
, __n
); }
751 // __uninitialized_default_novalue
752 // Fills [first, last) with std::distance(first, last) default-initialized
754 template<typename _ForwardIterator
>
756 __uninitialized_default_novalue(_ForwardIterator __first
,
757 _ForwardIterator __last
)
759 typedef typename iterator_traits
<_ForwardIterator
>::value_type
762 std::__uninitialized_default_novalue_1
<
763 is_trivially_default_constructible
<_ValueType
>::value
>::
764 __uninit_default_novalue(__first
, __last
);
767 // __uninitialized_default_n
768 // Fills [first, first + n) with n default-initialized value_type(s).
769 template<typename _ForwardIterator
, typename _Size
>
770 inline _ForwardIterator
771 __uninitialized_default_novalue_n(_ForwardIterator __first
, _Size __n
)
773 typedef typename iterator_traits
<_ForwardIterator
>::value_type
776 return __uninitialized_default_novalue_n_1
<
777 is_trivially_default_constructible
<_ValueType
>::value
>::
778 __uninit_default_novalue_n(__first
, __n
);
781 template<typename _InputIterator
, typename _Size
,
782 typename _ForwardIterator
>
784 __uninitialized_copy_n(_InputIterator __first
, _Size __n
,
785 _ForwardIterator __result
, input_iterator_tag
)
787 _ForwardIterator __cur
= __result
;
790 for (; __n
> 0; --__n
, (void) ++__first
, ++__cur
)
791 std::_Construct(std::__addressof(*__cur
), *__first
);
796 std::_Destroy(__result
, __cur
);
797 __throw_exception_again
;
801 template<typename _RandomAccessIterator
, typename _Size
,
802 typename _ForwardIterator
>
803 inline _ForwardIterator
804 __uninitialized_copy_n(_RandomAccessIterator __first
, _Size __n
,
805 _ForwardIterator __result
,
806 random_access_iterator_tag
)
807 { return std::uninitialized_copy(__first
, __first
+ __n
, __result
); }
809 template<typename _InputIterator
, typename _Size
,
810 typename _ForwardIterator
>
811 pair
<_InputIterator
, _ForwardIterator
>
812 __uninitialized_copy_n_pair(_InputIterator __first
, _Size __n
,
813 _ForwardIterator __result
, input_iterator_tag
)
815 _ForwardIterator __cur
= __result
;
818 for (; __n
> 0; --__n
, (void) ++__first
, ++__cur
)
819 std::_Construct(std::__addressof(*__cur
), *__first
);
820 return {__first
, __cur
};
824 std::_Destroy(__result
, __cur
);
825 __throw_exception_again
;
829 template<typename _RandomAccessIterator
, typename _Size
,
830 typename _ForwardIterator
>
831 inline pair
<_RandomAccessIterator
, _ForwardIterator
>
832 __uninitialized_copy_n_pair(_RandomAccessIterator __first
, _Size __n
,
833 _ForwardIterator __result
,
834 random_access_iterator_tag
)
836 auto __second_res
= uninitialized_copy(__first
, __first
+ __n
, __result
);
837 auto __first_res
= std::next(__first
, __n
);
838 return {__first_res
, __second_res
};
844 * @brief Copies the range [first,first+n) into result.
845 * @param __first An input iterator.
846 * @param __n The number of elements to copy.
847 * @param __result An output iterator.
848 * @return __result + __n
850 * Like copy_n(), but does not require an initialized output range.
852 template<typename _InputIterator
, typename _Size
, typename _ForwardIterator
>
853 inline _ForwardIterator
854 uninitialized_copy_n(_InputIterator __first
, _Size __n
,
855 _ForwardIterator __result
)
856 { return std::__uninitialized_copy_n(__first
, __n
, __result
,
857 std::__iterator_category(__first
)); }
859 /// @cond undocumented
860 template<typename _InputIterator
, typename _Size
, typename _ForwardIterator
>
861 inline pair
<_InputIterator
, _ForwardIterator
>
862 __uninitialized_copy_n_pair(_InputIterator __first
, _Size __n
,
863 _ForwardIterator __result
)
866 std::__uninitialized_copy_n_pair(__first
, __n
, __result
,
867 std::__iterator_category(__first
));
872 #if __cplusplus >= 201703L
873 # define __cpp_lib_raw_memory_algorithms 201606L
876 * @brief Default-initializes objects in the range [first,last).
877 * @param __first A forward iterator.
878 * @param __last A forward iterator.
880 template <typename _ForwardIterator
>
882 uninitialized_default_construct(_ForwardIterator __first
,
883 _ForwardIterator __last
)
885 __uninitialized_default_novalue(__first
, __last
);
889 * @brief Default-initializes objects in the range [first,first+count).
890 * @param __first A forward iterator.
891 * @param __count The number of objects to construct.
892 * @return __first + __count
894 template <typename _ForwardIterator
, typename _Size
>
895 inline _ForwardIterator
896 uninitialized_default_construct_n(_ForwardIterator __first
, _Size __count
)
898 return __uninitialized_default_novalue_n(__first
, __count
);
902 * @brief Value-initializes objects in the range [first,last).
903 * @param __first A forward iterator.
904 * @param __last A forward iterator.
906 template <typename _ForwardIterator
>
908 uninitialized_value_construct(_ForwardIterator __first
,
909 _ForwardIterator __last
)
911 return __uninitialized_default(__first
, __last
);
915 * @brief Value-initializes objects in the range [first,first+count).
916 * @param __first A forward iterator.
917 * @param __count The number of objects to construct.
918 * @return __result + __count
920 template <typename _ForwardIterator
, typename _Size
>
921 inline _ForwardIterator
922 uninitialized_value_construct_n(_ForwardIterator __first
, _Size __count
)
924 return __uninitialized_default_n(__first
, __count
);
928 * @brief Move-construct from the range [first,last) into result.
929 * @param __first An input iterator.
930 * @param __last An input iterator.
931 * @param __result An output iterator.
932 * @return __result + (__first - __last)
934 template <typename _InputIterator
, typename _ForwardIterator
>
935 inline _ForwardIterator
936 uninitialized_move(_InputIterator __first
, _InputIterator __last
,
937 _ForwardIterator __result
)
939 return std::uninitialized_copy
940 (_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
941 _GLIBCXX_MAKE_MOVE_ITERATOR(__last
), __result
);
945 * @brief Move-construct from the range [first,first+count) into result.
946 * @param __first An input iterator.
947 * @param __count The number of objects to initialize.
948 * @param __result An output iterator.
949 * @return __result + __count
951 template <typename _InputIterator
, typename _Size
, typename _ForwardIterator
>
952 inline pair
<_InputIterator
, _ForwardIterator
>
953 uninitialized_move_n(_InputIterator __first
, _Size __count
,
954 _ForwardIterator __result
)
956 auto __res
= std::__uninitialized_copy_n_pair
957 (_GLIBCXX_MAKE_MOVE_ITERATOR(__first
),
959 return {__res
.first
.base(), __res
.second
};
963 #if __cplusplus >= 201103L
964 /// @cond undocumented
966 template<typename _Tp
, typename _Up
, typename _Allocator
>
968 __relocate_object_a(_Tp
* __restrict __dest
, _Up
* __restrict __orig
,
970 noexcept(noexcept(std::allocator_traits
<_Allocator
>::construct(__alloc
,
971 __dest
, std::move(*__orig
)))
972 && noexcept(std::allocator_traits
<_Allocator
>::destroy(
973 __alloc
, std::__addressof(*__orig
))))
975 typedef std::allocator_traits
<_Allocator
> __traits
;
976 __traits::construct(__alloc
, __dest
, std::move(*__orig
));
977 __traits::destroy(__alloc
, std::__addressof(*__orig
));
980 // This class may be specialized for specific types.
981 // Also known as is_trivially_relocatable.
982 template<typename _Tp
, typename
= void>
983 struct __is_bitwise_relocatable
984 : is_trivial
<_Tp
> { };
986 template <typename _Tp
, typename _Up
>
987 inline __enable_if_t
<std::__is_bitwise_relocatable
<_Tp
>::value
, _Tp
*>
988 __relocate_a_1(_Tp
* __first
, _Tp
* __last
,
989 _Tp
* __result
, allocator
<_Up
>&) noexcept
991 ptrdiff_t __count
= __last
- __first
;
993 __builtin_memmove(__result
, __first
, __count
* sizeof(_Tp
));
994 return __result
+ __count
;
997 template <typename _InputIterator
, typename _ForwardIterator
,
999 inline _ForwardIterator
1000 __relocate_a_1(_InputIterator __first
, _InputIterator __last
,
1001 _ForwardIterator __result
, _Allocator
& __alloc
)
1002 noexcept(noexcept(std::__relocate_object_a(std::addressof(*__result
),
1003 std::addressof(*__first
),
1006 typedef typename iterator_traits
<_InputIterator
>::value_type
1008 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1010 static_assert(std::is_same
<_ValueType
, _ValueType2
>::value
,
1011 "relocation is only possible for values of the same type");
1012 _ForwardIterator __cur
= __result
;
1013 for (; __first
!= __last
; ++__first
, (void)++__cur
)
1014 std::__relocate_object_a(std::__addressof(*__cur
),
1015 std::__addressof(*__first
), __alloc
);
1019 template <typename _InputIterator
, typename _ForwardIterator
,
1020 typename _Allocator
>
1021 inline _ForwardIterator
1022 __relocate_a(_InputIterator __first
, _InputIterator __last
,
1023 _ForwardIterator __result
, _Allocator
& __alloc
)
1024 noexcept(noexcept(__relocate_a_1(std::__niter_base(__first
),
1025 std::__niter_base(__last
),
1026 std::__niter_base(__result
), __alloc
)))
1028 return __relocate_a_1(std::__niter_base(__first
),
1029 std::__niter_base(__last
),
1030 std::__niter_base(__result
), __alloc
);
1038 _GLIBCXX_END_NAMESPACE_VERSION
1041 #endif /* _STL_UNINITIALIZED_H */