30 #ifndef _RANGES_UNINITIALIZED_H
31 #define _RANGES_UNINITIALIZED_H 1
33 #if __cplusplus > 201703L
34 #if __cpp_lib_concepts
38 namespace std _GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 template<
typename _Tp>
47 __voidify(_Tp& __obj) noexcept
49 return const_cast<void*
>
53 template<
typename _Iter>
54 concept __nothrow_input_iterator
55 = (input_iterator<_Iter>
56 && is_lvalue_reference_v<iter_reference_t<_Iter>>
57 && same_as<remove_cvref_t<iter_reference_t<_Iter>>,
58 iter_value_t<_Iter>>);
60 template<
typename _Sent,
typename _Iter>
61 concept __nothrow_sentinel = sentinel_for<_Sent, _Iter>;
63 template<
typename _Range>
64 concept __nothrow_input_range
66 && __nothrow_input_iterator<iterator_t<_Range>>
67 && __nothrow_sentinel<sentinel_t<_Range>, iterator_t<_Range>>);
69 template<
typename _Iter>
70 concept __nothrow_forward_iterator
71 = (__nothrow_input_iterator<_Iter>
72 && forward_iterator<_Iter>
73 && __nothrow_sentinel<_Iter, _Iter>);
75 template<
typename _Range>
76 concept __nothrow_forward_range
77 = (__nothrow_input_range<_Range>
78 && __nothrow_forward_iterator<iterator_t<_Range>>);
83 template<__detail::__nothrow_input_iterator _Iter,
84 __detail::__nothrow_sentinel<_Iter> _Sent>
85 requires destructible<iter_value_t<_Iter>>
87 operator()(_Iter __first, _Sent __last)
const noexcept;
89 template<__detail::__nothrow_input_range _Range>
90 requires destructible<range_value_t<_Range>>
91 constexpr borrowed_iterator_t<_Range>
92 operator()(_Range&& __r)
const noexcept;
95 inline constexpr __destroy_fn destroy{};
99 template<
typename _Iter>
100 requires destructible<iter_value_t<_Iter>>
109 _DestroyGuard(
const _Iter& __iter)
115 { _M_cur =
nullptr; }
119 if (_M_cur !=
nullptr)
120 ranges::destroy(
std::move(_M_first), *_M_cur);
124 template<
typename _Iter>
125 requires destructible<iter_value_t<_Iter>>
126 && is_trivially_destructible_v<iter_value_t<_Iter>>
127 struct _DestroyGuard<_Iter>
130 _DestroyGuard(
const _Iter&)
139 struct __uninitialized_default_construct_fn
141 template<__detail::__nothrow_forward_iterator _Iter,
142 __detail::__nothrow_sentinel<_Iter> _Sent>
143 requires default_initializable<iter_value_t<_Iter>>
145 operator()(_Iter __first, _Sent __last)
const
147 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>;
148 if constexpr (is_trivially_default_constructible_v<_ValueType>)
149 return ranges::next(__first, __last);
152 auto __guard = __detail::_DestroyGuard(__first);
153 for (; __first != __last; ++__first)
154 ::
new (__detail::__voidify(*__first)) _ValueType;
160 template<__detail::__nothrow_forward_range _Range>
161 requires default_initializable<range_value_t<_Range>>
162 borrowed_iterator_t<_Range>
163 operator()(_Range&& __r)
const
165 return (*
this)(ranges::begin(__r), ranges::end(__r));
169 inline constexpr __uninitialized_default_construct_fn
172 struct __uninitialized_default_construct_n_fn
174 template<__detail::__nothrow_forward_iterator _Iter>
175 requires default_initializable<iter_value_t<_Iter>>
177 operator()(_Iter __first, iter_difference_t<_Iter> __n)
const
179 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>;
180 if constexpr (is_trivially_default_constructible_v<_ValueType>)
181 return ranges::next(__first, __n);
184 auto __guard = __detail::_DestroyGuard(__first);
185 for (; __n > 0; ++__first, (void) --__n)
186 ::new (__detail::__voidify(*__first)) _ValueType;
193 inline constexpr __uninitialized_default_construct_n_fn
196 struct __uninitialized_value_construct_fn
198 template<__detail::__nothrow_forward_iterator _Iter,
199 __detail::__nothrow_sentinel<_Iter> _Sent>
200 requires default_initializable<iter_value_t<_Iter>>
202 operator()(_Iter __first, _Sent __last)
const
204 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>;
205 if constexpr (is_trivial_v<_ValueType>
206 && is_copy_assignable_v<_ValueType>)
207 return ranges::fill(__first, __last, _ValueType());
210 auto __guard = __detail::_DestroyGuard(__first);
211 for (; __first != __last; ++__first)
212 ::
new (__detail::__voidify(*__first)) _ValueType();
218 template<__detail::__nothrow_forward_range _Range>
219 requires default_initializable<range_value_t<_Range>>
220 borrowed_iterator_t<_Range>
221 operator()(_Range&& __r)
const
223 return (*
this)(ranges::begin(__r), ranges::end(__r));
227 inline constexpr __uninitialized_value_construct_fn
230 struct __uninitialized_value_construct_n_fn
232 template<__detail::__nothrow_forward_iterator _Iter>
233 requires default_initializable<iter_value_t<_Iter>>
235 operator()(_Iter __first, iter_difference_t<_Iter> __n)
const
237 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>;
238 if constexpr (is_trivial_v<_ValueType>
239 && is_copy_assignable_v<_ValueType>)
240 return ranges::fill_n(__first, __n, _ValueType());
243 auto __guard = __detail::_DestroyGuard(__first);
244 for (; __n > 0; ++__first, (void) --__n)
245 ::new (__detail::__voidify(*__first)) _ValueType();
252 inline constexpr __uninitialized_value_construct_n_fn
255 template<
typename _Iter,
typename _Out>
256 using uninitialized_copy_result = in_out_result<_Iter, _Out>;
258 struct __uninitialized_copy_fn
260 template<input_iterator _Iter, sentinel_for<_Iter> _ISent,
261 __detail::__nothrow_forward_iterator _Out,
262 __detail::__nothrow_sentinel<_Out> _OSent>
263 requires constructible_from<iter_value_t<_Out>, iter_reference_t<_Iter>>
264 uninitialized_copy_result<_Iter, _Out>
265 operator()(_Iter __ifirst, _ISent __ilast,
266 _Out __ofirst, _OSent __olast)
const
268 using _OutType = remove_reference_t<iter_reference_t<_Out>>;
269 if constexpr (sized_sentinel_for<_ISent, _Iter>
270 && sized_sentinel_for<_OSent, _Out>
271 && is_trivial_v<_OutType>
272 && is_nothrow_assignable_v<_OutType&,
273 iter_reference_t<_Iter>>)
275 auto __d1 = __ilast - __ifirst;
276 auto __d2 = __olast - __ofirst;
282 auto __guard = __detail::_DestroyGuard(__ofirst);
283 for (; __ifirst != __ilast && __ofirst != __olast;
284 ++__ofirst, (void)++__ifirst)
285 ::new (__detail::__voidify(*__ofirst)) _OutType(*__ifirst);
291 template<input_range _IRange, __detail::__nothrow_forward_range _ORange>
292 requires constructible_from<range_value_t<_ORange>,
293 range_reference_t<_IRange>>
294 uninitialized_copy_result<borrowed_iterator_t<_IRange>,
295 borrowed_iterator_t<_ORange>>
296 operator()(_IRange&& __inr, _ORange&& __outr)
const
298 return (*
this)(ranges::begin(__inr), ranges::end(__inr),
299 ranges::begin(__outr), ranges::end(__outr));
305 template<
typename _Iter,
typename _Out>
306 using uninitialized_copy_n_result = in_out_result<_Iter, _Out>;
308 struct __uninitialized_copy_n_fn
310 template<input_iterator _Iter, __detail::__nothrow_forward_iterator _Out,
311 __detail::__nothrow_sentinel<_Out> _Sent>
312 requires constructible_from<iter_value_t<_Out>, iter_reference_t<_Iter>>
313 uninitialized_copy_n_result<_Iter, _Out>
314 operator()(_Iter __ifirst, iter_difference_t<_Iter> __n,
315 _Out __ofirst, _Sent __olast)
const
317 using _OutType = remove_reference_t<iter_reference_t<_Out>>;
318 if constexpr (sized_sentinel_for<_Sent, _Out>
319 && is_trivial_v<_OutType>
320 && is_nothrow_assignable_v<_OutType&,
321 iter_reference_t<_Iter>>)
323 auto __d = __olast - __ofirst;
329 auto __guard = __detail::_DestroyGuard(__ofirst);
330 for (; __n > 0 && __ofirst != __olast;
331 ++__ofirst, (void)++__ifirst, (
void)--__n)
332 ::
new (__detail::__voidify(*__ofirst)) _OutType(*__ifirst);
341 template<
typename _Iter,
typename _Out>
342 using uninitialized_move_result = in_out_result<_Iter, _Out>;
344 struct __uninitialized_move_fn
346 template<input_iterator _Iter, sentinel_for<_Iter> _ISent,
347 __detail::__nothrow_forward_iterator _Out,
348 __detail::__nothrow_sentinel<_Out> _OSent>
349 requires constructible_from<iter_value_t<_Out>,
350 iter_rvalue_reference_t<_Iter>>
351 uninitialized_move_result<_Iter, _Out>
352 operator()(_Iter __ifirst, _ISent __ilast,
353 _Out __ofirst, _OSent __olast)
const
355 using _OutType = remove_reference_t<iter_reference_t<_Out>>;
356 if constexpr (sized_sentinel_for<_ISent, _Iter>
357 && sized_sentinel_for<_OSent, _Out>
358 && is_trivial_v<_OutType>
359 && is_nothrow_assignable_v<_OutType&,
360 iter_rvalue_reference_t<_Iter>>)
362 auto __d1 = __ilast - __ifirst;
363 auto __d2 = __olast - __ofirst;
365 = ranges::copy_n(std::make_move_iterator(
std::move(__ifirst)),
371 auto __guard = __detail::_DestroyGuard(__ofirst);
372 for (; __ifirst != __ilast && __ofirst != __olast;
373 ++__ofirst, (void)++__ifirst)
374 ::new (__detail::__voidify(*__ofirst))
375 _OutType(ranges::iter_move(__ifirst));
381 template<input_range _IRange, __detail::__nothrow_forward_range _ORange>
382 requires constructible_from<range_value_t<_ORange>,
383 range_rvalue_reference_t<_IRange>>
384 uninitialized_move_result<borrowed_iterator_t<_IRange>,
385 borrowed_iterator_t<_ORange>>
386 operator()(_IRange&& __inr, _ORange&& __outr)
const
388 return (*
this)(ranges::begin(__inr), ranges::end(__inr),
389 ranges::begin(__outr), ranges::end(__outr));
395 template<
typename _Iter,
typename _Out>
396 using uninitialized_move_n_result = in_out_result<_Iter, _Out>;
398 struct __uninitialized_move_n_fn
400 template<input_iterator _Iter, __detail::__nothrow_forward_iterator _Out,
401 __detail::__nothrow_sentinel<_Out> _Sent>
402 requires constructible_from<iter_value_t<_Out>,
403 iter_rvalue_reference_t<_Iter>>
404 uninitialized_move_n_result<_Iter, _Out>
405 operator()(_Iter __ifirst, iter_difference_t<_Iter> __n,
406 _Out __ofirst, _Sent __olast)
const
408 using _OutType = remove_reference_t<iter_reference_t<_Out>>;
409 if constexpr (sized_sentinel_for<_Sent, _Out>
410 && is_trivial_v<_OutType>
411 && is_nothrow_assignable_v<_OutType&,
412 iter_rvalue_reference_t<_Iter>>)
414 auto __d = __olast - __ofirst;
416 = ranges::copy_n(std::make_move_iterator(
std::move(__ifirst)),
422 auto __guard = __detail::_DestroyGuard(__ofirst);
423 for (; __n > 0 && __ofirst != __olast;
424 ++__ofirst, (void)++__ifirst, (
void)--__n)
425 ::
new (__detail::__voidify(*__ofirst))
426 _OutType(ranges::iter_move(__ifirst));
435 struct __uninitialized_fill_fn
437 template<__detail::__nothrow_forward_iterator _Iter,
438 __detail::__nothrow_sentinel<_Iter> _Sent,
typename _Tp>
439 requires constructible_from<iter_value_t<_Iter>,
const _Tp&>
441 operator()(_Iter __first, _Sent __last,
const _Tp& __x)
const
443 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>;
444 if constexpr (is_trivial_v<_ValueType>
445 && is_nothrow_assignable_v<_ValueType&, const _Tp&>)
446 return ranges::fill(__first, __last, __x);
449 auto __guard = __detail::_DestroyGuard(__first);
450 for (; __first != __last; ++__first)
451 ::
new (__detail::__voidify(*__first)) _ValueType(__x);
457 template<__detail::__nothrow_forward_range _Range,
typename _Tp>
458 requires constructible_from<range_value_t<_Range>,
const _Tp&>
459 borrowed_iterator_t<_Range>
460 operator()(_Range&& __r,
const _Tp& __x)
const
462 return (*
this)(ranges::begin(__r), ranges::end(__r), __x);
468 struct __uninitialized_fill_n_fn
470 template<__detail::__nothrow_forward_iterator _Iter,
typename _Tp>
471 requires constructible_from<iter_value_t<_Iter>,
const _Tp&>
473 operator()(_Iter __first, iter_difference_t<_Iter> __n,
474 const _Tp& __x)
const
476 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>;
477 if constexpr (is_trivial_v<_ValueType>
478 && is_nothrow_assignable_v<_ValueType&, const _Tp&>)
479 return ranges::fill_n(__first, __n, __x);
482 auto __guard = __detail::_DestroyGuard(__first);
483 for (; __n > 0; ++__first, (void)--__n)
484 ::new (__detail::__voidify(*__first)) _ValueType(__x);
493 struct __construct_at_fn
495 template<
typename _Tp,
typename... _Args>
497 ::new (std::declval<void*>()) _Tp(
std::
declval<_Args>()...);
500 operator()(_Tp* __location, _Args&&... __args) const
501 noexcept(noexcept(
std::construct_at(__location,
504 return std::construct_at(__location,
505 std::forward<_Args>(__args)...);
509 inline constexpr __construct_at_fn construct_at{};
511 struct __destroy_at_fn
513 template<destructible _Tp>
515 operator()(_Tp* __location)
const noexcept
517 if constexpr (is_array_v<_Tp>)
518 ranges::destroy(ranges::begin(*__location), ranges::end(*__location));
524 inline constexpr __destroy_at_fn destroy_at{};
526 template<__detail::__nothrow_input_iterator _Iter,
527 __detail::__nothrow_sentinel<_Iter> _Sent>
528 requires destructible<iter_value_t<_Iter>>
530 __destroy_fn::operator()(_Iter __first, _Sent __last)
const noexcept
532 if constexpr (is_trivially_destructible_v<iter_value_t<_Iter>>)
533 return ranges::next(
std::
move(__first), __last);
536 for (; __first != __last; ++__first)
542 template<__detail::__nothrow_input_range _Range>
543 requires destructible<range_value_t<_Range>>
544 constexpr borrowed_iterator_t<_Range>
545 __destroy_fn::operator()(_Range&& __r)
const noexcept
547 return (*
this)(ranges::begin(__r), ranges::end(__r));
550 struct __destroy_n_fn
552 template<__detail::__nothrow_input_iterator _Iter>
553 requires destructible<iter_value_t<_Iter>>
555 operator()(_Iter __first, iter_difference_t<_Iter> __n)
const noexcept
557 if constexpr (is_trivially_destructible_v<iter_value_t<_Iter>>)
558 return ranges::next(
std::
move(__first), __n);
561 for (; __n > 0; ++__first, (void)--__n)
568 inline constexpr __destroy_n_fn destroy_n{};
570 _GLIBCXX_END_NAMESPACE_VERSION
pair< _InputIterator, _ForwardIterator > uninitialized_move_n(_InputIterator __first, _Size __count, _ForwardIterator __result)
Move-construct from the range [first,first+count) into result.
_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).
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
auto declval() noexcept -> decltype(__declval< _Tp >(0))
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
ISO C++ entities toplevel namespace is std.