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 *
407 * Like std::fill, but does not require an initialized output range.
408 */
409 template<typename _ForwardIterator, typename _Tp>
410 _GLIBCXX26_CONSTEXPR
411 inline void
412 uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last,
413 const _Tp& __x)
414 {
415 // We would like to use memset to optimize this loop when possible.
416 // As for std::uninitialized_copy, the optimization requires
417 // contiguous iterators and trivially copyable value types,
418 // with the additional requirement that sizeof(_Tp) == 1 because
419 // memset only writes single bytes.
420
421 // FIXME: We could additionally enable this for 1-byte enums.
422 // Maybe any 1-byte Val if is_trivially_constructible<Val, const T&>?
423
425 _ValueType;
426
427#if __cplusplus >= 201103L
428#pragma GCC diagnostic push
429#pragma GCC diagnostic ignored "-Wc++17-extensions"
430#pragma GCC diagnostic ignored "-Wclass-memaccess"
431#if __glibcxx_raw_memory_algorithms >= 202411L // >= C++26
432 if consteval {
433 return std::__do_uninit_fill(__first, __last, __x);
434 }
435#endif
436 if constexpr (__is_byte<_ValueType>::__value)
439 {
440 using _BasePtr = decltype(std::__niter_base(__first));
441 if constexpr (is_pointer<_BasePtr>::value)
442 {
443 void* __dest = std::__niter_base(__first);
444 ptrdiff_t __n = __last - __first;
445 if (__n > 0) [[__likely__]]
446 __builtin_memset(__dest, (unsigned char)__x, __n);
447 return;
448 }
449#if __cpp_lib_concepts
450 else if constexpr (contiguous_iterator<_ForwardIterator>)
451 {
452 auto __dest = std::to_address(__first);
453 auto __n = __last - __first;
454 if (__n > 0) [[__likely__]]
455 __builtin_memset(__dest, (unsigned char)__x, __n);
456 return;
457 }
458#endif
459 }
460 std::__do_uninit_fill(__first, __last, __x);
461#pragma GCC diagnostic pop
462#else // C++98
463 const bool __can_memset = __is_byte<_ValueType>::__value
464 && __is_integer<_Tp>::__value;
465
466 __uninitialized_fill<__can_memset>::__uninit_fill(__first, __last, __x);
467#endif
468 }
469
470 /// @cond undocumented
471
472 // This is the default implementation of std::uninitialized_fill_n.
473 template<typename _ForwardIterator, typename _Size, typename _Tp>
474 _GLIBCXX20_CONSTEXPR
475 _ForwardIterator
476 __do_uninit_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
477 {
478 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
479#if __cplusplus >= 201103L
480#pragma GCC diagnostic push
481#pragma GCC diagnostic ignored "-Wc++17-extensions"
482 if constexpr (is_integral<_Size>::value)
483 // Loop will never terminate if __n is negative.
484 __glibcxx_assert(__n >= 0);
485 else if constexpr (is_floating_point<_Size>::value)
486 // Loop will never terminate if __n is not an integer.
487 __glibcxx_assert(__n >= 0 && static_cast<size_t>(__n) == __n);
488#pragma GCC diagnostic pop
489#endif
490 for (; __n--; ++__first)
491 std::_Construct(std::__addressof(*__first), __x);
492 __guard.release();
493 return __first;
494 }
495
496#if __cplusplus < 201103L
497 // Use template specialization for C++98 when 'if constexpr' can't be used.
498 template<bool _CanMemset>
499 struct __uninitialized_fill_n
500 {
501 template<typename _ForwardIterator, typename _Size, typename _Tp>
502 static _ForwardIterator
503 __uninit_fill_n(_ForwardIterator __first, _Size __n,
504 const _Tp& __x)
505 { return std::__do_uninit_fill_n(__first, __n, __x); }
506 };
507
508 template<>
509 struct __uninitialized_fill_n<true>
510 {
511 // Overload for generic iterators.
512 template<typename _ForwardIterator, typename _Size, typename _Tp>
513 static _ForwardIterator
514 __uninit_fill_n(_ForwardIterator __first, _Size __n,
515 const _Tp& __x)
516 {
517 if (__unwrappable_niter<_ForwardIterator>::__value)
518 {
519 _ForwardIterator __last = __first;
520 std::advance(__last, __n);
521 __uninitialized_fill<true>::__uninit_fill(__first, __last, __x);
522 return __last;
523 }
524 else
525 return std::__do_uninit_fill_n(__first, __n, __x);
526 }
527 };
528#endif
529 /// @endcond
530
531#pragma GCC diagnostic push
532#pragma GCC diagnostic ignored "-Wc++17-extensions"
533#pragma GCC diagnostic ignored "-Wclass-memaccess"
534 // _GLIBCXX_RESOLVE_LIB_DEFECTS
535 // DR 1339. uninitialized_fill_n should return the end of its range
536 /**
537 * @brief Copies the value x into the range [first,first+n).
538 * @param __first A forward iterator.
539 * @param __n The number of copies to make.
540 * @param __x The source value.
541 * @return __first + __n.
542 *
543 * Like std::fill_n, but does not require an initialized output range.
544 */
545 template<typename _ForwardIterator, typename _Size, typename _Tp>
546 _GLIBCXX26_CONSTEXPR
547 inline _ForwardIterator
548 uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
549 {
550 // See uninitialized_fill conditions. We also require _Size to be
551 // an integer. The standard only requires _Size to be decrementable
552 // and contextually convertible to bool, so don't assume first+n works.
553
554 // FIXME: We could additionally enable this for 1-byte enums.
555
557 _ValueType;
558
559#if __cplusplus >= 201103L
560#if __glibcxx_raw_memory_algorithms >= 202411L // >= C++26
561 if consteval {
562 return std::__do_uninit_fill_n(__first, __n, __x);
563 }
564#endif
565 if constexpr (__is_byte<_ValueType>::__value)
566 if constexpr (is_integral<_Tp>::value)
567 if constexpr (is_integral<_Size>::value)
568 {
569 using _BasePtr = decltype(std::__niter_base(__first));
570 if constexpr (is_pointer<_BasePtr>::value)
571 {
572 void* __dest = std::__niter_base(__first);
573 if (__n > 0) [[__likely__]]
574 {
575 __builtin_memset(__dest, (unsigned char)__x, __n);
576 __first += __n;
577 }
578 return __first;
579 }
580#if __cpp_lib_concepts
581 else if constexpr (contiguous_iterator<_ForwardIterator>)
582 {
583 auto __dest = std::to_address(__first);
584 if (__n > 0) [[__likely__]]
585 {
586 __builtin_memset(__dest, (unsigned char)__x, __n);
587 __first += __n;
588 }
589 return __first;
590 }
591#endif
592 }
593 return std::__do_uninit_fill_n(__first, __n, __x);
594#else // C++98
595 const bool __can_memset = __is_byte<_ValueType>::__value
596 && __is_integer<_Tp>::__value
597 && __is_integer<_Size>::__value;
598
599 return __uninitialized_fill_n<__can_memset>::
600 __uninit_fill_n(__first, __n, __x);
601#endif
602 }
603#pragma GCC diagnostic pop
604
605 /// @cond undocumented
606
607 // Extensions: versions of uninitialized_copy, uninitialized_fill,
608 // and uninitialized_fill_n that take an allocator parameter.
609 // We dispatch back to the standard versions when we're given the
610 // default allocator. For nondefault allocators we do not use
611 // any of the POD optimizations.
612
613 template<typename _InputIterator, typename _Sentinel,
614 typename _ForwardIterator, typename _Allocator>
615 _GLIBCXX20_CONSTEXPR
616 _ForwardIterator
617 __uninitialized_copy_a(_InputIterator __first, _Sentinel __last,
618 _ForwardIterator __result, _Allocator& __alloc)
619 {
620 _UninitDestroyGuard<_ForwardIterator, _Allocator>
621 __guard(__result, __alloc);
622
624 for (; __first != __last; ++__first, (void)++__result)
625 __traits::construct(__alloc, std::__addressof(*__result), *__first);
626 __guard.release();
627 return __result;
628 }
629
630#if _GLIBCXX_HOSTED
631 template<typename _InputIterator, typename _Sentinel,
632 typename _ForwardIterator, typename _Tp>
633 _GLIBCXX20_CONSTEXPR
634 inline _ForwardIterator
635 __uninitialized_copy_a(_InputIterator __first, _Sentinel __last,
636 _ForwardIterator __result, allocator<_Tp>&)
637 {
638#ifdef __cpp_lib_is_constant_evaluated
639 if (std::is_constant_evaluated())
640 return std::__do_uninit_copy(std::move(__first), __last, __result);
641#endif
642
643#ifdef __glibcxx_ranges
644 if constexpr (!is_same_v<_InputIterator, _Sentinel>)
645 {
646 // Convert to a common range if possible, to benefit from memcpy
647 // optimizations that std::uninitialized_copy might use.
648 if constexpr (sized_sentinel_for<_Sentinel, _InputIterator>
649 && random_access_iterator<_InputIterator>)
650 return std::uninitialized_copy(__first,
651 __first + (__last - __first),
652 __result);
653 else // Just use default implementation.
654 return std::__do_uninit_copy(std::move(__first), __last, __result);
655 }
656 else
657 return std::uninitialized_copy(std::move(__first), __last, __result);
658#else
659 return std::uninitialized_copy(__first, __last, __result);
660#endif
661 }
662#endif
663
664#if __cplusplus >= 201103L
665 template<typename _ITp, typename _IRef, typename _IPtr, typename _OTp,
666 typename _Tp>
667 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
668 __uninitialized_copy_a(
669 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr> __first,
670 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr> __last,
671 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*> __result,
673
674 template<typename _Iter, typename _OTp, typename _Tp>
675 __enable_if_t<__is_random_access_iter<_Iter>::value,
676 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>>
677 __uninitialized_copy_a(_Iter __first, _Iter __last,
678 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*> __result,
680
681 template<typename _ITp, typename _IRef, typename _IPtr, typename _OTp,
682 typename _Tp>
683 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
684 __uninitialized_move_a(
685 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr> __first,
686 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr> __last,
687 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*> __result,
689#endif
690
691 template<typename _InputIterator, typename _ForwardIterator,
692 typename _Allocator>
693 _GLIBCXX20_CONSTEXPR
694 inline _ForwardIterator
695 __uninitialized_move_a(_InputIterator __first, _InputIterator __last,
696 _ForwardIterator __result, _Allocator& __alloc)
697 {
698 return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
699 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
700 __result, __alloc);
701 }
702
703 template<typename _InputIterator, typename _ForwardIterator,
704 typename _Allocator>
705 _GLIBCXX20_CONSTEXPR
706 inline _ForwardIterator
707 __uninitialized_move_if_noexcept_a(_InputIterator __first,
708 _InputIterator __last,
709 _ForwardIterator __result,
710 _Allocator& __alloc)
711 {
712 return std::__uninitialized_copy_a
713 (_GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__first),
714 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__last), __result, __alloc);
715 }
716
717 template<typename _ForwardIterator, typename _Tp, typename _Allocator>
718 _GLIBCXX20_CONSTEXPR
719 void
720 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
721 const _Tp& __x, _Allocator& __alloc)
722 {
723 _UninitDestroyGuard<_ForwardIterator, _Allocator>
724 __guard(__first, __alloc);
725
726 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
727 for (; __first != __last; ++__first)
728 __traits::construct(__alloc, std::__addressof(*__first), __x);
729
730 __guard.release();
731 }
732
733#if _GLIBCXX_HOSTED
734 template<typename _ForwardIterator, typename _Tp, typename _Tp2>
735 _GLIBCXX20_CONSTEXPR
736 inline void
737 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
738 const _Tp& __x, allocator<_Tp2>&)
739 {
740#ifdef __cpp_lib_is_constant_evaluated
741 if (std::is_constant_evaluated())
742 return std::__do_uninit_fill(__first, __last, __x);
743#endif
744 std::uninitialized_fill(__first, __last, __x);
745 }
746#endif
747
748 template<typename _ForwardIterator, typename _Size, typename _Tp,
749 typename _Allocator>
750 _GLIBCXX20_CONSTEXPR
751 _ForwardIterator
752 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
753 const _Tp& __x, _Allocator& __alloc)
754 {
755 _UninitDestroyGuard<_ForwardIterator, _Allocator>
756 __guard(__first, __alloc);
757 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
758 for (; __n > 0; --__n, (void) ++__first)
759 __traits::construct(__alloc, std::__addressof(*__first), __x);
760 __guard.release();
761 return __first;
762 }
763
764#if _GLIBCXX_HOSTED
765 template<typename _ForwardIterator, typename _Size, typename _Tp,
766 typename _Tp2>
767 _GLIBCXX20_CONSTEXPR
768 inline _ForwardIterator
769 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
770 const _Tp& __x, allocator<_Tp2>&)
771 {
772#ifdef __cpp_lib_is_constant_evaluated
773 if (std::is_constant_evaluated())
774 return std::__do_uninit_fill_n(__first, __n, __x);
775#endif
776 return std::uninitialized_fill_n(__first, __n, __x);
777 }
778#endif
779
780 // Extensions: __uninitialized_copy_move, __uninitialized_move_copy,
781 // __uninitialized_fill_move, __uninitialized_move_fill.
782 // All of these algorithms take a user-supplied allocator, which is used
783 // for construction and destruction.
784
785 // __uninitialized_copy_move
786 // Copies [first1, last1) into [result, result + (last1 - first1)), and
787 // move [first2, last2) into
788 // [result, result + (last1 - first1) + (last2 - first2)).
789 template<typename _InputIterator1, typename _InputIterator2,
790 typename _ForwardIterator, typename _Allocator>
791 inline _ForwardIterator
792 __uninitialized_copy_move(_InputIterator1 __first1,
793 _InputIterator1 __last1,
794 _InputIterator2 __first2,
795 _InputIterator2 __last2,
796 _ForwardIterator __result,
797 _Allocator& __alloc)
798 {
799 _ForwardIterator __mid = std::__uninitialized_copy_a(__first1, __last1,
800 __result, __alloc);
801 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__result,
802 __alloc);
803 __result = __mid; // Everything up to __mid is now guarded.
804 __result = std::__uninitialized_move_a(__first2, __last2, __mid, __alloc);
805 __guard.release();
806 return __result;
807 }
808
809 // __uninitialized_move_copy
810 // Moves [first1, last1) into [result, result + (last1 - first1)), and
811 // copies [first2, last2) into
812 // [result, result + (last1 - first1) + (last2 - first2)).
813 template<typename _InputIterator1, typename _InputIterator2,
814 typename _ForwardIterator, typename _Allocator>
815 inline _ForwardIterator
816 __uninitialized_move_copy(_InputIterator1 __first1,
817 _InputIterator1 __last1,
818 _InputIterator2 __first2,
819 _InputIterator2 __last2,
820 _ForwardIterator __result,
821 _Allocator& __alloc)
822 {
823 _ForwardIterator __mid = std::__uninitialized_move_a(__first1, __last1,
824 __result, __alloc);
825 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__result,
826 __alloc);
827 __result = __mid; // Everything up to __mid is now guarded.
828 __result = std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc);
829 __guard.release();
830 return __result;
831 }
832
833 // __uninitialized_fill_move
834 // Fills [result, mid) with x, and moves [first, last) into
835 // [mid, mid + (last - first)).
836 template<typename _ForwardIterator, typename _Tp, typename _InputIterator,
837 typename _Allocator>
838 inline _ForwardIterator
839 __uninitialized_fill_move(_ForwardIterator __result, _ForwardIterator __mid,
840 const _Tp& __x, _InputIterator __first,
841 _InputIterator __last, _Allocator& __alloc)
842 {
843 std::__uninitialized_fill_a(__result, __mid, __x, __alloc);
844 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__result,
845 __alloc);
846 __result = __mid; // Everything up to __mid is now guarded.
847 __result = std::__uninitialized_move_a(__first, __last, __mid, __alloc);
848 __guard.release();
849 return __result;
850 }
851
852 // __uninitialized_move_fill
853 // Moves [first1, last1) into [first2, first2 + (last1 - first1)), and
854 // fills [first2 + (last1 - first1), last2) with x.
855 template<typename _InputIterator, typename _ForwardIterator, typename _Tp,
856 typename _Allocator>
857 inline void
858 __uninitialized_move_fill(_InputIterator __first1, _InputIterator __last1,
859 _ForwardIterator __first2,
860 _ForwardIterator __last2, const _Tp& __x,
861 _Allocator& __alloc)
862 {
863 _ForwardIterator __mid2 = std::__uninitialized_move_a(__first1, __last1,
864 __first2,
865 __alloc);
866 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__first2,
867 __alloc);
868 __first2 = __mid2; // Everything up to __mid2 is now guarded.
869 std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc);
870 __guard.release();
871 }
872
873 /// @endcond
874
875#if __cplusplus >= 201103L
876 /// @cond undocumented
877
878 // Extensions: __uninitialized_default, __uninitialized_default_n,
879 // __uninitialized_default_a, __uninitialized_default_n_a.
880
881 template<bool _TrivialValueType>
882 struct __uninitialized_default_1
883 {
884 template<typename _ForwardIterator>
885 _GLIBCXX26_CONSTEXPR
886 static void
887 __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
888 {
889 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
890 for (; __first != __last; ++__first)
892 __guard.release();
893 }
894 };
895
896 template<>
897 struct __uninitialized_default_1<true>
898 {
899 template<typename _ForwardIterator>
900 _GLIBCXX26_CONSTEXPR
901 static void
902 __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
903 {
904 if (__first == __last)
905 return;
906
907 typename iterator_traits<_ForwardIterator>::value_type* __val
908 = std::addressof(*__first);
909 std::_Construct(__val);
910 if (++__first != __last)
911 std::fill(__first, __last, *__val);
912 }
913 };
914
915 template<bool _TrivialValueType>
916 struct __uninitialized_default_n_1
917 {
918 template<typename _ForwardIterator, typename _Size>
919 _GLIBCXX20_CONSTEXPR
920 static _ForwardIterator
921 __uninit_default_n(_ForwardIterator __first, _Size __n)
922 {
923 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
924 for (; __n > 0; --__n, (void) ++__first)
926 __guard.release();
927 return __first;
928 }
929 };
930
931 template<>
932 struct __uninitialized_default_n_1<true>
933 {
934 template<typename _ForwardIterator, typename _Size>
935 _GLIBCXX20_CONSTEXPR
936 static _ForwardIterator
937 __uninit_default_n(_ForwardIterator __first, _Size __n)
938 {
939 if (__n > 0)
940 {
941 typename iterator_traits<_ForwardIterator>::value_type* __val
942 = std::addressof(*__first);
943 std::_Construct(__val);
944 ++__first;
945 __first = std::fill_n(__first, __n - 1, *__val);
946 }
947 return __first;
948 }
949 };
950
951 // __uninitialized_default
952 // Fills [first, last) with value-initialized value_types.
953 template<typename _ForwardIterator>
954 _GLIBCXX20_CONSTEXPR
955 inline void
956 __uninitialized_default(_ForwardIterator __first,
957 _ForwardIterator __last)
958 {
959#ifdef __cpp_lib_is_constant_evaluated
960 if (std::is_constant_evaluated())
961 return __uninitialized_default_1<false>::
962 __uninit_default(__first, __last);
963#endif
964
966 _ValueType;
967 // trivial types can have deleted assignment
968 const bool __assignable = is_copy_assignable<_ValueType>::value;
969
970 std::__uninitialized_default_1<__is_trivial(_ValueType)
971 && __assignable>::
972 __uninit_default(__first, __last);
973 }
974
975 // __uninitialized_default_n
976 // Fills [first, first + n) with value-initialized value_types.
977 template<typename _ForwardIterator, typename _Size>
978 _GLIBCXX20_CONSTEXPR
979 inline _ForwardIterator
980 __uninitialized_default_n(_ForwardIterator __first, _Size __n)
981 {
982#ifdef __cpp_lib_is_constant_evaluated
983 if (std::is_constant_evaluated())
984 return __uninitialized_default_n_1<false>::
985 __uninit_default_n(__first, __n);
986#endif
987
989 _ValueType;
990 // See uninitialized_fill_n for the conditions for using std::fill_n.
991 constexpr bool __can_fill
992 = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
993
994 return __uninitialized_default_n_1<__is_trivial(_ValueType)
995 && __can_fill>::
996 __uninit_default_n(__first, __n);
997 }
998
999
1000 // __uninitialized_default_a
1001 // Fills [first, last) with value_types constructed by the allocator
1002 // alloc, with no arguments passed to the construct call.
1003 template<typename _ForwardIterator, typename _Allocator>
1004 void
1005 __uninitialized_default_a(_ForwardIterator __first,
1006 _ForwardIterator __last,
1007 _Allocator& __alloc)
1008 {
1009 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__first,
1010 __alloc);
1011 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
1012 for (; __first != __last; ++__first)
1013 __traits::construct(__alloc, std::addressof(*__first));
1014 __guard.release();
1015 }
1016
1017#if _GLIBCXX_HOSTED
1018 template<typename _ForwardIterator, typename _Tp>
1019 inline void
1020 __uninitialized_default_a(_ForwardIterator __first,
1021 _ForwardIterator __last,
1023 { std::__uninitialized_default(__first, __last); }
1024#endif
1025
1026 // __uninitialized_default_n_a
1027 // Fills [first, first + n) with value_types constructed by the allocator
1028 // alloc, with no arguments passed to the construct call.
1029 template<typename _ForwardIterator, typename _Size, typename _Allocator>
1030 _GLIBCXX20_CONSTEXPR _ForwardIterator
1031 __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
1032 _Allocator& __alloc)
1033 {
1034 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__first,
1035 __alloc);
1036 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
1037 for (; __n > 0; --__n, (void) ++__first)
1038 __traits::construct(__alloc, std::addressof(*__first));
1039 __guard.release();
1040 return __first;
1041 }
1042
1043#if _GLIBCXX_HOSTED
1044 // __uninitialized_default_n_a specialization for std::allocator,
1045 // which ignores the allocator and value-initializes the elements.
1046 template<typename _ForwardIterator, typename _Size, typename _Tp>
1047 _GLIBCXX20_CONSTEXPR
1048 inline _ForwardIterator
1049 __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
1051 { return std::__uninitialized_default_n(__first, __n); }
1052#endif
1053
1054 template<bool _TrivialValueType>
1055 struct __uninitialized_default_novalue_1
1056 {
1057 template<typename _ForwardIterator>
1058 _GLIBCXX26_CONSTEXPR
1059 static void
1060 __uninit_default_novalue(_ForwardIterator __first,
1061 _ForwardIterator __last)
1062 {
1063 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
1064 for (; __first != __last; ++__first)
1065 std::_Construct_novalue(std::addressof(*__first));
1066 __guard.release();
1067 }
1068 };
1069
1070 template<>
1071 struct __uninitialized_default_novalue_1<true>
1072 {
1073 template<typename _ForwardIterator>
1074 _GLIBCXX26_CONSTEXPR
1075 static void
1076 __uninit_default_novalue(_ForwardIterator, _ForwardIterator)
1077 {
1078 }
1079 };
1080
1081 template<bool _TrivialValueType>
1082 struct __uninitialized_default_novalue_n_1
1083 {
1084 template<typename _ForwardIterator, typename _Size>
1085 _GLIBCXX26_CONSTEXPR
1086 static _ForwardIterator
1087 __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
1088 {
1089 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
1090 for (; __n > 0; --__n, (void) ++__first)
1091 std::_Construct_novalue(std::addressof(*__first));
1092 __guard.release();
1093 return __first;
1094 }
1095 };
1096
1097 template<>
1098 struct __uninitialized_default_novalue_n_1<true>
1099 {
1100 template<typename _ForwardIterator, typename _Size>
1101 _GLIBCXX26_CONSTEXPR
1102 static _ForwardIterator
1103 __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
1104 { return std::next(__first, __n); }
1105 };
1106
1107 // __uninitialized_default_novalue
1108 // Fills [first, last) with default-initialized value_types.
1109 template<typename _ForwardIterator>
1110 _GLIBCXX26_CONSTEXPR
1111 inline void
1112 __uninitialized_default_novalue(_ForwardIterator __first,
1113 _ForwardIterator __last)
1114 {
1116 _ValueType;
1117
1118 std::__uninitialized_default_novalue_1<
1120 __uninit_default_novalue(__first, __last);
1121 }
1122
1123 // __uninitialized_default_novalue_n
1124 // Fills [first, first + n) with default-initialized value_types.
1125 template<typename _ForwardIterator, typename _Size>
1126 _GLIBCXX26_CONSTEXPR
1127 inline _ForwardIterator
1128 __uninitialized_default_novalue_n(_ForwardIterator __first, _Size __n)
1129 {
1131 _ValueType;
1132
1133 return __uninitialized_default_novalue_n_1<
1135 __uninit_default_novalue_n(__first, __n);
1136 }
1137
1138 template<typename _InputIterator, typename _Size,
1139 typename _ForwardIterator>
1140 _GLIBCXX26_CONSTEXPR
1141 _ForwardIterator
1142 __uninitialized_copy_n(_InputIterator __first, _Size __n,
1143 _ForwardIterator __result, input_iterator_tag)
1144 {
1145 _UninitDestroyGuard<_ForwardIterator> __guard(__result);
1146 for (; __n > 0; --__n, (void) ++__first, ++__result)
1147 std::_Construct(std::addressof(*__result), *__first);
1148 __guard.release();
1149 return __result;
1150 }
1151
1152 template<typename _RandomAccessIterator, typename _Size,
1153 typename _ForwardIterator>
1154 _GLIBCXX26_CONSTEXPR
1155 inline _ForwardIterator
1156 __uninitialized_copy_n(_RandomAccessIterator __first, _Size __n,
1157 _ForwardIterator __result,
1159 { return std::uninitialized_copy(__first, __first + __n, __result); }
1160
1161 template<typename _InputIterator, typename _Size,
1162 typename _ForwardIterator>
1163 _GLIBCXX26_CONSTEXPR
1165 __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
1166 _ForwardIterator __result, input_iterator_tag)
1167 {
1168 _UninitDestroyGuard<_ForwardIterator> __guard(__result);
1169 for (; __n > 0; --__n, (void) ++__first, ++__result)
1170 std::_Construct(std::addressof(*__result), *__first);
1171 __guard.release();
1172 return {__first, __result};
1173 }
1174
1175 template<typename _RandomAccessIterator, typename _Size,
1176 typename _ForwardIterator>
1177 _GLIBCXX26_CONSTEXPR
1179 __uninitialized_copy_n_pair(_RandomAccessIterator __first, _Size __n,
1180 _ForwardIterator __result,
1182 {
1183 auto __second_res = uninitialized_copy(__first, __first + __n, __result);
1184 auto __first_res = std::next(__first, __n);
1185 return {__first_res, __second_res};
1186 }
1187
1188 /// @endcond
1189
1190 /**
1191 * @brief Copies the range [first,first+n) into result.
1192 * @param __first An input iterator.
1193 * @param __n The number of elements to copy.
1194 * @param __result An output iterator.
1195 * @return __result + __n
1196 * @since C++11
1197 *
1198 * Like copy_n(), but does not require an initialized output range.
1199 */
1200 template<typename _InputIterator, typename _Size, typename _ForwardIterator>
1201 _GLIBCXX26_CONSTEXPR
1202 inline _ForwardIterator
1203 uninitialized_copy_n(_InputIterator __first, _Size __n,
1204 _ForwardIterator __result)
1205 { return std::__uninitialized_copy_n(__first, __n, __result,
1206 std::__iter_concept_or_category(__first)); }
1207
1208 /// @cond undocumented
1209 template<typename _InputIterator, typename _Size, typename _ForwardIterator>
1210 _GLIBCXX26_CONSTEXPR
1212 __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
1213 _ForwardIterator __result)
1214 {
1215 return
1216 std::__uninitialized_copy_n_pair(__first, __n, __result,
1217 std::__iter_concept_or_category(__first));
1218 }
1219 /// @endcond
1220#endif
1221
1222#ifdef __glibcxx_raw_memory_algorithms // C++ >= 17
1223 /**
1224 * @brief Default-initializes objects in the range [first,last).
1225 * @param __first A forward iterator.
1226 * @param __last A forward iterator.
1227 * @since C++17
1228 */
1229 template <typename _ForwardIterator>
1230 _GLIBCXX26_CONSTEXPR
1231 inline void
1232 uninitialized_default_construct(_ForwardIterator __first,
1233 _ForwardIterator __last)
1234 {
1235 std::__uninitialized_default_novalue(__first, __last);
1236 }
1237
1238 /**
1239 * @brief Default-initializes objects in the range [first,first+count).
1240 * @param __first A forward iterator.
1241 * @param __count The number of objects to construct.
1242 * @return __first + __count
1243 * @since C++17
1244 */
1245 template <typename _ForwardIterator, typename _Size>
1246 _GLIBCXX26_CONSTEXPR
1247 inline _ForwardIterator
1248 uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
1249 {
1250 return std::__uninitialized_default_novalue_n(__first, __count);
1251 }
1252
1253 /**
1254 * @brief Value-initializes objects in the range [first,last).
1255 * @param __first A forward iterator.
1256 * @param __last A forward iterator.
1257 * @since C++17
1258 */
1259 template <typename _ForwardIterator>
1260 _GLIBCXX26_CONSTEXPR
1261 inline void
1262 uninitialized_value_construct(_ForwardIterator __first,
1263 _ForwardIterator __last)
1264 {
1265 return std::__uninitialized_default(__first, __last);
1266 }
1267
1268 /**
1269 * @brief Value-initializes objects in the range [first,first+count).
1270 * @param __first A forward iterator.
1271 * @param __count The number of objects to construct.
1272 * @return __result + __count
1273 * @since C++17
1274 */
1275 template <typename _ForwardIterator, typename _Size>
1276 _GLIBCXX26_CONSTEXPR
1277 inline _ForwardIterator
1278 uninitialized_value_construct_n(_ForwardIterator __first, _Size __count)
1279 {
1280 return std::__uninitialized_default_n(__first, __count);
1281 }
1282
1283 /**
1284 * @brief Move-construct from the range [first,last) into result.
1285 * @param __first An input iterator.
1286 * @param __last An input iterator.
1287 * @param __result An output iterator.
1288 * @return __result + (__first - __last)
1289 * @since C++17
1290 */
1291 template <typename _InputIterator, typename _ForwardIterator>
1292 _GLIBCXX26_CONSTEXPR
1293 inline _ForwardIterator
1294 uninitialized_move(_InputIterator __first, _InputIterator __last,
1295 _ForwardIterator __result)
1296 {
1297 return std::uninitialized_copy(std::make_move_iterator(__first),
1298 std::make_move_iterator(__last),
1299 __result);
1300 }
1301
1302 /**
1303 * @brief Move-construct from the range [first,first+count) into result.
1304 * @param __first An input iterator.
1305 * @param __count The number of objects to initialize.
1306 * @param __result An output iterator.
1307 * @return __result + __count
1308 * @since C++17
1309 */
1310 template <typename _InputIterator, typename _Size, typename _ForwardIterator>
1311 _GLIBCXX26_CONSTEXPR
1313 uninitialized_move_n(_InputIterator __first, _Size __count,
1314 _ForwardIterator __result)
1315 {
1316 auto __res
1317 = std::__uninitialized_copy_n_pair(std::make_move_iterator(__first),
1318 __count, __result);
1319 return {__res.first.base(), __res.second};
1320 }
1321#endif // __glibcxx_raw_memory_algorithms
1322
1323#if __cplusplus >= 201103L
1324 /// @cond undocumented
1325
1326 template<typename _Tp, typename _Up, typename _Allocator>
1327 _GLIBCXX20_CONSTEXPR
1328 inline void
1329 __relocate_object_a(_Tp* __restrict __dest, _Up* __restrict __orig,
1330 _Allocator& __alloc)
1331 noexcept(noexcept(std::allocator_traits<_Allocator>::construct(__alloc,
1332 __dest, std::move(*__orig)))
1334 __alloc, std::addressof(*__orig))))
1335 {
1336 typedef std::allocator_traits<_Allocator> __traits;
1337 __traits::construct(__alloc, __dest, std::move(*__orig));
1338 __traits::destroy(__alloc, std::addressof(*__orig));
1339 }
1340
1341 // This class may be specialized for specific types.
1342 // Also known as is_trivially_relocatable.
1343 template<typename _Tp, typename = void>
1344 struct __is_bitwise_relocatable
1345 : __bool_constant<__is_trivial(_Tp)>
1346 { };
1347
1348 template <typename _InputIterator, typename _ForwardIterator,
1349 typename _Allocator>
1350 _GLIBCXX20_CONSTEXPR
1351 inline _ForwardIterator
1352 __relocate_a_1(_InputIterator __first, _InputIterator __last,
1353 _ForwardIterator __result, _Allocator& __alloc)
1354 noexcept(noexcept(std::__relocate_object_a(std::addressof(*__result),
1355 std::addressof(*__first),
1356 __alloc)))
1357 {
1359 _ValueType;
1361 _ValueType2;
1362 static_assert(std::is_same<_ValueType, _ValueType2>::value,
1363 "relocation is only possible for values of the same type");
1364 _ForwardIterator __cur = __result;
1365 for (; __first != __last; ++__first, (void)++__cur)
1366 std::__relocate_object_a(std::addressof(*__cur),
1367 std::addressof(*__first), __alloc);
1368 return __cur;
1369 }
1370
1371#if _GLIBCXX_HOSTED
1372 template <typename _Tp, typename _Up>
1373 _GLIBCXX20_CONSTEXPR
1374 inline __enable_if_t<std::__is_bitwise_relocatable<_Tp>::value, _Tp*>
1375 __relocate_a_1(_Tp* __first, _Tp* __last,
1376 _Tp* __result,
1377 [[__maybe_unused__]] allocator<_Up>& __alloc) noexcept
1378 {
1379 ptrdiff_t __count = __last - __first;
1380 if (__count > 0)
1381 {
1382#ifdef __cpp_lib_is_constant_evaluated
1383 if (std::is_constant_evaluated())
1384 {
1385 // Can't use memcpy. Wrap the pointer so that __relocate_a_1
1386 // resolves to the non-trivial overload above.
1387 __gnu_cxx::__normal_iterator<_Tp*, void> __out(__result);
1388 __out = std::__relocate_a_1(__first, __last, __out, __alloc);
1389 return __out.base();
1390 }
1391#endif
1392 __builtin_memcpy(__result, __first, __count * sizeof(_Tp));
1393 }
1394 return __result + __count;
1395 }
1396#endif
1397
1398 template <typename _InputIterator, typename _ForwardIterator,
1399 typename _Allocator>
1400 _GLIBCXX20_CONSTEXPR
1401 inline _ForwardIterator
1402 __relocate_a(_InputIterator __first, _InputIterator __last,
1403 _ForwardIterator __result, _Allocator& __alloc)
1404 noexcept(noexcept(__relocate_a_1(std::__niter_base(__first),
1405 std::__niter_base(__last),
1406 std::__niter_base(__result), __alloc)))
1407 {
1408 return std::__relocate_a_1(std::__niter_base(__first),
1409 std::__niter_base(__last),
1410 std::__niter_base(__result), __alloc);
1411 }
1412
1413 /// @endcond
1414#endif // C++11
1415
1416 /// @} group memory
1417
1418_GLIBCXX_END_NAMESPACE_VERSION
1419} // namespace
1420
1421#endif /* _STL_UNINITIALIZED_H */
constexpr _ForwardIterator uninitialized_value_construct_n(_ForwardIterator __first, _Size __count)
Value-initializes objects in the range [first,first+count).
constexpr _ForwardIterator uninitialized_copy_n(_InputIterator __first, _Size __n, _ForwardIterator __result)
Copies the range [first,first+n) into result.
constexpr void uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp &__x)
Copies the value x into the range [first,last).
constexpr void uninitialized_default_construct(_ForwardIterator __first, _ForwardIterator __last)
Default-initializes objects in the range [first,last).
constexpr void uninitialized_value_construct(_ForwardIterator __first, _ForwardIterator __last)
Value-initializes objects in the range [first,last).
constexpr _ForwardIterator uninitialized_move(_InputIterator __first, _InputIterator __last, _ForwardIterator __result)
Move-construct from the range [first,last) into result.
constexpr pair< _InputIterator, _ForwardIterator > uninitialized_move_n(_InputIterator __first, _Size __count, _ForwardIterator __result)
Move-construct from the range [first,first+count) into result.
constexpr _ForwardIterator uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp &__x)
Copies the value x into the range [first,first+n).
constexpr _ForwardIterator uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
Default-initializes objects in the range [first,first+count).
constexpr _ForwardIterator uninitialized_copy(_InputIterator __first, _InputIterator __last, _ForwardIterator __result)
Copies the range [first,last) 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:2353
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:542
is_pointer
Definition type_traits:629
is_copy_assignable
Definition type_traits:1368
is_trivially_default_constructible
Definition type_traits:1436
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 (or references) of arbitrary type.
Definition stl_pair.h:307
Marking input iterators.
Random-access iterators support a superset of bidirectional iterator operations.
Uniform interface to C++98 and C++11 allocators.