libstdc++: stl_uninitialized.h Source File

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