libstdc++
stl_uninitialized.h
Go to the documentation of this file.
1// Raw memory manipulators -*- C++ -*-
2
3// Copyright (C) 2001-2025 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 template<typename _InputIterator, typename _ForwardIterator,
666 typename _Allocator>
667 _GLIBCXX20_CONSTEXPR
668 inline _ForwardIterator
669 __uninitialized_move_a(_InputIterator __first, _InputIterator __last,
670 _ForwardIterator __result, _Allocator& __alloc)
671 {
672 return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
673 _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
674 __result, __alloc);
675 }
676
677 template<typename _InputIterator, typename _ForwardIterator,
678 typename _Allocator>
679 _GLIBCXX20_CONSTEXPR
680 inline _ForwardIterator
681 __uninitialized_move_if_noexcept_a(_InputIterator __first,
682 _InputIterator __last,
683 _ForwardIterator __result,
684 _Allocator& __alloc)
685 {
686 return std::__uninitialized_copy_a
687 (_GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__first),
688 _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__last), __result, __alloc);
689 }
690
691 template<typename _ForwardIterator, typename _Tp, typename _Allocator>
692 _GLIBCXX20_CONSTEXPR
693 void
694 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
695 const _Tp& __x, _Allocator& __alloc)
696 {
697 _UninitDestroyGuard<_ForwardIterator, _Allocator>
698 __guard(__first, __alloc);
699
700 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
701 for (; __first != __last; ++__first)
702 __traits::construct(__alloc, std::__addressof(*__first), __x);
703
704 __guard.release();
705 }
706
707#if _GLIBCXX_HOSTED
708 template<typename _ForwardIterator, typename _Tp, typename _Tp2>
709 _GLIBCXX20_CONSTEXPR
710 inline void
711 __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
712 const _Tp& __x, allocator<_Tp2>&)
713 {
714#ifdef __cpp_lib_is_constant_evaluated
715 if (std::is_constant_evaluated())
716 return std::__do_uninit_fill(__first, __last, __x);
717#endif
718 std::uninitialized_fill(__first, __last, __x);
719 }
720#endif
721
722 template<typename _ForwardIterator, typename _Size, typename _Tp,
723 typename _Allocator>
724 _GLIBCXX20_CONSTEXPR
725 _ForwardIterator
726 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
727 const _Tp& __x, _Allocator& __alloc)
728 {
729 _UninitDestroyGuard<_ForwardIterator, _Allocator>
730 __guard(__first, __alloc);
731 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
732 for (; __n > 0; --__n, (void) ++__first)
733 __traits::construct(__alloc, std::__addressof(*__first), __x);
734 __guard.release();
735 return __first;
736 }
737
738#if _GLIBCXX_HOSTED
739 template<typename _ForwardIterator, typename _Size, typename _Tp,
740 typename _Tp2>
741 _GLIBCXX20_CONSTEXPR
742 inline _ForwardIterator
743 __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
744 const _Tp& __x, allocator<_Tp2>&)
745 {
746#ifdef __cpp_lib_is_constant_evaluated
747 if (std::is_constant_evaluated())
748 return std::__do_uninit_fill_n(__first, __n, __x);
749#endif
750 return std::uninitialized_fill_n(__first, __n, __x);
751 }
752#endif
753
754 // Extensions: __uninitialized_copy_move, __uninitialized_move_copy,
755 // __uninitialized_fill_move, __uninitialized_move_fill.
756 // All of these algorithms take a user-supplied allocator, which is used
757 // for construction and destruction.
758
759 // __uninitialized_copy_move
760 // Copies [first1, last1) into [result, result + (last1 - first1)), and
761 // move [first2, last2) into
762 // [result, result + (last1 - first1) + (last2 - first2)).
763 template<typename _InputIterator1, typename _InputIterator2,
764 typename _ForwardIterator, typename _Allocator>
765 inline _ForwardIterator
766 __uninitialized_copy_move(_InputIterator1 __first1,
767 _InputIterator1 __last1,
768 _InputIterator2 __first2,
769 _InputIterator2 __last2,
770 _ForwardIterator __result,
771 _Allocator& __alloc)
772 {
773 _ForwardIterator __mid = std::__uninitialized_copy_a(__first1, __last1,
774 __result, __alloc);
775 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__result,
776 __alloc);
777 __result = __mid; // Everything up to __mid is now guarded.
778 __result = std::__uninitialized_move_a(__first2, __last2, __mid, __alloc);
779 __guard.release();
780 return __result;
781 }
782
783 // __uninitialized_move_copy
784 // Moves [first1, last1) into [result, result + (last1 - first1)), and
785 // copies [first2, last2) into
786 // [result, result + (last1 - first1) + (last2 - first2)).
787 template<typename _InputIterator1, typename _InputIterator2,
788 typename _ForwardIterator, typename _Allocator>
789 inline _ForwardIterator
790 __uninitialized_move_copy(_InputIterator1 __first1,
791 _InputIterator1 __last1,
792 _InputIterator2 __first2,
793 _InputIterator2 __last2,
794 _ForwardIterator __result,
795 _Allocator& __alloc)
796 {
797 _ForwardIterator __mid = std::__uninitialized_move_a(__first1, __last1,
798 __result, __alloc);
799 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__result,
800 __alloc);
801 __result = __mid; // Everything up to __mid is now guarded.
802 __result = std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc);
803 __guard.release();
804 return __result;
805 }
806
807 // __uninitialized_fill_move
808 // Fills [result, mid) with x, and moves [first, last) into
809 // [mid, mid + (last - first)).
810 template<typename _ForwardIterator, typename _Tp, typename _InputIterator,
811 typename _Allocator>
812 inline _ForwardIterator
813 __uninitialized_fill_move(_ForwardIterator __result, _ForwardIterator __mid,
814 const _Tp& __x, _InputIterator __first,
815 _InputIterator __last, _Allocator& __alloc)
816 {
817 std::__uninitialized_fill_a(__result, __mid, __x, __alloc);
818 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__result,
819 __alloc);
820 __result = __mid; // Everything up to __mid is now guarded.
821 __result = std::__uninitialized_move_a(__first, __last, __mid, __alloc);
822 __guard.release();
823 return __result;
824 }
825
826 // __uninitialized_move_fill
827 // Moves [first1, last1) into [first2, first2 + (last1 - first1)), and
828 // fills [first2 + (last1 - first1), last2) with x.
829 template<typename _InputIterator, typename _ForwardIterator, typename _Tp,
830 typename _Allocator>
831 inline void
832 __uninitialized_move_fill(_InputIterator __first1, _InputIterator __last1,
833 _ForwardIterator __first2,
834 _ForwardIterator __last2, const _Tp& __x,
835 _Allocator& __alloc)
836 {
837 _ForwardIterator __mid2 = std::__uninitialized_move_a(__first1, __last1,
838 __first2,
839 __alloc);
840 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__first2,
841 __alloc);
842 __first2 = __mid2; // Everything up to __mid2 is now guarded.
843 std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc);
844 __guard.release();
845 }
846
847 /// @endcond
848
849#if __cplusplus >= 201103L
850 /// @cond undocumented
851
852 // Extensions: __uninitialized_default, __uninitialized_default_n,
853 // __uninitialized_default_a, __uninitialized_default_n_a.
854
855 template<bool _TrivialValueType>
856 struct __uninitialized_default_1
857 {
858 template<typename _ForwardIterator>
859 _GLIBCXX26_CONSTEXPR
860 static void
861 __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
862 {
863 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
864 for (; __first != __last; ++__first)
866 __guard.release();
867 }
868 };
869
870 template<>
871 struct __uninitialized_default_1<true>
872 {
873 template<typename _ForwardIterator>
874 _GLIBCXX26_CONSTEXPR
875 static void
876 __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
877 {
878 if (__first == __last)
879 return;
880
881 typename iterator_traits<_ForwardIterator>::value_type* __val
882 = std::addressof(*__first);
883 std::_Construct(__val);
884 if (++__first != __last)
885 std::fill(__first, __last, *__val);
886 }
887 };
888
889 template<bool _TrivialValueType>
890 struct __uninitialized_default_n_1
891 {
892 template<typename _ForwardIterator, typename _Size>
893 _GLIBCXX20_CONSTEXPR
894 static _ForwardIterator
895 __uninit_default_n(_ForwardIterator __first, _Size __n)
896 {
897 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
898 for (; __n > 0; --__n, (void) ++__first)
900 __guard.release();
901 return __first;
902 }
903 };
904
905 template<>
906 struct __uninitialized_default_n_1<true>
907 {
908 template<typename _ForwardIterator, typename _Size>
909 _GLIBCXX20_CONSTEXPR
910 static _ForwardIterator
911 __uninit_default_n(_ForwardIterator __first, _Size __n)
912 {
913 if (__n > 0)
914 {
915 typename iterator_traits<_ForwardIterator>::value_type* __val
916 = std::addressof(*__first);
917 std::_Construct(__val);
918 ++__first;
919 __first = std::fill_n(__first, __n - 1, *__val);
920 }
921 return __first;
922 }
923 };
924
925 // __uninitialized_default
926 // Fills [first, last) with value-initialized value_types.
927 template<typename _ForwardIterator>
928 _GLIBCXX20_CONSTEXPR
929 inline void
930 __uninitialized_default(_ForwardIterator __first,
931 _ForwardIterator __last)
932 {
933#ifdef __cpp_lib_is_constant_evaluated
934 if (std::is_constant_evaluated())
935 return __uninitialized_default_1<false>::
936 __uninit_default(__first, __last);
937#endif
938
940 _ValueType;
941 // trivial types can have deleted assignment
942 const bool __assignable = is_copy_assignable<_ValueType>::value;
943
944 std::__uninitialized_default_1<__is_trivial(_ValueType)
945 && __assignable>::
946 __uninit_default(__first, __last);
947 }
948
949 // __uninitialized_default_n
950 // Fills [first, first + n) with value-initialized value_types.
951 template<typename _ForwardIterator, typename _Size>
952 _GLIBCXX20_CONSTEXPR
953 inline _ForwardIterator
954 __uninitialized_default_n(_ForwardIterator __first, _Size __n)
955 {
956#ifdef __cpp_lib_is_constant_evaluated
957 if (std::is_constant_evaluated())
958 return __uninitialized_default_n_1<false>::
959 __uninit_default_n(__first, __n);
960#endif
961
963 _ValueType;
964 // See uninitialized_fill_n for the conditions for using std::fill_n.
965 constexpr bool __can_fill
966 = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
967
968 return __uninitialized_default_n_1<__is_trivial(_ValueType)
969 && __can_fill>::
970 __uninit_default_n(__first, __n);
971 }
972
973
974 // __uninitialized_default_a
975 // Fills [first, last) with value_types constructed by the allocator
976 // alloc, with no arguments passed to the construct call.
977 template<typename _ForwardIterator, typename _Allocator>
978 void
979 __uninitialized_default_a(_ForwardIterator __first,
980 _ForwardIterator __last,
981 _Allocator& __alloc)
982 {
983 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__first,
984 __alloc);
985 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
986 for (; __first != __last; ++__first)
987 __traits::construct(__alloc, std::addressof(*__first));
988 __guard.release();
989 }
990
991#if _GLIBCXX_HOSTED
992 template<typename _ForwardIterator, typename _Tp>
993 inline void
994 __uninitialized_default_a(_ForwardIterator __first,
995 _ForwardIterator __last,
997 { std::__uninitialized_default(__first, __last); }
998#endif
999
1000 // __uninitialized_default_n_a
1001 // Fills [first, first + n) with value_types constructed by the allocator
1002 // alloc, with no arguments passed to the construct call.
1003 template<typename _ForwardIterator, typename _Size, typename _Allocator>
1004 _GLIBCXX20_CONSTEXPR _ForwardIterator
1005 __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
1006 _Allocator& __alloc)
1007 {
1008 _UninitDestroyGuard<_ForwardIterator, _Allocator> __guard(__first,
1009 __alloc);
1010 typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
1011 for (; __n > 0; --__n, (void) ++__first)
1012 __traits::construct(__alloc, std::addressof(*__first));
1013 __guard.release();
1014 return __first;
1015 }
1016
1017#if _GLIBCXX_HOSTED
1018 // __uninitialized_default_n_a specialization for std::allocator,
1019 // which ignores the allocator and value-initializes the elements.
1020 template<typename _ForwardIterator, typename _Size, typename _Tp>
1021 _GLIBCXX20_CONSTEXPR
1022 inline _ForwardIterator
1023 __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
1025 { return std::__uninitialized_default_n(__first, __n); }
1026#endif
1027
1028 template<bool _TrivialValueType>
1029 struct __uninitialized_default_novalue_1
1030 {
1031 template<typename _ForwardIterator>
1032 _GLIBCXX26_CONSTEXPR
1033 static void
1034 __uninit_default_novalue(_ForwardIterator __first,
1035 _ForwardIterator __last)
1036 {
1037 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
1038 for (; __first != __last; ++__first)
1039 std::_Construct_novalue(std::addressof(*__first));
1040 __guard.release();
1041 }
1042 };
1043
1044 template<>
1045 struct __uninitialized_default_novalue_1<true>
1046 {
1047 template<typename _ForwardIterator>
1048 _GLIBCXX26_CONSTEXPR
1049 static void
1050 __uninit_default_novalue(_ForwardIterator, _ForwardIterator)
1051 {
1052 }
1053 };
1054
1055 template<bool _TrivialValueType>
1056 struct __uninitialized_default_novalue_n_1
1057 {
1058 template<typename _ForwardIterator, typename _Size>
1059 _GLIBCXX26_CONSTEXPR
1060 static _ForwardIterator
1061 __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
1062 {
1063 _UninitDestroyGuard<_ForwardIterator> __guard(__first);
1064 for (; __n > 0; --__n, (void) ++__first)
1065 std::_Construct_novalue(std::addressof(*__first));
1066 __guard.release();
1067 return __first;
1068 }
1069 };
1070
1071 template<>
1072 struct __uninitialized_default_novalue_n_1<true>
1073 {
1074 template<typename _ForwardIterator, typename _Size>
1075 _GLIBCXX26_CONSTEXPR
1076 static _ForwardIterator
1077 __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
1078 { return std::next(__first, __n); }
1079 };
1080
1081 // __uninitialized_default_novalue
1082 // Fills [first, last) with default-initialized value_types.
1083 template<typename _ForwardIterator>
1084 _GLIBCXX26_CONSTEXPR
1085 inline void
1086 __uninitialized_default_novalue(_ForwardIterator __first,
1087 _ForwardIterator __last)
1088 {
1090 _ValueType;
1091
1092 std::__uninitialized_default_novalue_1<
1094 __uninit_default_novalue(__first, __last);
1095 }
1096
1097 // __uninitialized_default_novalue_n
1098 // Fills [first, first + n) with default-initialized value_types.
1099 template<typename _ForwardIterator, typename _Size>
1100 _GLIBCXX26_CONSTEXPR
1101 inline _ForwardIterator
1102 __uninitialized_default_novalue_n(_ForwardIterator __first, _Size __n)
1103 {
1105 _ValueType;
1106
1107 return __uninitialized_default_novalue_n_1<
1109 __uninit_default_novalue_n(__first, __n);
1110 }
1111
1112 template<typename _InputIterator, typename _Size,
1113 typename _ForwardIterator>
1114 _GLIBCXX26_CONSTEXPR
1115 _ForwardIterator
1116 __uninitialized_copy_n(_InputIterator __first, _Size __n,
1117 _ForwardIterator __result, input_iterator_tag)
1118 {
1119 _UninitDestroyGuard<_ForwardIterator> __guard(__result);
1120 for (; __n > 0; --__n, (void) ++__first, ++__result)
1121 std::_Construct(std::addressof(*__result), *__first);
1122 __guard.release();
1123 return __result;
1124 }
1125
1126 template<typename _RandomAccessIterator, typename _Size,
1127 typename _ForwardIterator>
1128 _GLIBCXX26_CONSTEXPR
1129 inline _ForwardIterator
1130 __uninitialized_copy_n(_RandomAccessIterator __first, _Size __n,
1131 _ForwardIterator __result,
1133 { return std::uninitialized_copy(__first, __first + __n, __result); }
1134
1135 template<typename _InputIterator, typename _Size,
1136 typename _ForwardIterator>
1137 _GLIBCXX26_CONSTEXPR
1139 __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
1140 _ForwardIterator __result, input_iterator_tag)
1141 {
1142 _UninitDestroyGuard<_ForwardIterator> __guard(__result);
1143 for (; __n > 0; --__n, (void) ++__first, ++__result)
1144 std::_Construct(std::addressof(*__result), *__first);
1145 __guard.release();
1146 return {__first, __result};
1147 }
1148
1149 template<typename _RandomAccessIterator, typename _Size,
1150 typename _ForwardIterator>
1151 _GLIBCXX26_CONSTEXPR
1153 __uninitialized_copy_n_pair(_RandomAccessIterator __first, _Size __n,
1154 _ForwardIterator __result,
1156 {
1157 auto __second_res = uninitialized_copy(__first, __first + __n, __result);
1158 auto __first_res = std::next(__first, __n);
1159 return {__first_res, __second_res};
1160 }
1161
1162 /// @endcond
1163
1164 /**
1165 * @brief Copies the range [first,first+n) into result.
1166 * @param __first An input iterator.
1167 * @param __n The number of elements to copy.
1168 * @param __result An output iterator.
1169 * @return __result + __n
1170 * @since C++11
1171 *
1172 * Like copy_n(), but does not require an initialized output range.
1173 */
1174 template<typename _InputIterator, typename _Size, typename _ForwardIterator>
1175 _GLIBCXX26_CONSTEXPR
1176 inline _ForwardIterator
1177 uninitialized_copy_n(_InputIterator __first, _Size __n,
1178 _ForwardIterator __result)
1179 { return std::__uninitialized_copy_n(__first, __n, __result,
1180 std::__iterator_category(__first)); }
1181
1182 /// @cond undocumented
1183 template<typename _InputIterator, typename _Size, typename _ForwardIterator>
1184 _GLIBCXX26_CONSTEXPR
1186 __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
1187 _ForwardIterator __result)
1188 {
1189 return
1190 std::__uninitialized_copy_n_pair(__first, __n, __result,
1191 std::__iterator_category(__first));
1192 }
1193 /// @endcond
1194#endif
1195
1196#ifdef __glibcxx_raw_memory_algorithms // C++ >= 17
1197 /**
1198 * @brief Default-initializes objects in the range [first,last).
1199 * @param __first A forward iterator.
1200 * @param __last A forward iterator.
1201 * @since C++17
1202 */
1203 template <typename _ForwardIterator>
1204 _GLIBCXX26_CONSTEXPR
1205 inline void
1206 uninitialized_default_construct(_ForwardIterator __first,
1207 _ForwardIterator __last)
1208 {
1209 std::__uninitialized_default_novalue(__first, __last);
1210 }
1211
1212 /**
1213 * @brief Default-initializes objects in the range [first,first+count).
1214 * @param __first A forward iterator.
1215 * @param __count The number of objects to construct.
1216 * @return __first + __count
1217 * @since C++17
1218 */
1219 template <typename _ForwardIterator, typename _Size>
1220 _GLIBCXX26_CONSTEXPR
1221 inline _ForwardIterator
1222 uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
1223 {
1224 return std::__uninitialized_default_novalue_n(__first, __count);
1225 }
1226
1227 /**
1228 * @brief Value-initializes objects in the range [first,last).
1229 * @param __first A forward iterator.
1230 * @param __last A forward iterator.
1231 * @since C++17
1232 */
1233 template <typename _ForwardIterator>
1234 _GLIBCXX26_CONSTEXPR
1235 inline void
1236 uninitialized_value_construct(_ForwardIterator __first,
1237 _ForwardIterator __last)
1238 {
1239 return std::__uninitialized_default(__first, __last);
1240 }
1241
1242 /**
1243 * @brief Value-initializes objects in the range [first,first+count).
1244 * @param __first A forward iterator.
1245 * @param __count The number of objects to construct.
1246 * @return __result + __count
1247 * @since C++17
1248 */
1249 template <typename _ForwardIterator, typename _Size>
1250 _GLIBCXX26_CONSTEXPR
1251 inline _ForwardIterator
1252 uninitialized_value_construct_n(_ForwardIterator __first, _Size __count)
1253 {
1254 return std::__uninitialized_default_n(__first, __count);
1255 }
1256
1257 /**
1258 * @brief Move-construct from the range [first,last) into result.
1259 * @param __first An input iterator.
1260 * @param __last An input iterator.
1261 * @param __result An output iterator.
1262 * @return __result + (__first - __last)
1263 * @since C++17
1264 */
1265 template <typename _InputIterator, typename _ForwardIterator>
1266 _GLIBCXX26_CONSTEXPR
1267 inline _ForwardIterator
1268 uninitialized_move(_InputIterator __first, _InputIterator __last,
1269 _ForwardIterator __result)
1270 {
1271 return std::uninitialized_copy(std::make_move_iterator(__first),
1272 std::make_move_iterator(__last),
1273 __result);
1274 }
1275
1276 /**
1277 * @brief Move-construct from the range [first,first+count) into result.
1278 * @param __first An input iterator.
1279 * @param __count The number of objects to initialize.
1280 * @param __result An output iterator.
1281 * @return __result + __count
1282 * @since C++17
1283 */
1284 template <typename _InputIterator, typename _Size, typename _ForwardIterator>
1285 _GLIBCXX26_CONSTEXPR
1287 uninitialized_move_n(_InputIterator __first, _Size __count,
1288 _ForwardIterator __result)
1289 {
1290 auto __res
1291 = std::__uninitialized_copy_n_pair(std::make_move_iterator(__first),
1292 __count, __result);
1293 return {__res.first.base(), __res.second};
1294 }
1295#endif // __glibcxx_raw_memory_algorithms
1296
1297#if __cplusplus >= 201103L
1298 /// @cond undocumented
1299
1300 template<typename _Tp, typename _Up, typename _Allocator>
1301 _GLIBCXX20_CONSTEXPR
1302 inline void
1303 __relocate_object_a(_Tp* __restrict __dest, _Up* __restrict __orig,
1304 _Allocator& __alloc)
1305 noexcept(noexcept(std::allocator_traits<_Allocator>::construct(__alloc,
1306 __dest, std::move(*__orig)))
1308 __alloc, std::addressof(*__orig))))
1309 {
1310 typedef std::allocator_traits<_Allocator> __traits;
1311 __traits::construct(__alloc, __dest, std::move(*__orig));
1312 __traits::destroy(__alloc, std::addressof(*__orig));
1313 }
1314
1315 // This class may be specialized for specific types.
1316 // Also known as is_trivially_relocatable.
1317 template<typename _Tp, typename = void>
1318 struct __is_bitwise_relocatable
1319 : __bool_constant<__is_trivial(_Tp)>
1320 { };
1321
1322 template <typename _InputIterator, typename _ForwardIterator,
1323 typename _Allocator>
1324 _GLIBCXX20_CONSTEXPR
1325 inline _ForwardIterator
1326 __relocate_a_1(_InputIterator __first, _InputIterator __last,
1327 _ForwardIterator __result, _Allocator& __alloc)
1328 noexcept(noexcept(std::__relocate_object_a(std::addressof(*__result),
1329 std::addressof(*__first),
1330 __alloc)))
1331 {
1333 _ValueType;
1335 _ValueType2;
1336 static_assert(std::is_same<_ValueType, _ValueType2>::value,
1337 "relocation is only possible for values of the same type");
1338 _ForwardIterator __cur = __result;
1339 for (; __first != __last; ++__first, (void)++__cur)
1340 std::__relocate_object_a(std::addressof(*__cur),
1341 std::addressof(*__first), __alloc);
1342 return __cur;
1343 }
1344
1345#if _GLIBCXX_HOSTED
1346 template <typename _Tp, typename _Up>
1347 _GLIBCXX20_CONSTEXPR
1348 inline __enable_if_t<std::__is_bitwise_relocatable<_Tp>::value, _Tp*>
1349 __relocate_a_1(_Tp* __first, _Tp* __last,
1350 _Tp* __result,
1351 [[__maybe_unused__]] allocator<_Up>& __alloc) noexcept
1352 {
1353 ptrdiff_t __count = __last - __first;
1354 if (__count > 0)
1355 {
1356#ifdef __cpp_lib_is_constant_evaluated
1357 if (std::is_constant_evaluated())
1358 {
1359 // Can't use memcpy. Wrap the pointer so that __relocate_a_1
1360 // resolves to the non-trivial overload above.
1361 __gnu_cxx::__normal_iterator<_Tp*, void> __out(__result);
1362 __out = std::__relocate_a_1(__first, __last, __out, __alloc);
1363 return __out.base();
1364 }
1365#endif
1366 __builtin_memcpy(__result, __first, __count * sizeof(_Tp));
1367 }
1368 return __result + __count;
1369 }
1370#endif
1371
1372 template <typename _InputIterator, typename _ForwardIterator,
1373 typename _Allocator>
1374 _GLIBCXX20_CONSTEXPR
1375 inline _ForwardIterator
1376 __relocate_a(_InputIterator __first, _InputIterator __last,
1377 _ForwardIterator __result, _Allocator& __alloc)
1378 noexcept(noexcept(__relocate_a_1(std::__niter_base(__first),
1379 std::__niter_base(__last),
1380 std::__niter_base(__result), __alloc)))
1381 {
1382 return std::__relocate_a_1(std::__niter_base(__first),
1383 std::__niter_base(__last),
1384 std::__niter_base(__result), __alloc);
1385 }
1386
1387 /// @endcond
1388#endif // C++11
1389
1390 /// @} group memory
1391
1392_GLIBCXX_END_NAMESPACE_VERSION
1393} // namespace
1394
1395#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:2309
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
constexpr iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
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:486
is_pointer
Definition type_traits:573
is_copy_assignable
Definition type_traits:1324
is_trivially_default_constructible
Definition type_traits:1392
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
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.
Traits class for iterators.
Uniform interface to C++98 and C++11 allocators.