libstdc++
stl_uninitialized.h
Go to the documentation of this file.
1// Raw memory manipulators -*- C++ -*-
2
3// Copyright (C) 2001-2026 Free Software Foundation, Inc.
4//
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)
9// any later version.
10
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.
15
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.
19
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/>.
24
25/*
26 *
27 * Copyright (c) 1994
28 * Hewlett-Packard Company
29 *
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.
37 *
38 *
39 * Copyright (c) 1996,1997
40 * Silicon Graphics Computer Systems, Inc.
41 *
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.
49 */
50
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}
54 */
55
56#ifndef _STL_UNINITIALIZED_H
57#define _STL_UNINITIALIZED_H 1
58
59#if __cplusplus >= 201103L
60# include <type_traits>
61# include <bits/ptr_traits.h> // to_address
62# include <bits/stl_pair.h> // pair
63# include <bits/stl_algobase.h> // fill, fill_n
64#endif
65
66#include <bits/cpp_type_traits.h> // __is_pointer
67#include <bits/stl_iterator_base_funcs.h> // distance, advance
68#include <bits/stl_iterator.h> // __niter_base
69#include <ext/alloc_traits.h> // __alloc_traits
70
71namespace std _GLIBCXX_VISIBILITY(default)
72{
73_GLIBCXX_BEGIN_NAMESPACE_VERSION
74
75 /** @addtogroup memory
76 * @{
77 */
78
79 /// @cond undocumented
80
81 template<typename _ForwardIterator, typename _Alloc = void>
82 struct _UninitDestroyGuard
83 {
84 _GLIBCXX20_CONSTEXPR
85 explicit
86 _UninitDestroyGuard(_ForwardIterator& __first, _Alloc& __a)
87 : _M_first(__first), _M_cur(__builtin_addressof(__first)), _M_alloc(__a)
88 { }
89
90 _GLIBCXX20_CONSTEXPR
91 ~_UninitDestroyGuard()
92 {
93 if (__builtin_expect(_M_cur != 0, 0))
94 std::_Destroy(_M_first, *_M_cur, _M_alloc);
95 }
96
97 _GLIBCXX20_CONSTEXPR
98 void release() { _M_cur = 0; }
99
100 private:
101 _ForwardIterator const _M_first;
102 _ForwardIterator* _M_cur;
103 _Alloc& _M_alloc;
104
105 _UninitDestroyGuard(const _UninitDestroyGuard&);
106 };
107
108 template<typename _ForwardIterator>
109 struct _UninitDestroyGuard<_ForwardIterator, void>
110 {
111 _GLIBCXX20_CONSTEXPR
112 explicit
113 _UninitDestroyGuard(_ForwardIterator& __first)
114 : _M_first(__first), _M_cur(__builtin_addressof(__first))
115 { }
116
117 _GLIBCXX20_CONSTEXPR
118 ~_UninitDestroyGuard()
119 {
120 if (__builtin_expect(_M_cur != 0, 0))
121#if __cplusplus == 201703L
122 // std::uninitialized_{value,default}{,_n} can construct array types,
123 // but std::_Destroy cannot handle them until C++20 (PR 120397).
124 _S_destroy(_M_first, *_M_cur);
125#else
126 std::_Destroy(_M_first, *_M_cur);
127#endif
128 }
129
130 _GLIBCXX20_CONSTEXPR
131 void release() { _M_cur = 0; }
132
133 _ForwardIterator const _M_first;
134 _ForwardIterator* _M_cur;
135
136 private:
137 _UninitDestroyGuard(const _UninitDestroyGuard&);
138
139#if __cplusplus == 201703L
140 template<typename _Iter>
141 static void
142 _S_destroy(_Iter __first, _Iter __last)
143 {
144 using _ValT = typename iterator_traits<_Iter>::value_type;
145 if constexpr (is_array<_ValT>::value)
146 for (; __first != __last; ++__first)
147 _S_destroy(*__first, *__first + extent<_ValT>::value);
148 else
149 std::_Destroy(__first, __last);
150 }
151#endif
152 };
153
154 // This is the default implementation of std::uninitialized_copy.
155 // This can be used with C++20 iterators and non-common ranges.
156 template<typename _InputIterator, typename _Sentinel,
157 typename _ForwardIterator>
158 _GLIBCXX20_CONSTEXPR
159 _ForwardIterator
160 __do_uninit_copy(_InputIterator __first, _Sentinel __last,
161 _ForwardIterator __result)
162 {
163 _UninitDestroyGuard<_ForwardIterator> __guard(__result);
164 for (; __first != __last; ++__first, (void)++__result)
165 std::_Construct(std::__addressof(*__result), *__first);
166 __guard.release();
167 return __result;
168 }
169
170#if __cplusplus < 201103L
171
172 // True if we can unwrap _Iter to get a pointer by using std::__niter_base.
173 template<typename _Iter,
174 typename _Base = __decltype(std::__niter_base(*(_Iter*)0))>
175 struct __unwrappable_niter
176 { enum { __value = false }; };
177
178 template<typename _Iter, typename _Tp>
179 struct __unwrappable_niter<_Iter, _Tp*>
180 { enum { __value = true }; };
181
182 // Use template specialization for C++98 when 'if constexpr' can't be used.
183 template<bool _CanMemcpy>
184 struct __uninitialized_copy
185 {
186 template<typename _InputIterator, typename _ForwardIterator>
187 static _ForwardIterator
188 __uninit_copy(_InputIterator __first, _InputIterator __last,
189 _ForwardIterator __result)
190 { return std::__do_uninit_copy(__first, __last, __result); }
191 };
192
193 template<>
194 struct __uninitialized_copy<true>
195 {
196 // Overload for generic iterators.
197 template<typename _InputIterator, typename _ForwardIterator>
198 static _ForwardIterator
199 __uninit_copy(_InputIterator __first, _InputIterator __last,
200 _ForwardIterator __result)
201 {
202 if (__unwrappable_niter<_InputIterator>::__value
203 && __unwrappable_niter<_ForwardIterator>::__value)
204 {
205 __uninit_copy(std::__niter_base(__first),
206 std::__niter_base(__last),
207 std::__niter_base(__result));
208 std::advance(__result, std::distance(__first, __last));
209 return __result;
210 }
211 else
212 return std::__do_uninit_copy(__first, __last, __result);
213 }
214
215 // Overload for pointers.
216 template<typename _Tp, typename _Up>
217 static _Up*
218 __uninit_copy(_Tp* __first, _Tp* __last, _Up* __result)
219 {
220 // Ensure that we don't successfully memcpy in cases that should be
221 // ill-formed because is_constructible<_Up, _Tp&> is false.
222 typedef __typeof__(static_cast<_Up>(*__first)) __check
223 __attribute__((__unused__));
224
225 const ptrdiff_t __n = __last - __first;
226 if (__builtin_expect(__n > 0, true))
227 {
228 __builtin_memcpy(__result, __first, __n * sizeof(_Tp));
229 __result += __n;
230 }
231 return __result;
232 }
233 };
234#endif
235 /// @endcond
236
237#pragma GCC diagnostic push
238#pragma GCC diagnostic ignored "-Wc++17-extensions"
239#pragma GCC diagnostic ignored "-Wclass-memaccess"
240 /**
241 * @brief Copies the range [first,last) into result.
242 * @param __first An input iterator.
243 * @param __last An input iterator.
244 * @param __result A forward iterator.
245 * @return __result + (__last - __first)
246 *
247 * Like std::copy, but does not require an initialized output range.
248 */
249 template<typename _InputIterator, typename _ForwardIterator>
250 _GLIBCXX26_CONSTEXPR
251 inline _ForwardIterator
252 uninitialized_copy(_InputIterator __first, _InputIterator __last,
253 _ForwardIterator __result)
254 {
255 // We can use memcpy to copy the ranges under these conditions:
256 //
257 // _ForwardIterator and _InputIterator are both contiguous iterators,
258 // so that we can turn them into pointers to pass to memcpy.
259 // Before C++20 we can't detect all contiguous iterators, so we only
260 // handle built-in pointers and __normal_iterator<T*, C> types.
261 //
262 // The value types of both iterators are trivially-copyable types,
263 // so that memcpy is not undefined and can begin the lifetime of
264 // new objects in the output range.
265 //
266 // Finally, memcpy from the source type, S, to the destination type, D,
267 // must give the same value as initialization of D from S would give.
268 // We require is_trivially_constructible<D, S> to be true, but that is
269 // not sufficient. Some cases of trivial initialization are not just a
270 // bitwise copy, even when sizeof(D) == sizeof(S),
271 // e.g. bit_cast<unsigned>(1.0f) != 1u because the corresponding bits
272 // of the value representations do not have the same meaning.
273 // We cannot tell when this condition is true in general,
274 // so we rely on the __memcpyable trait.
275
276#if __cplusplus >= 201103L
277 using _Dest = decltype(std::__niter_base(__result));
278 using _Src = decltype(std::__niter_base(__first));
280
281#if __glibcxx_raw_memory_algorithms >= 202411L // >= C++26
282 if consteval {
283 return std::__do_uninit_copy(__first, __last, __result);
284 }
285#endif
286 if constexpr (!__is_trivially_constructible(_ValT, decltype(*__first)))
287 return std::__do_uninit_copy(__first, __last, __result);
288 else if constexpr (__memcpyable<_Dest, _Src>::__value)
289 {
290 ptrdiff_t __n = __last - __first;
291 if (__n > 0) [[__likely__]]
292 {
293 using _ValT = typename remove_pointer<_Src>::type;
294 __builtin_memcpy(std::__niter_base(__result),
295 std::__niter_base(__first),
296 __n * sizeof(_ValT));
297 __result += __n;
298 }
299 return __result;
300 }
301#if __cpp_lib_concepts
302 else if constexpr (contiguous_iterator<_ForwardIterator>
303 && contiguous_iterator<_InputIterator>)
304 {
305 using _DestPtr = decltype(std::to_address(__result));
306 using _SrcPtr = decltype(std::to_address(__first));
307 if constexpr (__memcpyable<_DestPtr, _SrcPtr>::__value)
308 {
309 if (auto __n = __last - __first; __n > 0) [[likely]]
310 {
311 void* __dest = std::to_address(__result);
312 const void* __src = std::to_address(__first);
313 size_t __nbytes = __n * sizeof(remove_pointer_t<_DestPtr>);
314 __builtin_memcpy(__dest, __src, __nbytes);
315 __result += __n;
316 }
317 return __result;
318 }
319 else
320 return std::__do_uninit_copy(__first, __last, __result);
321 }
322#endif
323 else
324 return std::__do_uninit_copy(__first, __last, __result);
325#else // C++98
327 _ValueType1;
329 _ValueType2;
330
331 const bool __can_memcpy
332 = __memcpyable<_ValueType1*, _ValueType2*>::__value
333 && __is_trivially_constructible(_ValueType2, __decltype(*__first));
334
335 return __uninitialized_copy<__can_memcpy>::
336 __uninit_copy(__first, __last, __result);
337#endif
338 }
339#pragma GCC diagnostic pop
340
341 /// @cond undocumented
342
343 // This is the default implementation of std::uninitialized_fill.
344 template<typename _ForwardIterator, typename _Tp>
345 _GLIBCXX20_CONSTEXPR void
346 __do_uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
347 const _Tp& __x)
348 {
349 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
350 for (; __first != __last; ++__first)
351 std::_Construct(std::__addressof(*__first), __x);
352 __guard.release();
353 }
354
355#if __cplusplus < 201103L
356 // Use template specialization for C++98 when 'if constexpr' can't be used.
357 template<bool _CanMemset>
358 struct __uninitialized_fill
359 {
360 template<typename _ForwardIterator, typename _Tp>
361 static void
362 __uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
363 const _Tp& __x)
364 { std::__do_uninit_fill(__first, __last, __x); }
365 };
366
367 template<>
368 struct __uninitialized_fill<true>
369 {
370 // Overload for generic iterators.
371 template<typename _ForwardIterator, typename _Tp>
372 static void
373 __uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
374 const _Tp& __x)
375 {
376 if (__unwrappable_niter<_ForwardIterator>::__value)
377 __uninit_fill(std::__niter_base(__first),
378 std::__niter_base(__last),
379 __x);
380 else
381 std::__do_uninit_fill(__first, __last, __x);
382 }
383
384 // Overload for pointers.
385 template<typename _Up, typename _Tp>
386 static void
387 __uninit_fill(_Up* __first, _Up* __last, const _Tp& __x)
388 {
389 // Ensure that we don't successfully memset in cases that should be
390 // ill-formed because is_constructible<_Up, const _Tp&> is false.
391 typedef __typeof__(static_cast<_Up>(__x)) __check
392 __attribute__((__unused__));
393
394 if (__first != __last)
395 __builtin_memset(__first, (unsigned char)__x, __last - __first);
396 }
397 };
398#endif
399 /// @endcond
400
401 /**
402 * @brief Copies the value x into the range [first,last).
403 * @param __first A forward iterator.
404 * @param __last A forward iterator.
405 * @param __x The source value.
406 * @return Nothing.
407 *
408 * Like std::fill, but does not require an initialized output range.
409 */
410 template<typename _ForwardIterator, typename _Tp>
411 _GLIBCXX26_CONSTEXPR
412 inline void
413 uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last,
414 const _Tp& __x)
415 {
416 // We would like to use memset to optimize this loop when possible.
417 // As for std::uninitialized_copy, the optimization requires
418 // contiguous iterators and trivially copyable value types,
419 // with the additional requirement that sizeof(_Tp) == 1 because
420 // memset only writes single bytes.
421
422 // FIXME: We could additionally enable this for 1-byte enums.
423 // Maybe any 1-byte Val if is_trivially_constructible<Val, const T&>?
424
426 _ValueType;
427
428#if __cplusplus >= 201103L
429#pragma GCC diagnostic push
430#pragma GCC diagnostic ignored "-Wc++17-extensions"
431#pragma GCC diagnostic ignored "-Wclass-memaccess"
432#if __glibcxx_raw_memory_algorithms >= 202411L // >= C++26
433 if consteval {
434 return std::__do_uninit_fill(__first, __last, __x);
435 }
436#endif
437 if constexpr (__is_byte<_ValueType>::__value)
440 {
441 using _BasePtr = decltype(std::__niter_base(__first));
442 if constexpr (is_pointer<_BasePtr>::value)
443 {
444 void* __dest = std::__niter_base(__first);
445 ptrdiff_t __n = __last - __first;
446 if (__n > 0) [[__likely__]]
447 __builtin_memset(__dest, (unsigned char)__x, __n);
448 return;
449 }
450#if __cpp_lib_concepts
451 else if constexpr (contiguous_iterator<_ForwardIterator>)
452 {
453 auto __dest = std::to_address(__first);
454 auto __n = __last - __first;
455 if (__n > 0) [[__likely__]]
456 __builtin_memset(__dest, (unsigned char)__x, __n);
457 return;
458 }
459#endif
460 }
461 std::__do_uninit_fill(__first, __last, __x);
462#pragma GCC diagnostic pop
463#else // C++98
464 const bool __can_memset = __is_byte<_ValueType>::__value
465 && __is_integer<_Tp>::__value;
466
467 __uninitialized_fill<__can_memset>::__uninit_fill(__first, __last, __x);
468#endif
469 }
470
471 /// @cond undocumented
472
473 // This is the default implementation of std::uninitialized_fill_n.
474 template<typename _ForwardIterator, typename _Size, typename _Tp>
475 _GLIBCXX20_CONSTEXPR
476 _ForwardIterator
477 __do_uninit_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
478 {
479 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
480#if __cplusplus >= 201103L
481#pragma GCC diagnostic push
482#pragma GCC diagnostic ignored "-Wc++17-extensions"
483 if constexpr (is_integral<_Size>::value)
484 // Loop will never terminate if __n is negative.
485 __glibcxx_assert(__n >= 0);
486 else if constexpr (is_floating_point<_Size>::value)
487 // Loop will never terminate if __n is not an integer.
488 __glibcxx_assert(__n >= 0 && static_cast<size_t>(__n) == __n);
489#pragma GCC diagnostic pop
490#endif
491 for (; __n--; ++__first)
492 std::_Construct(std::__addressof(*__first), __x);
493 __guard.release();
494 return __first;
495 }
496
497#if __cplusplus < 201103L
498 // Use template specialization for C++98 when 'if constexpr' can't be used.
499 template<bool _CanMemset>
500 struct __uninitialized_fill_n
501 {
502 template<typename _ForwardIterator, typename _Size, typename _Tp>
503 static _ForwardIterator
504 __uninit_fill_n(_ForwardIterator __first, _Size __n,
505 const _Tp& __x)
506 { return std::__do_uninit_fill_n(__first, __n, __x); }
507 };
508
509 template<>
510 struct __uninitialized_fill_n<true>
511 {
512 // Overload for generic iterators.
513 template<typename _ForwardIterator, typename _Size, typename _Tp>
514 static _ForwardIterator
515 __uninit_fill_n(_ForwardIterator __first, _Size __n,
516 const _Tp& __x)
517 {
518 if (__unwrappable_niter<_ForwardIterator>::__value)
519 {
520 _ForwardIterator __last = __first;
521 std::advance(__last, __n);
522 __uninitialized_fill<true>::__uninit_fill(__first, __last, __x);
523 return __last;
524 }
525 else
526 return std::__do_uninit_fill_n(__first, __n, __x);
527 }
528 };
529#endif
530 /// @endcond
531
532#pragma GCC diagnostic push
533#pragma GCC diagnostic ignored "-Wc++17-extensions"
534#pragma GCC diagnostic ignored "-Wclass-memaccess"
535 // _GLIBCXX_RESOLVE_LIB_DEFECTS
536 // DR 1339. uninitialized_fill_n should return the end of its range
537 /**
538 * @brief Copies the value x into the range [first,first+n).
539 * @param __first A forward iterator.
540 * @param __n The number of copies to make.
541 * @param __x The source value.
542 * @return __first + __n.
543 *
544 * Like std::fill_n, but does not require an initialized output range.
545 */
546 template<typename _ForwardIterator, typename _Size, typename _Tp>
547 _GLIBCXX26_CONSTEXPR
548 inline _ForwardIterator
549 uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
550 {
551 // See uninitialized_fill conditions. We also require _Size to be
552 // an integer. The standard only requires _Size to be decrementable
553 // and contextually convertible to bool, so don't assume first+n works.
554
555 // FIXME: We could additionally enable this for 1-byte enums.
556
558 _ValueType;
559
560#if __cplusplus >= 201103L
561#if __glibcxx_raw_memory_algorithms >= 202411L // >= C++26
562 if consteval {
563 return std::__do_uninit_fill_n(__first, __n, __x);
564 }
565#endif
566 if constexpr (__is_byte<_ValueType>::__value)
567 if constexpr (is_integral<_Tp>::value)
568 if constexpr (is_integral<_Size>::value)
569 {
570 using _BasePtr = decltype(std::__niter_base(__first));
571 if constexpr (is_pointer<_BasePtr>::value)
572 {
573 void* __dest = std::__niter_base(__first);
574 if (__n > 0) [[__likely__]]
575 {
576 __builtin_memset(__dest, (unsigned char)__x, __n);
577 __first += __n;
578 }
579 return __first;
580 }
581#if __cpp_lib_concepts
582 else if constexpr (contiguous_iterator<_ForwardIterator>)
583 {
584 auto __dest = std::to_address(__first);
585 if (__n > 0) [[__likely__]]
586 {
587 __builtin_memset(__dest, (unsigned char)__x, __n);
588 __first += __n;
589 }
590 return __first;
591 }
592#endif
593 }
594 return std::__do_uninit_fill_n(__first, __n, __x);
595#else // C++98
596 const bool __can_memset = __is_byte<_ValueType>::__value
597 && __is_integer<_Tp>::__value
598 && __is_integer<_Size>::__value;
599
600 return __uninitialized_fill_n<__can_memset>::
601 __uninit_fill_n(__first, __n, __x);
602#endif
603 }
604#pragma GCC diagnostic pop
605
606 /// @cond undocumented
607
608 // Extensions: versions of uninitialized_copy, uninitialized_fill,
609 // and uninitialized_fill_n that take an allocator parameter.
610 // We dispatch back to the standard versions when we're given the
611 // default allocator. For nondefault allocators we do not use
612 // any of the POD optimizations.
613
614 template<typename _InputIterator, typename _Sentinel,
615 typename _ForwardIterator, typename _Allocator>
616 _GLIBCXX20_CONSTEXPR
617 _ForwardIterator
618 __uninitialized_copy_a(_InputIterator __first, _Sentinel __last,
619 _ForwardIterator __result, _Allocator& __alloc)
620 {
621 _UninitDestroyGuard<_ForwardIterator, _Allocator>
622 __guard(__result, __alloc);
623
625 for (; __first != __last; ++__first, (void)++__result)
626 __traits::construct(__alloc, std::__addressof(*__result), *__first);
627 __guard.release();
628 return __result;
629 }
630
631#if _GLIBCXX_HOSTED
632 template<typename _InputIterator, typename _Sentinel,
633 typename _ForwardIterator, typename _Tp>
634 _GLIBCXX20_CONSTEXPR
635 inline _ForwardIterator
636 __uninitialized_copy_a(_InputIterator __first, _Sentinel __last,
637 _ForwardIterator __result, allocator<_Tp>&)
638 {
639#ifdef __cpp_lib_is_constant_evaluated
640 if (std::is_constant_evaluated())
641 return std::__do_uninit_copy(std::move(__first), __last, __result);
642#endif
643
644#ifdef __glibcxx_ranges
645 if constexpr (!is_same_v<_InputIterator, _Sentinel>)
646 {
647 // Convert to a common range if possible, to benefit from memcpy
648 // optimizations that std::uninitialized_copy might use.
649 if constexpr (sized_sentinel_for<_Sentinel, _InputIterator>
650 && random_access_iterator<_InputIterator>)
651 return std::uninitialized_copy(__first,
652 __first + (__last - __first),
653 __result);
654 else // Just use default implementation.
655 return std::__do_uninit_copy(std::move(__first), __last, __result);
656 }
657 else
658 return std::uninitialized_copy(std::move(__first), __last, __result);
659#else
660 return std::uninitialized_copy(__first, __last, __result);
661#endif
662 }
663#endif
664
665#if __cplusplus >= 201103L
666 template<typename _ITp, typename _IRef, typename _IPtr, typename _OTp,
667 typename _Tp>
668 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
669 __uninitialized_copy_a(
670 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr> __first,
671 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr> __last,
672 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*> __result,
674
675 template<typename _Iter, typename _OTp, typename _Tp>
676 __enable_if_t<__is_random_access_iter<_Iter>::value,
677 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>>
678 __uninitialized_copy_a(_Iter __first, _Iter __last,
679 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*> __result,
681
682 template<typename _ITp, typename _IRef, typename _IPtr, typename _OTp,
683 typename _Tp>
684 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
685 __uninitialized_move_a(
686 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr> __first,
687 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr> __last,
688 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*> __result,
690#endif
691
692 template<typename _InputIterator, typename _ForwardIterator,
693 typename _Allocator>
694 _GLIBCXX20_CONSTEXPR
695 inline _ForwardIterator
696 __uninitialized_move_a(_InputIterator __first, _InputIterator __last,
697 _ForwardIterator __result, _Allocator& __alloc)
698 {
699 return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
700 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
701 __result, __alloc);
702 }
703
704 template<typename _InputIterator, typename _ForwardIterator,
705 typename _Allocator>
706 _GLIBCXX20_CONSTEXPR
707 inline _ForwardIterator
708 __uninitialized_move_if_noexcept_a(_InputIterator __first,
709 _InputIterator __last,
710 _ForwardIterator __result,
711 _Allocator& __alloc)
712 {
713 return std::__uninitialized_copy_a
714 (_GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__first),
715 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__last), __result, __alloc);
716 }
717
718 template<typename _ForwardIterator, typename _Tp, typename _Allocator>
719 _GLIBCXX20_CONSTEXPR
720 void
721 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
722 const _Tp& __x, _Allocator& __alloc)
723 {
724 _UninitDestroyGuard<_ForwardIterator, _Allocator>
725 __guard(__first, __alloc);
726
727 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
728 for (; __first != __last; ++__first)
729 __traits::construct(__alloc, std::__addressof(*__first), __x);
730
731 __guard.release();
732 }
733
734#if _GLIBCXX_HOSTED
735 template<typename _ForwardIterator, typename _Tp, typename _Tp2>
736 _GLIBCXX20_CONSTEXPR
737 inline void
738 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
739 const _Tp& __x, allocator<_Tp2>&)
740 {
741#ifdef __cpp_lib_is_constant_evaluated
742 if (std::is_constant_evaluated())
743 return std::__do_uninit_fill(__first, __last, __x);
744#endif
745 std::uninitialized_fill(__first, __last, __x);
746 }
747#endif
748
749 template<typename _ForwardIterator, typename _Size, typename _Tp,
750 typename _Allocator>
751 _GLIBCXX20_CONSTEXPR
752 _ForwardIterator
753 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
754 const _Tp& __x, _Allocator& __alloc)
755 {
756 _UninitDestroyGuard<_ForwardIterator, _Allocator>
757 __guard(__first, __alloc);
758 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
759 for (; __n > 0; --__n, (void) ++__first)
760 __traits::construct(__alloc, std::__addressof(*__first), __x);
761 __guard.release();
762 return __first;
763 }
764
765#if _GLIBCXX_HOSTED
766 template<typename _ForwardIterator, typename _Size, typename _Tp,
767 typename _Tp2>
768 _GLIBCXX20_CONSTEXPR
769 inline _ForwardIterator
770 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
771 const _Tp& __x, allocator<_Tp2>&)
772 {
773#ifdef __cpp_lib_is_constant_evaluated
774 if (std::is_constant_evaluated())
775 return std::__do_uninit_fill_n(__first, __n, __x);
776#endif
777 return std::uninitialized_fill_n(__first, __n, __x);
778 }
779#endif
780
781 // Extensions: __uninitialized_copy_move, __uninitialized_move_copy,
782 // __uninitialized_fill_move, __uninitialized_move_fill.
783 // All of these algorithms take a user-supplied allocator, which is used
784 // for construction and destruction.
785
786 // __uninitialized_copy_move
787 // Copies [first1, last1) into [result, result + (last1 - first1)), and
788 // move [first2, last2) into
789 // [result, result + (last1 - first1) + (last2 - first2)).
790 template<typename _InputIterator1, typename _InputIterator2,
791 typename _ForwardIterator, typename _Allocator>
792 inline _ForwardIterator
793 __uninitialized_copy_move(_InputIterator1 __first1,
794 _InputIterator1 __last1,
795 _InputIterator2 __first2,
796 _InputIterator2 __last2,
797 _ForwardIterator __result,
798 _Allocator& __alloc)
799 {
800 _ForwardIterator __mid = std::__uninitialized_copy_a(__first1, __last1,
801 __result, __alloc);
802 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__result,
803 __alloc);
804 __result = __mid; // Everything up to __mid is now guarded.
805 __result = std::__uninitialized_move_a(__first2, __last2, __mid, __alloc);
806 __guard.release();
807 return __result;
808 }
809
810 // __uninitialized_move_copy
811 // Moves [first1, last1) into [result, result + (last1 - first1)), and
812 // copies [first2, last2) into
813 // [result, result + (last1 - first1) + (last2 - first2)).
814 template<typename _InputIterator1, typename _InputIterator2,
815 typename _ForwardIterator, typename _Allocator>
816 inline _ForwardIterator
817 __uninitialized_move_copy(_InputIterator1 __first1,
818 _InputIterator1 __last1,
819 _InputIterator2 __first2,
820 _InputIterator2 __last2,
821 _ForwardIterator __result,
822 _Allocator& __alloc)
823 {
824 _ForwardIterator __mid = std::__uninitialized_move_a(__first1, __last1,
825 __result, __alloc);
826 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__result,
827 __alloc);
828 __result = __mid; // Everything up to __mid is now guarded.
829 __result = std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc);
830 __guard.release();
831 return __result;
832 }
833
834 // __uninitialized_fill_move
835 // Fills [result, mid) with x, and moves [first, last) into
836 // [mid, mid + (last - first)).
837 template<typename _ForwardIterator, typename _Tp, typename _InputIterator,
838 typename _Allocator>
839 inline _ForwardIterator
840 __uninitialized_fill_move(_ForwardIterator __result, _ForwardIterator __mid,
841 const _Tp& __x, _InputIterator __first,
842 _InputIterator __last, _Allocator& __alloc)
843 {
844 std::__uninitialized_fill_a(__result, __mid, __x, __alloc);
845 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__result,
846 __alloc);
847 __result = __mid; // Everything up to __mid is now guarded.
848 __result = std::__uninitialized_move_a(__first, __last, __mid, __alloc);
849 __guard.release();
850 return __result;
851 }
852
853 // __uninitialized_move_fill
854 // Moves [first1, last1) into [first2, first2 + (last1 - first1)), and
855 // fills [first2 + (last1 - first1), last2) with x.
856 template<typename _InputIterator, typename _ForwardIterator, typename _Tp,
857 typename _Allocator>
858 inline void
859 __uninitialized_move_fill(_InputIterator __first1, _InputIterator __last1,
860 _ForwardIterator __first2,
861 _ForwardIterator __last2, const _Tp& __x,
862 _Allocator& __alloc)
863 {
864 _ForwardIterator __mid2 = std::__uninitialized_move_a(__first1, __last1,
865 __first2,
866 __alloc);
867 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__first2,
868 __alloc);
869 __first2 = __mid2; // Everything up to __mid2 is now guarded.
870 std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc);
871 __guard.release();
872 }
873
874 /// @endcond
875
876#if __cplusplus >= 201103L
877 /// @cond undocumented
878
879 // Extensions: __uninitialized_default, __uninitialized_default_n,
880 // __uninitialized_default_a, __uninitialized_default_n_a.
881
882 template<bool _TrivialValueType>
883 struct __uninitialized_default_1
884 {
885 template<typename _ForwardIterator>
886 _GLIBCXX26_CONSTEXPR
887 static void
888 __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
889 {
890 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
891 for (; __first != __last; ++__first)
893 __guard.release();
894 }
895 };
896
897 template<>
898 struct __uninitialized_default_1<true>
899 {
900 template<typename _ForwardIterator>
901 _GLIBCXX26_CONSTEXPR
902 static void
903 __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
904 {
905 if (__first == __last)
906 return;
907
908 typename iterator_traits<_ForwardIterator>::value_type* __val
909 = std::addressof(*__first);
910 std::_Construct(__val);
911 if (++__first != __last)
912 std::fill(__first, __last, *__val);
913 }
914 };
915
916 template<bool _TrivialValueType>
917 struct __uninitialized_default_n_1
918 {
919 template<typename _ForwardIterator, typename _Size>
920 _GLIBCXX20_CONSTEXPR
921 static _ForwardIterator
922 __uninit_default_n(_ForwardIterator __first, _Size __n)
923 {
924 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
925 for (; __n > 0; --__n, (void) ++__first)
927 __guard.release();
928 return __first;
929 }
930 };
931
932 template<>
933 struct __uninitialized_default_n_1<true>
934 {
935 template<typename _ForwardIterator, typename _Size>
936 _GLIBCXX20_CONSTEXPR
937 static _ForwardIterator
938 __uninit_default_n(_ForwardIterator __first, _Size __n)
939 {
940 if (__n > 0)
941 {
942 typename iterator_traits<_ForwardIterator>::value_type* __val
943 = std::addressof(*__first);
944 std::_Construct(__val);
945 ++__first;
946 __first = std::fill_n(__first, __n - 1, *__val);
947 }
948 return __first;
949 }
950 };
951
952 // __uninitialized_default
953 // Fills [first, last) with value-initialized value_types.
954 template<typename _ForwardIterator>
955 _GLIBCXX20_CONSTEXPR
956 inline void
957 __uninitialized_default(_ForwardIterator __first,
958 _ForwardIterator __last)
959 {
960#ifdef __cpp_lib_is_constant_evaluated
961 if (std::is_constant_evaluated())
962 return __uninitialized_default_1<false>::
963 __uninit_default(__first, __last);
964#endif
965
967 _ValueType;
968 // trivial types can have deleted assignment
969 const bool __assignable = is_copy_assignable<_ValueType>::value;
970
971 std::__uninitialized_default_1<__is_trivial(_ValueType)
972 && __assignable>::
973 __uninit_default(__first, __last);
974 }
975
976 // __uninitialized_default_n
977 // Fills [first, first + n) with value-initialized value_types.
978 template<typename _ForwardIterator, typename _Size>
979 _GLIBCXX20_CONSTEXPR
980 inline _ForwardIterator
981 __uninitialized_default_n(_ForwardIterator __first, _Size __n)
982 {
983#ifdef __cpp_lib_is_constant_evaluated
984 if (std::is_constant_evaluated())
985 return __uninitialized_default_n_1<false>::
986 __uninit_default_n(__first, __n);
987#endif
988
990 _ValueType;
991 // See uninitialized_fill_n for the conditions for using std::fill_n.
992 constexpr bool __can_fill
993 = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
994
995 return __uninitialized_default_n_1<__is_trivial(_ValueType)
996 && __can_fill>::
997 __uninit_default_n(__first, __n);
998 }
999
1000
1001 // __uninitialized_default_a
1002 // Fills [first, last) with value_types constructed by the allocator
1003 // alloc, with no arguments passed to the construct call.
1004 template<typename _ForwardIterator, typename _Allocator>
1005 void
1006 __uninitialized_default_a(_ForwardIterator __first,
1007 _ForwardIterator __last,
1008 _Allocator& __alloc)
1009 {
1010 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__first,
1011 __alloc);
1012 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
1013 for (; __first != __last; ++__first)
1014 __traits::construct(__alloc, std::addressof(*__first));
1015 __guard.release();
1016 }
1017
1018#if _GLIBCXX_HOSTED
1019 template<typename _ForwardIterator, typename _Tp>
1020 inline void
1021 __uninitialized_default_a(_ForwardIterator __first,
1022 _ForwardIterator __last,
1024 { std::__uninitialized_default(__first, __last); }
1025#endif
1026
1027 // __uninitialized_default_n_a
1028 // Fills [first, first + n) with value_types constructed by the allocator
1029 // alloc, with no arguments passed to the construct call.
1030 template<typename _ForwardIterator, typename _Size, typename _Allocator>
1031 _GLIBCXX20_CONSTEXPR _ForwardIterator
1032 __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
1033 _Allocator& __alloc)
1034 {
1035 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__first,
1036 __alloc);
1037 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
1038 for (; __n > 0; --__n, (void) ++__first)
1039 __traits::construct(__alloc, std::addressof(*__first));
1040 __guard.release();
1041 return __first;
1042 }
1043
1044#if _GLIBCXX_HOSTED
1045 // __uninitialized_default_n_a specialization for std::allocator,
1046 // which ignores the allocator and value-initializes the elements.
1047 template<typename _ForwardIterator, typename _Size, typename _Tp>
1048 _GLIBCXX20_CONSTEXPR
1049 inline _ForwardIterator
1050 __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
1052 { return std::__uninitialized_default_n(__first, __n); }
1053#endif
1054
1055 template<bool _TrivialValueType>
1056 struct __uninitialized_default_novalue_1
1057 {
1058 template<typename _ForwardIterator>
1059 _GLIBCXX26_CONSTEXPR
1060 static void
1061 __uninit_default_novalue(_ForwardIterator __first,
1062 _ForwardIterator __last)
1063 {
1064 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
1065 for (; __first != __last; ++__first)
1066 std::_Construct_novalue(std::addressof(*__first));
1067 __guard.release();
1068 }
1069 };
1070
1071 template<>
1072 struct __uninitialized_default_novalue_1<true>
1073 {
1074 template<typename _ForwardIterator>
1075 _GLIBCXX26_CONSTEXPR
1076 static void
1077 __uninit_default_novalue(_ForwardIterator, _ForwardIterator)
1078 {
1079 }
1080 };
1081
1082 template<bool _TrivialValueType>
1083 struct __uninitialized_default_novalue_n_1
1084 {
1085 template<typename _ForwardIterator, typename _Size>
1086 _GLIBCXX26_CONSTEXPR
1087 static _ForwardIterator
1088 __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
1089 {
1090 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
1091 for (; __n > 0; --__n, (void) ++__first)
1092 std::_Construct_novalue(std::addressof(*__first));
1093 __guard.release();
1094 return __first;
1095 }
1096 };
1097
1098 template<>
1099 struct __uninitialized_default_novalue_n_1<true>
1100 {
1101 template<typename _ForwardIterator, typename _Size>
1102 _GLIBCXX26_CONSTEXPR
1103 static _ForwardIterator
1104 __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
1105 { return std::next(__first, __n); }
1106 };
1107
1108 // __uninitialized_default_novalue
1109 // Fills [first, last) with default-initialized value_types.
1110 template<typename _ForwardIterator>
1111 _GLIBCXX26_CONSTEXPR
1112 inline void
1113 __uninitialized_default_novalue(_ForwardIterator __first,
1114 _ForwardIterator __last)
1115 {
1117 _ValueType;
1118
1119 std::__uninitialized_default_novalue_1<
1121 __uninit_default_novalue(__first, __last);
1122 }
1123
1124 // __uninitialized_default_novalue_n
1125 // Fills [first, first + n) with default-initialized value_types.
1126 template<typename _ForwardIterator, typename _Size>
1127 _GLIBCXX26_CONSTEXPR
1128 inline _ForwardIterator
1129 __uninitialized_default_novalue_n(_ForwardIterator __first, _Size __n)
1130 {
1132 _ValueType;
1133
1134 return __uninitialized_default_novalue_n_1<
1136 __uninit_default_novalue_n(__first, __n);
1137 }
1138
1139 template<typename _InputIterator, typename _Size,
1140 typename _ForwardIterator>
1141 _GLIBCXX26_CONSTEXPR
1142 _ForwardIterator
1143 __uninitialized_copy_n(_InputIterator __first, _Size __n,
1144 _ForwardIterator __result, input_iterator_tag)
1145 {
1146 _UninitDestroyGuard<_ForwardIterator> __guard(__result);
1147 for (; __n > 0; --__n, (void) ++__first, ++__result)
1148 std::_Construct(std::addressof(*__result), *__first);
1149 __guard.release();
1150 return __result;
1151 }
1152
1153 template<typename _RandomAccessIterator, typename _Size,
1154 typename _ForwardIterator>
1155 _GLIBCXX26_CONSTEXPR
1156 inline _ForwardIterator
1157 __uninitialized_copy_n(_RandomAccessIterator __first, _Size __n,
1158 _ForwardIterator __result,
1160 { return std::uninitialized_copy(__first, __first + __n, __result); }
1161
1162 template<typename _InputIterator, typename _Size,
1163 typename _ForwardIterator>
1164 _GLIBCXX26_CONSTEXPR
1166 __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
1167 _ForwardIterator __result, input_iterator_tag)
1168 {
1169 _UninitDestroyGuard<_ForwardIterator> __guard(__result);
1170 for (; __n > 0; --__n, (void) ++__first, ++__result)
1171 std::_Construct(std::addressof(*__result), *__first);
1172 __guard.release();
1173 return {__first, __result};
1174 }
1175
1176 template<typename _RandomAccessIterator, typename _Size,
1177 typename _ForwardIterator>
1178 _GLIBCXX26_CONSTEXPR
1180 __uninitialized_copy_n_pair(_RandomAccessIterator __first, _Size __n,
1181 _ForwardIterator __result,
1183 {
1184 auto __second_res = uninitialized_copy(__first, __first + __n, __result);
1185 auto __first_res = std::next(__first, __n);
1186 return {__first_res, __second_res};
1187 }
1188
1189 /// @endcond
1190
1191 /**
1192 * @brief Copies the range [first,first+n) into result.
1193 * @param __first An input iterator.
1194 * @param __n The number of elements to copy.
1195 * @param __result An output iterator.
1196 * @return __result + __n
1197 * @since C++11
1198 *
1199 * Like copy_n(), but does not require an initialized output range.
1200 */
1201 template<typename _InputIterator, typename _Size, typename _ForwardIterator>
1202 _GLIBCXX26_CONSTEXPR
1203 inline _ForwardIterator
1204 uninitialized_copy_n(_InputIterator __first, _Size __n,
1205 _ForwardIterator __result)
1206 { return std::__uninitialized_copy_n(__first, __n, __result,
1207 std::__iter_concept_or_category(__first)); }
1208
1209 /// @cond undocumented
1210 template<typename _InputIterator, typename _Size, typename _ForwardIterator>
1211 _GLIBCXX26_CONSTEXPR
1213 __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
1214 _ForwardIterator __result)
1215 {
1216 return
1217 std::__uninitialized_copy_n_pair(__first, __n, __result,
1218 std::__iter_concept_or_category(__first));
1219 }
1220 /// @endcond
1221#endif
1222
1223#ifdef __glibcxx_raw_memory_algorithms // C++ >= 17
1224 /**
1225 * @brief Default-initializes objects in the range [first,last).
1226 * @param __first A forward iterator.
1227 * @param __last A forward iterator.
1228 * @since C++17
1229 */
1230 template <typename _ForwardIterator>
1231 _GLIBCXX26_CONSTEXPR
1232 inline void
1233 uninitialized_default_construct(_ForwardIterator __first,
1234 _ForwardIterator __last)
1235 {
1236 std::__uninitialized_default_novalue(__first, __last);
1237 }
1238
1239 /**
1240 * @brief Default-initializes objects in the range [first,first+count).
1241 * @param __first A forward iterator.
1242 * @param __count The number of objects to construct.
1243 * @return __first + __count
1244 * @since C++17
1245 */
1246 template <typename _ForwardIterator, typename _Size>
1247 _GLIBCXX26_CONSTEXPR
1248 inline _ForwardIterator
1249 uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
1250 {
1251 return std::__uninitialized_default_novalue_n(__first, __count);
1252 }
1253
1254 /**
1255 * @brief Value-initializes objects in the range [first,last).
1256 * @param __first A forward iterator.
1257 * @param __last A forward iterator.
1258 * @since C++17
1259 */
1260 template <typename _ForwardIterator>
1261 _GLIBCXX26_CONSTEXPR
1262 inline void
1263 uninitialized_value_construct(_ForwardIterator __first,
1264 _ForwardIterator __last)
1265 {
1266 return std::__uninitialized_default(__first, __last);
1267 }
1268
1269 /**
1270 * @brief Value-initializes objects in the range [first,first+count).
1271 * @param __first A forward iterator.
1272 * @param __count The number of objects to construct.
1273 * @return __result + __count
1274 * @since C++17
1275 */
1276 template <typename _ForwardIterator, typename _Size>
1277 _GLIBCXX26_CONSTEXPR
1278 inline _ForwardIterator
1279 uninitialized_value_construct_n(_ForwardIterator __first, _Size __count)
1280 {
1281 return std::__uninitialized_default_n(__first, __count);
1282 }
1283
1284 /**
1285 * @brief Move-construct from the range [first,last) into result.
1286 * @param __first An input iterator.
1287 * @param __last An input iterator.
1288 * @param __result An output iterator.
1289 * @return __result + (__first - __last)
1290 * @since C++17
1291 */
1292 template <typename _InputIterator, typename _ForwardIterator>
1293 _GLIBCXX26_CONSTEXPR
1294 inline _ForwardIterator
1295 uninitialized_move(_InputIterator __first, _InputIterator __last,
1296 _ForwardIterator __result)
1297 {
1298 return std::uninitialized_copy(std::make_move_iterator(__first),
1299 std::make_move_iterator(__last),
1300 __result);
1301 }
1302
1303 /**
1304 * @brief Move-construct from the range [first,first+count) into result.
1305 * @param __first An input iterator.
1306 * @param __count The number of objects to initialize.
1307 * @param __result An output iterator.
1308 * @return __result + __count
1309 * @since C++17
1310 */
1311 template <typename _InputIterator, typename _Size, typename _ForwardIterator>
1312 _GLIBCXX26_CONSTEXPR
1314 uninitialized_move_n(_InputIterator __first, _Size __count,
1315 _ForwardIterator __result)
1316 {
1317 auto __res
1318 = std::__uninitialized_copy_n_pair(std::make_move_iterator(__first),
1319 __count, __result);
1320 return {__res.first.base(), __res.second};
1321 }
1322#endif // __glibcxx_raw_memory_algorithms
1323
1324#if __cplusplus >= 201103L
1325 /// @cond undocumented
1326
1327 template<typename _Tp, typename _Up, typename _Allocator>
1328 _GLIBCXX20_CONSTEXPR
1329 inline void
1330 __relocate_object_a(_Tp* __restrict __dest, _Up* __restrict __orig,
1331 _Allocator& __alloc)
1332 noexcept(noexcept(std::allocator_traits<_Allocator>::construct(__alloc,
1333 __dest, std::move(*__orig)))
1335 __alloc, std::addressof(*__orig))))
1336 {
1337 typedef std::allocator_traits<_Allocator> __traits;
1338 __traits::construct(__alloc, __dest, std::move(*__orig));
1339 __traits::destroy(__alloc, std::addressof(*__orig));
1340 }
1341
1342 // This class may be specialized for specific types.
1343 // Also known as is_trivially_relocatable.
1344 template<typename _Tp, typename = void>
1345 struct __is_bitwise_relocatable
1346 : __bool_constant<__is_trivial(_Tp)>
1347 { };
1348
1349 template <typename _InputIterator, typename _ForwardIterator,
1350 typename _Allocator>
1351 _GLIBCXX20_CONSTEXPR
1352 inline _ForwardIterator
1353 __relocate_a_1(_InputIterator __first, _InputIterator __last,
1354 _ForwardIterator __result, _Allocator& __alloc)
1355 noexcept(noexcept(std::__relocate_object_a(std::addressof(*__result),
1356 std::addressof(*__first),
1357 __alloc)))
1358 {
1360 _ValueType;
1362 _ValueType2;
1363 static_assert(std::is_same<_ValueType, _ValueType2>::value,
1364 "relocation is only possible for values of the same type");
1365 _ForwardIterator __cur = __result;
1366 for (; __first != __last; ++__first, (void)++__cur)
1367 std::__relocate_object_a(std::addressof(*__cur),
1368 std::addressof(*__first), __alloc);
1369 return __cur;
1370 }
1371
1372#if _GLIBCXX_HOSTED
1373 template <typename _Tp, typename _Up>
1374 _GLIBCXX20_CONSTEXPR
1375 inline __enable_if_t<std::__is_bitwise_relocatable<_Tp>::value, _Tp*>
1376 __relocate_a_1(_Tp* __first, _Tp* __last,
1377 _Tp* __result,
1378 [[__maybe_unused__]] allocator<_Up>& __alloc) noexcept
1379 {
1380 ptrdiff_t __count = __last - __first;
1381 if (__count > 0)
1382 {
1383#ifdef __cpp_lib_is_constant_evaluated
1384 if (std::is_constant_evaluated())
1385 {
1386 // Can't use memcpy. Wrap the pointer so that __relocate_a_1
1387 // resolves to the non-trivial overload above.
1388 __gnu_cxx::__normal_iterator<_Tp*, void> __out(__result);
1389 __out = std::__relocate_a_1(__first, __last, __out, __alloc);
1390 return __out.base();
1391 }
1392#endif
1393 __builtin_memcpy(__result, __first, __count * sizeof(_Tp));
1394 }
1395 return __result + __count;
1396 }
1397#endif
1398
1399 template <typename _InputIterator, typename _ForwardIterator,
1400 typename _Allocator>
1401 _GLIBCXX20_CONSTEXPR
1402 inline _ForwardIterator
1403 __relocate_a(_InputIterator __first, _InputIterator __last,
1404 _ForwardIterator __result, _Allocator& __alloc)
1405 noexcept(noexcept(__relocate_a_1(std::__niter_base(__first),
1406 std::__niter_base(__last),
1407 std::__niter_base(__result), __alloc)))
1408 {
1409 return std::__relocate_a_1(std::__niter_base(__first),
1410 std::__niter_base(__last),
1411 std::__niter_base(__result), __alloc);
1412 }
1413
1414 /// @endcond
1415#endif // C++11
1416
1417 /// @} group memory
1418
1419_GLIBCXX_END_NAMESPACE_VERSION
1420} // namespace
1421
1422#endif /* _STL_UNINITIALIZED_H */
_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 * to_address(_Tp *__ptr) noexcept
Obtain address referenced by a pointer to an object.
Definition ptr_traits.h:232
typename remove_pointer< _Tp >::type remove_pointer_t
Alias template for remove_pointer.
Definition type_traits:2352
pair(_T1, _T2) -> pair< _T1, _T2 >
Two pairs are equal iff their members are equal.
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...
Definition move.h:176
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition move.h:138
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
ISO C++ entities toplevel namespace is std.
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr void _Construct(_Tp *__p, _Args &&... __args)
constexpr void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
constexpr void _Destroy(_ForwardIterator __first, _ForwardIterator __last)
is_integral
Definition type_traits:541
is_pointer
Definition type_traits:628
is_copy_assignable
Definition type_traits:1367
is_trivially_default_constructible
Definition type_traits:1435
Uniform interface to all allocator types.
static constexpr void construct(_Alloc &__a, _Tp *__p, _Args &&... __args) noexcept(_S_nothrow_construct< _Tp, _Args... >())
Construct an object of type _Tp
static constexpr void destroy(_Alloc &__a, _Tp *__p) noexcept(_S_nothrow_destroy< _Tp >())
Destroy an object of type _Tp.
The standard allocator, as per C++03 [20.4.1].
Definition allocator.h:134
Traits class for iterators.
Struct holding two objects of arbitrary type.
Definition stl_pair.h:304
Marking input iterators.
Random-access iterators support a superset of bidirectional iterator operations.
Uniform interface to C++98 and C++11 allocators.