56#ifndef _STL_UNINITIALIZED_H
57#define _STL_UNINITIALIZED_H 1
59#if __cplusplus >= 201103L
66#if __cplusplus >= 201703L
70namespace std _GLIBCXX_VISIBILITY(default)
72_GLIBCXX_BEGIN_NAMESPACE_VERSION
80#if __cplusplus >= 201103L
81 template<
typename _ValueType,
typename _Tp>
83 __check_constructible()
90 static_assert(is_constructible<_ValueType, _Tp>::value,
91 "result type must be constructible from input type");
99# define _GLIBCXX_USE_ASSIGN_FOR_INIT(T, U) \
100 __is_trivial(T) && __is_assignable(T&, U) \
101 && std::__check_constructible<T, U>()
106# define _GLIBCXX_USE_ASSIGN_FOR_INIT(T, U) \
107 __is_trivial(T) && __is_assignable(T&, U)
110 template<
typename _InputIterator,
typename _ForwardIterator>
113 __do_uninit_copy(_InputIterator __first, _InputIterator __last,
114 _ForwardIterator __result)
116 _ForwardIterator __cur = __result;
119 for (; __first != __last; ++__first, (void)++__cur)
126 __throw_exception_again;
130 template<
bool _TrivialValueTypes>
131 struct __uninitialized_copy
133 template<
typename _InputIterator,
typename _ForwardIterator>
134 static _ForwardIterator
135 __uninit_copy(_InputIterator __first, _InputIterator __last,
136 _ForwardIterator __result)
137 {
return std::__do_uninit_copy(__first, __last, __result); }
141 struct __uninitialized_copy<true>
143 template<
typename _InputIterator,
typename _ForwardIterator>
144 static _ForwardIterator
145 __uninit_copy(_InputIterator __first, _InputIterator __last,
146 _ForwardIterator __result)
147 {
return std::copy(__first, __last, __result); }
161 template<
typename _InputIterator,
typename _ForwardIterator>
162 inline _ForwardIterator
164 _ForwardIterator __result)
174 const bool __can_memmove = __is_trivial(_ValueType1);
176#if __cplusplus < 201103L
179 using _From =
decltype(*__first);
181 const bool __assignable
182 = _GLIBCXX_USE_ASSIGN_FOR_INIT(_ValueType2, _From);
184 return std::__uninitialized_copy<__can_memmove && __assignable>::
185 __uninit_copy(__first, __last, __result);
190 template<
typename _ForwardIterator,
typename _Tp>
191 _GLIBCXX20_CONSTEXPR
void
192 __do_uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
195 _ForwardIterator __cur = __first;
198 for (; __cur != __last; ++__cur)
204 __throw_exception_again;
208 template<
bool _TrivialValueType>
209 struct __uninitialized_fill
211 template<
typename _ForwardIterator,
typename _Tp>
213 __uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
215 { std::__do_uninit_fill(__first, __last, __x); }
219 struct __uninitialized_fill<true>
221 template<
typename _ForwardIterator,
typename _Tp>
223 __uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
225 { std::fill(__first, __last, __x); }
239 template<
typename _ForwardIterator,
typename _Tp>
249 const bool __can_fill
250 = _GLIBCXX_USE_ASSIGN_FOR_INIT(_ValueType,
const _Tp&);
252 std::__uninitialized_fill<__can_fill>::
253 __uninit_fill(__first, __last, __x);
258 template<
typename _ForwardIterator,
typename _Size,
typename _Tp>
261 __do_uninit_fill_n(_ForwardIterator __first, _Size __n,
const _Tp& __x)
263 _ForwardIterator __cur = __first;
266 for (; __n > 0; --__n, (void) ++__cur)
273 __throw_exception_again;
277 template<
bool _TrivialValueType>
278 struct __uninitialized_fill_n
280 template<
typename _ForwardIterator,
typename _Size,
typename _Tp>
281 static _ForwardIterator
282 __uninit_fill_n(_ForwardIterator __first, _Size __n,
284 {
return std::__do_uninit_fill_n(__first, __n, __x); }
288 struct __uninitialized_fill_n<true>
290 template<
typename _ForwardIterator,
typename _Size,
typename _Tp>
291 static _ForwardIterator
292 __uninit_fill_n(_ForwardIterator __first, _Size __n,
294 {
return std::fill_n(__first, __n, __x); }
310 template<
typename _ForwardIterator,
typename _Size,
typename _Tp>
311 inline _ForwardIterator
319 const bool __can_fill
320 = _GLIBCXX_USE_ASSIGN_FOR_INIT(_ValueType,
const _Tp&)
324 && __is_integer<_Size>::__value;
326 return __uninitialized_fill_n<__can_fill>::
327 __uninit_fill_n(__first, __n, __x);
330#undef _GLIBCXX_USE_ASSIGN_FOR_INIT
340 template<
typename _InputIterator,
typename _ForwardIterator,
344 __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
345 _ForwardIterator __result, _Allocator& __alloc)
347 _ForwardIterator __cur = __result;
351 for (; __first != __last; ++__first, (void)++__cur)
358 __throw_exception_again;
363 template<
typename _InputIterator,
typename _ForwardIterator,
typename _Tp>
365 inline _ForwardIterator
366 __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
367 _ForwardIterator __result, allocator<_Tp>&)
369#ifdef __cpp_lib_is_constant_evaluated
370 if (std::is_constant_evaluated())
371 return std::__do_uninit_copy(__first, __last, __result);
377 template<
typename _InputIterator,
typename _ForwardIterator,
380 inline _ForwardIterator
381 __uninitialized_move_a(_InputIterator __first, _InputIterator __last,
382 _ForwardIterator __result, _Allocator& __alloc)
384 return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
385 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
389 template<
typename _InputIterator,
typename _ForwardIterator,
392 inline _ForwardIterator
393 __uninitialized_move_if_noexcept_a(_InputIterator __first,
394 _InputIterator __last,
395 _ForwardIterator __result,
398 return std::__uninitialized_copy_a
399 (_GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__first),
400 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__last), __result, __alloc);
403 template<
typename _ForwardIterator,
typename _Tp,
typename _Allocator>
406 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
407 const _Tp& __x, _Allocator& __alloc)
409 _ForwardIterator __cur = __first;
413 for (; __cur != __last; ++__cur)
419 __throw_exception_again;
424 template<
typename _ForwardIterator,
typename _Tp,
typename _Tp2>
427 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
428 const _Tp& __x, allocator<_Tp2>&)
430#ifdef __cpp_lib_is_constant_evaluated
431 if (std::is_constant_evaluated())
432 return std::__do_uninit_fill(__first, __last, __x);
438 template<
typename _ForwardIterator,
typename _Size,
typename _Tp,
442 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
443 const _Tp& __x, _Allocator& __alloc)
445 _ForwardIterator __cur = __first;
449 for (; __n > 0; --__n, (void) ++__cur)
456 __throw_exception_again;
461 template<
typename _ForwardIterator,
typename _Size,
typename _Tp,
464 inline _ForwardIterator
465 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
466 const _Tp& __x, allocator<_Tp2>&)
468#ifdef __cpp_lib_is_constant_evaluated
469 if (std::is_constant_evaluated())
470 return std::__do_uninit_fill_n(__first, __n, __x);
485 template<
typename _InputIterator1,
typename _InputIterator2,
486 typename _ForwardIterator,
typename _Allocator>
487 inline _ForwardIterator
488 __uninitialized_copy_move(_InputIterator1 __first1,
489 _InputIterator1 __last1,
490 _InputIterator2 __first2,
491 _InputIterator2 __last2,
492 _ForwardIterator __result,
495 _ForwardIterator __mid = std::__uninitialized_copy_a(__first1, __last1,
500 return std::__uninitialized_move_a(__first2, __last2, __mid, __alloc);
505 __throw_exception_again;
513 template<
typename _InputIterator1,
typename _InputIterator2,
514 typename _ForwardIterator,
typename _Allocator>
515 inline _ForwardIterator
516 __uninitialized_move_copy(_InputIterator1 __first1,
517 _InputIterator1 __last1,
518 _InputIterator2 __first2,
519 _InputIterator2 __last2,
520 _ForwardIterator __result,
523 _ForwardIterator __mid = std::__uninitialized_move_a(__first1, __last1,
528 return std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc);
533 __throw_exception_again;
540 template<
typename _ForwardIterator,
typename _Tp,
typename _InputIterator,
542 inline _ForwardIterator
543 __uninitialized_fill_move(_ForwardIterator __result, _ForwardIterator __mid,
544 const _Tp& __x, _InputIterator __first,
545 _InputIterator __last, _Allocator& __alloc)
547 std::__uninitialized_fill_a(__result, __mid, __x, __alloc);
550 return std::__uninitialized_move_a(__first, __last, __mid, __alloc);
555 __throw_exception_again;
562 template<
typename _InputIterator,
typename _ForwardIterator,
typename _Tp,
565 __uninitialized_move_fill(_InputIterator __first1, _InputIterator __last1,
566 _ForwardIterator __first2,
567 _ForwardIterator __last2,
const _Tp& __x,
570 _ForwardIterator __mid2 = std::__uninitialized_move_a(__first1, __last1,
575 std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc);
580 __throw_exception_again;
586#if __cplusplus >= 201103L
592 template<
bool _TrivialValueType>
593 struct __uninitialized_default_1
595 template<
typename _ForwardIterator>
597 __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
599 _ForwardIterator __cur = __first;
602 for (; __cur != __last; ++__cur)
608 __throw_exception_again;
614 struct __uninitialized_default_1<true>
616 template<
typename _ForwardIterator>
618 __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
620 if (__first == __last)
623 typename iterator_traits<_ForwardIterator>::value_type* __val
626 if (++__first != __last)
627 std::fill(__first, __last, *__val);
631 template<
bool _TrivialValueType>
632 struct __uninitialized_default_n_1
634 template<
typename _ForwardIterator,
typename _Size>
636 static _ForwardIterator
637 __uninit_default_n(_ForwardIterator __first, _Size __n)
639 _ForwardIterator __cur = __first;
642 for (; __n > 0; --__n, (void) ++__cur)
649 __throw_exception_again;
655 struct __uninitialized_default_n_1<true>
657 template<
typename _ForwardIterator,
typename _Size>
659 static _ForwardIterator
660 __uninit_default_n(_ForwardIterator __first, _Size __n)
664 typename iterator_traits<_ForwardIterator>::value_type* __val
668 __first = std::fill_n(__first, __n - 1, *__val);
676 template<
typename _ForwardIterator>
678 __uninitialized_default(_ForwardIterator __first,
679 _ForwardIterator __last)
681 typedef typename iterator_traits<_ForwardIterator>::value_type
684 const bool __assignable = is_copy_assignable<_ValueType>::value;
686 std::__uninitialized_default_1<__is_trivial(_ValueType)
688 __uninit_default(__first, __last);
693 template<
typename _ForwardIterator,
typename _Size>
695 inline _ForwardIterator
696 __uninitialized_default_n(_ForwardIterator __first, _Size __n)
698#ifdef __cpp_lib_is_constant_evaluated
699 if (std::is_constant_evaluated())
700 return __uninitialized_default_n_1<false>::
701 __uninit_default_n(__first, __n);
704 typedef typename iterator_traits<_ForwardIterator>::value_type
707 constexpr bool __can_fill
708 = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
710 return __uninitialized_default_n_1<__is_trivial(_ValueType)
712 __uninit_default_n(__first, __n);
719 template<
typename _ForwardIterator,
typename _Allocator>
721 __uninitialized_default_a(_ForwardIterator __first,
722 _ForwardIterator __last,
725 _ForwardIterator __cur = __first;
729 for (; __cur != __last; ++__cur)
735 __throw_exception_again;
740 template<
typename _ForwardIterator,
typename _Tp>
742 __uninitialized_default_a(_ForwardIterator __first,
743 _ForwardIterator __last,
745 { std::__uninitialized_default(__first, __last); }
751 template<
typename _ForwardIterator,
typename _Size,
typename _Allocator>
752 _GLIBCXX20_CONSTEXPR _ForwardIterator
753 __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
756 _ForwardIterator __cur = __first;
760 for (; __n > 0; --__n, (void) ++__cur)
767 __throw_exception_again;
774 template<
typename _ForwardIterator,
typename _Size,
typename _Tp>
776 inline _ForwardIterator
777 __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
779 {
return std::__uninitialized_default_n(__first, __n); }
782 template<
bool _TrivialValueType>
783 struct __uninitialized_default_novalue_1
785 template<
typename _ForwardIterator>
787 __uninit_default_novalue(_ForwardIterator __first,
788 _ForwardIterator __last)
790 _ForwardIterator __cur = __first;
793 for (; __cur != __last; ++__cur)
799 __throw_exception_again;
805 struct __uninitialized_default_novalue_1<true>
807 template<
typename _ForwardIterator>
809 __uninit_default_novalue(_ForwardIterator, _ForwardIterator)
814 template<
bool _TrivialValueType>
815 struct __uninitialized_default_novalue_n_1
817 template<
typename _ForwardIterator,
typename _Size>
818 static _ForwardIterator
819 __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
821 _ForwardIterator __cur = __first;
824 for (; __n > 0; --__n, (void) ++__cur)
831 __throw_exception_again;
837 struct __uninitialized_default_novalue_n_1<true>
839 template<
typename _ForwardIterator,
typename _Size>
840 static _ForwardIterator
841 __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
842 {
return std::next(__first, __n); }
847 template<
typename _ForwardIterator>
849 __uninitialized_default_novalue(_ForwardIterator __first,
850 _ForwardIterator __last)
852 typedef typename iterator_traits<_ForwardIterator>::value_type
855 std::__uninitialized_default_novalue_1<
856 is_trivially_default_constructible<_ValueType>::value>::
857 __uninit_default_novalue(__first, __last);
862 template<
typename _ForwardIterator,
typename _Size>
863 inline _ForwardIterator
864 __uninitialized_default_novalue_n(_ForwardIterator __first, _Size __n)
866 typedef typename iterator_traits<_ForwardIterator>::value_type
869 return __uninitialized_default_novalue_n_1<
870 is_trivially_default_constructible<_ValueType>::value>::
871 __uninit_default_novalue_n(__first, __n);
874 template<
typename _InputIterator,
typename _Size,
875 typename _ForwardIterator>
877 __uninitialized_copy_n(_InputIterator __first, _Size __n,
878 _ForwardIterator __result, input_iterator_tag)
880 _ForwardIterator __cur = __result;
883 for (; __n > 0; --__n, (void) ++__first, ++__cur)
890 __throw_exception_again;
894 template<
typename _RandomAccessIterator,
typename _Size,
895 typename _ForwardIterator>
896 inline _ForwardIterator
897 __uninitialized_copy_n(_RandomAccessIterator __first, _Size __n,
898 _ForwardIterator __result,
899 random_access_iterator_tag)
902 template<
typename _InputIterator,
typename _Size,
903 typename _ForwardIterator>
904 pair<_InputIterator, _ForwardIterator>
905 __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
906 _ForwardIterator __result, input_iterator_tag)
908 _ForwardIterator __cur = __result;
911 for (; __n > 0; --__n, (void) ++__first, ++__cur)
913 return {__first, __cur};
918 __throw_exception_again;
922 template<
typename _RandomAccessIterator,
typename _Size,
923 typename _ForwardIterator>
924 inline pair<_RandomAccessIterator, _ForwardIterator>
925 __uninitialized_copy_n_pair(_RandomAccessIterator __first, _Size __n,
926 _ForwardIterator __result,
927 random_access_iterator_tag)
930 auto __first_res = std::next(__first, __n);
931 return {__first_res, __second_res};
946 template<
typename _InputIterator,
typename _Size,
typename _ForwardIterator>
947 inline _ForwardIterator
949 _ForwardIterator __result)
950 {
return std::__uninitialized_copy_n(__first, __n, __result,
954 template<
typename _InputIterator,
typename _Size,
typename _ForwardIterator>
955 inline pair<_InputIterator, _ForwardIterator>
956 __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
957 _ForwardIterator __result)
960 std::__uninitialized_copy_n_pair(__first, __n, __result,
966#ifdef __glibcxx_raw_memory_algorithms
973 template <
typename _ForwardIterator>
976 _ForwardIterator __last)
978 __uninitialized_default_novalue(__first, __last);
988 template <
typename _ForwardIterator,
typename _Size>
989 inline _ForwardIterator
992 return __uninitialized_default_novalue_n(__first, __count);
1001 template <
typename _ForwardIterator>
1004 _ForwardIterator __last)
1006 return __uninitialized_default(__first, __last);
1016 template <
typename _ForwardIterator,
typename _Size>
1017 inline _ForwardIterator
1020 return __uninitialized_default_n(__first, __count);
1031 template <
typename _InputIterator,
typename _ForwardIterator>
1032 inline _ForwardIterator
1034 _ForwardIterator __result)
1037 (_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
1038 _GLIBCXX_MAKE_MOVE_ITERATOR(__last), __result);
1049 template <
typename _InputIterator,
typename _Size,
typename _ForwardIterator>
1050 inline pair<_InputIterator, _ForwardIterator>
1052 _ForwardIterator __result)
1054 auto __res = std::__uninitialized_copy_n_pair
1055 (_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
1057 return {__res.first.base(), __res.second};
1061#if __cplusplus >= 201103L
1064 template<
typename _Tp,
typename _Up,
typename _Allocator>
1065 _GLIBCXX20_CONSTEXPR
1067 __relocate_object_a(_Tp* __restrict __dest, _Up* __restrict __orig,
1068 _Allocator& __alloc)
1075 __traits::construct(__alloc, __dest,
std::move(*__orig));
1081 template<
typename _Tp,
typename =
void>
1082 struct __is_bitwise_relocatable
1083 : is_trivial<_Tp> { };
1085 template <
typename _InputIterator,
typename _ForwardIterator,
1086 typename _Allocator>
1087 _GLIBCXX20_CONSTEXPR
1088 inline _ForwardIterator
1089 __relocate_a_1(_InputIterator __first, _InputIterator __last,
1090 _ForwardIterator __result, _Allocator& __alloc)
1091 noexcept(
noexcept(std::__relocate_object_a(
std::addressof(*__result),
1095 typedef typename iterator_traits<_InputIterator>::value_type
1097 typedef typename iterator_traits<_ForwardIterator>::value_type
1100 "relocation is only possible for values of the same type");
1101 _ForwardIterator __cur = __result;
1102 for (; __first != __last; ++__first, (void)++__cur)
1109 template <
typename _Tp,
typename _Up>
1110 _GLIBCXX20_CONSTEXPR
1111 inline __enable_if_t<std::__is_bitwise_relocatable<_Tp>::value, _Tp*>
1112 __relocate_a_1(_Tp* __first, _Tp* __last,
1114 [[__maybe_unused__]] allocator<_Up>& __alloc)
noexcept
1116 ptrdiff_t __count = __last - __first;
1119#ifdef __cpp_lib_is_constant_evaluated
1120 if (std::is_constant_evaluated())
1124 __gnu_cxx::__normal_iterator<_Tp*, void> __out(__result);
1125 __out = std::__relocate_a_1(__first, __last, __out, __alloc);
1126 return __out.base();
1129 __builtin_memcpy(__result, __first, __count *
sizeof(_Tp));
1131 return __result + __count;
1135 template <
typename _InputIterator,
typename _ForwardIterator,
1136 typename _Allocator>
1137 _GLIBCXX20_CONSTEXPR
1138 inline _ForwardIterator
1139 __relocate_a(_InputIterator __first, _InputIterator __last,
1140 _ForwardIterator __result, _Allocator& __alloc)
1141 noexcept(
noexcept(__relocate_a_1(std::__niter_base(__first),
1142 std::__niter_base(__last),
1143 std::__niter_base(__result), __alloc)))
1145 return std::__relocate_a_1(std::__niter_base(__first),
1146 std::__niter_base(__last),
1147 std::__niter_base(__result), __alloc);
1155_GLIBCXX_END_NAMESPACE_VERSION
_ForwardIterator uninitialized_copy_n(_InputIterator __first, _Size __n, _ForwardIterator __result)
Copies the range [first,first+n) into result.
void uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__x)
Copies the value x into the range [first,last).
_ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __count)
Value-initializes objects in the range [first,first+count).
_ForwardIterator uninitialized_move(_InputIterator __first, _InputIterator __last, _ForwardIterator __result)
Move-construct from the range [first,last) into result.
_ForwardIterator uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp &__x)
Copies the value x into the range [first,first+n).
_ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
Default-initializes objects in the range [first,first+count).
void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last)
Default-initializes objects in the range [first,last).
_ForwardIterator uninitialized_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result)
Copies the range [first,last) into result.
void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last)
Value-initializes objects in the range [first,last).
pair< _InputIterator, _ForwardIterator > uninitialized_move_n(_InputIterator __first, _Size __count, _ForwardIterator __result)
Move-construct from the range [first,first+count) into result.
constexpr _Tp * addressof(_Tp &__r) noexcept
Returns the actual address of the object or function referenced by r, even in the presence of an over...
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
ISO C++ entities toplevel namespace is std.
constexpr void _Construct(_Tp *__p, _Args &&... __args)
constexpr void _Destroy(_ForwardIterator __first, _ForwardIterator __last)
Uniform interface to all allocator types.
static constexpr auto construct(_Alloc &__a, _Tp *__p, _Args &&... __args) noexcept(noexcept(_S_construct(__a, __p, std::forward< _Args >(__args)...))) -> decltype(_S_construct(__a, __p, std::forward< _Args >(__args)...))
Construct an object of type _Tp
Traits class for iterators.
Uniform interface to C++98 and C++11 allocators.