Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/c++/15/bits/ranges_uninitialized.h
1 // Raw memory manipulators -*- C++ -*- 2 3 // Copyright (C) 2020-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 /** @file bits/ranges_uninitialized.h 26 * This is an internal header file, included by other library headers. 27 * Do not attempt to use it directly. @headername{memory} 28 */ 29 30 #ifndef _RANGES_UNINITIALIZED_H 31 #define _RANGES_UNINITIALIZED_H 1 32 33 #if __cplusplus > 201703L 34 #if __cpp_lib_concepts 35 36 #include <bits/ranges_algobase.h> 37 38 namespace std _GLIBCXX_VISIBILITY(default) 39 { 40 _GLIBCXX_BEGIN_NAMESPACE_VERSION 41 namespace ranges 42 { 43 namespace __detail 44 { 45 template<typename _Tp> 46 constexpr void* 47 __voidify(_Tp& __obj) noexcept 48 { 49 return const_cast<void*> 50 (static_cast<const volatile void*>(std::__addressof(__obj))); 51 } 52 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>>); 59 60 template<typename _Sent, typename _Iter> 61 concept __nothrow_sentinel = sentinel_for<_Sent, _Iter>; 62 63 template<typename _Range> 64 concept __nothrow_input_range 65 = (range<_Range> 66 && __nothrow_input_iterator<iterator_t<_Range>> 67 && __nothrow_sentinel<sentinel_t<_Range>, iterator_t<_Range>>); 68 69 template<typename _Iter> 70 concept __nothrow_forward_iterator 71 = (__nothrow_input_iterator<_Iter> 72 && forward_iterator<_Iter> 73 && __nothrow_sentinel<_Iter, _Iter>); 74 75 template<typename _Range> 76 concept __nothrow_forward_range 77 = (__nothrow_input_range<_Range> 78 && __nothrow_forward_iterator<iterator_t<_Range>>); 79 } // namespace __detail 80 81 struct __destroy_fn 82 { 83 template<__detail::__nothrow_input_iterator _Iter, 84 __detail::__nothrow_sentinel<_Iter> _Sent> 85 requires destructible<iter_value_t<_Iter>> 86 constexpr _Iter 87 operator()(_Iter __first, _Sent __last) const noexcept; 88 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; 93 }; 94 95 inline constexpr __destroy_fn destroy{}; 96 97 namespace __detail 98 { 99 template<typename _Iter> 100 requires destructible<iter_value_t<_Iter>> 101 struct _DestroyGuard 102 { 103 private: 104 _Iter _M_first; 105 const _Iter* _M_cur; 106 107 public: 108 constexpr 109 explicit 110 _DestroyGuard(const _Iter& __iter) 111 : _M_first(__iter), _M_cur(std::__addressof(__iter)) 112 { } 113 114 constexpr 115 void 116 release() noexcept 117 { _M_cur = nullptr; } 118 119 constexpr 120 ~_DestroyGuard() 121 { 122 if (_M_cur != nullptr) 123 ranges::destroy(std::move(_M_first), *_M_cur); 124 } 125 }; 126 127 template<typename _Iter> 128 requires destructible<iter_value_t<_Iter>> 129 && is_trivially_destructible_v<iter_value_t<_Iter>> 130 struct _DestroyGuard<_Iter> 131 { 132 constexpr 133 explicit 134 _DestroyGuard(const _Iter&) 135 { } 136 137 constexpr 138 void 139 release() noexcept 140 { } 141 }; 142 } // namespace __detail 143 144 struct __uninitialized_default_construct_fn 145 { 146 template<__detail::__nothrow_forward_iterator _Iter, 147 __detail::__nothrow_sentinel<_Iter> _Sent> 148 requires default_initializable<iter_value_t<_Iter>> 149 _GLIBCXX26_CONSTEXPR 150 _Iter 151 operator()(_Iter __first, _Sent __last) const 152 { 153 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>; 154 if constexpr (is_trivially_default_constructible_v<_ValueType>) 155 return ranges::next(__first, __last); 156 else 157 { 158 auto __guard = __detail::_DestroyGuard(__first); 159 for (; __first != __last; ++__first) 160 ::new (__detail::__voidify(*__first)) _ValueType; 161 __guard.release(); 162 return __first; 163 } 164 } 165 166 template<__detail::__nothrow_forward_range _Range> 167 requires default_initializable<range_value_t<_Range>> 168 _GLIBCXX26_CONSTEXPR 169 borrowed_iterator_t<_Range> 170 operator()(_Range&& __r) const 171 { 172 return (*this)(ranges::begin(__r), ranges::end(__r)); 173 } 174 }; 175 176 inline constexpr __uninitialized_default_construct_fn 177 uninitialized_default_construct{}; 178 179 struct __uninitialized_default_construct_n_fn 180 { 181 template<__detail::__nothrow_forward_iterator _Iter> 182 requires default_initializable<iter_value_t<_Iter>> 183 _GLIBCXX26_CONSTEXPR 184 _Iter 185 operator()(_Iter __first, iter_difference_t<_Iter> __n) const 186 { 187 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>; 188 if constexpr (is_trivially_default_constructible_v<_ValueType>) 189 return ranges::next(__first, __n); 190 else 191 { 192 auto __guard = __detail::_DestroyGuard(__first); 193 for (; __n > 0; ++__first, (void) --__n) 194 ::new (__detail::__voidify(*__first)) _ValueType; 195 __guard.release(); 196 return __first; 197 } 198 } 199 }; 200 201 inline constexpr __uninitialized_default_construct_n_fn 202 uninitialized_default_construct_n; 203 204 struct __uninitialized_value_construct_fn 205 { 206 template<__detail::__nothrow_forward_iterator _Iter, 207 __detail::__nothrow_sentinel<_Iter> _Sent> 208 requires default_initializable<iter_value_t<_Iter>> 209 _GLIBCXX26_CONSTEXPR 210 _Iter 211 operator()(_Iter __first, _Sent __last) const 212 { 213 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>; 214 if constexpr (is_trivially_default_constructible_v<_ValueType> 215 && is_trivially_copy_assignable_v<_ValueType>) 216 return ranges::fill(__first, __last, _ValueType()); 217 else 218 { 219 auto __guard = __detail::_DestroyGuard(__first); 220 for (; __first != __last; ++__first) 221 ::new (__detail::__voidify(*__first)) _ValueType(); 222 __guard.release(); 223 return __first; 224 } 225 } 226 227 template<__detail::__nothrow_forward_range _Range> 228 requires default_initializable<range_value_t<_Range>> 229 _GLIBCXX26_CONSTEXPR 230 borrowed_iterator_t<_Range> 231 operator()(_Range&& __r) const 232 { 233 return (*this)(ranges::begin(__r), ranges::end(__r)); 234 } 235 }; 236 237 inline constexpr __uninitialized_value_construct_fn 238 uninitialized_value_construct{}; 239 240 struct __uninitialized_value_construct_n_fn 241 { 242 template<__detail::__nothrow_forward_iterator _Iter> 243 requires default_initializable<iter_value_t<_Iter>> 244 _GLIBCXX26_CONSTEXPR 245 _Iter 246 operator()(_Iter __first, iter_difference_t<_Iter> __n) const 247 { 248 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>; 249 if constexpr (is_trivially_default_constructible_v<_ValueType> 250 && is_trivially_copy_assignable_v<_ValueType>) 251 return ranges::fill_n(__first, __n, _ValueType()); 252 else 253 { 254 auto __guard = __detail::_DestroyGuard(__first); 255 for (; __n > 0; ++__first, (void) --__n) 256 ::new (__detail::__voidify(*__first)) _ValueType(); 257 __guard.release(); 258 return __first; 259 } 260 } 261 }; 262 263 inline constexpr __uninitialized_value_construct_n_fn 264 uninitialized_value_construct_n; 265 266 template<typename _Iter, typename _Out> 267 using uninitialized_copy_result = in_out_result<_Iter, _Out>; 268 269 struct __uninitialized_copy_fn 270 { 271 template<input_iterator _Iter, sentinel_for<_Iter> _ISent, 272 __detail::__nothrow_forward_iterator _Out, 273 __detail::__nothrow_sentinel<_Out> _OSent> 274 requires constructible_from<iter_value_t<_Out>, iter_reference_t<_Iter>> 275 _GLIBCXX26_CONSTEXPR 276 uninitialized_copy_result<_Iter, _Out> 277 operator()(_Iter __ifirst, _ISent __ilast, 278 _Out __ofirst, _OSent __olast) const 279 { 280 using _OutType = remove_reference_t<iter_reference_t<_Out>>; 281 if constexpr (sized_sentinel_for<_ISent, _Iter> 282 && sized_sentinel_for<_OSent, _Out> 283 && is_trivially_constructible_v<_OutType, iter_reference_t<_Iter>> 284 && is_trivially_default_constructible_v<_OutType> 285 && is_trivially_assignable_v<_OutType&, 286 iter_reference_t<_Iter>>) 287 { 288 auto __d = __ilast - __ifirst; 289 if (auto __d2 = __olast - __ofirst; __d2 < __d) 290 __d = static_cast<iter_difference_t<_Iter>>(__d2); 291 return ranges::copy_n(std::move(__ifirst), __d, __ofirst); 292 } 293 else 294 { 295 auto __guard = __detail::_DestroyGuard(__ofirst); 296 for (; __ifirst != __ilast && __ofirst != __olast; 297 ++__ofirst, (void)++__ifirst) 298 ::new (__detail::__voidify(*__ofirst)) _OutType(*__ifirst); 299 __guard.release(); 300 return {std::move(__ifirst), __ofirst}; 301 } 302 } 303 304 template<input_range _IRange, __detail::__nothrow_forward_range _ORange> 305 requires constructible_from<range_value_t<_ORange>, 306 range_reference_t<_IRange>> 307 _GLIBCXX26_CONSTEXPR 308 uninitialized_copy_result<borrowed_iterator_t<_IRange>, 309 borrowed_iterator_t<_ORange>> 310 operator()(_IRange&& __inr, _ORange&& __outr) const 311 { 312 return (*this)(ranges::begin(__inr), ranges::end(__inr), 313 ranges::begin(__outr), ranges::end(__outr)); 314 } 315 }; 316 317 inline constexpr __uninitialized_copy_fn uninitialized_copy{}; 318 319 template<typename _Iter, typename _Out> 320 using uninitialized_copy_n_result = in_out_result<_Iter, _Out>; 321 322 struct __uninitialized_copy_n_fn 323 { 324 template<input_iterator _Iter, __detail::__nothrow_forward_iterator _Out, 325 __detail::__nothrow_sentinel<_Out> _Sent> 326 requires constructible_from<iter_value_t<_Out>, iter_reference_t<_Iter>> 327 _GLIBCXX26_CONSTEXPR 328 uninitialized_copy_n_result<_Iter, _Out> 329 operator()(_Iter __ifirst, iter_difference_t<_Iter> __n, 330 _Out __ofirst, _Sent __olast) const 331 { 332 using _OutType = remove_reference_t<iter_reference_t<_Out>>; 333 if constexpr (sized_sentinel_for<_Sent, _Out> 334 && is_trivially_constructible_v<_OutType, iter_reference_t<_Iter>> 335 && is_trivially_default_constructible_v<_OutType> 336 && is_trivially_assignable_v<_OutType&, 337 iter_reference_t<_Iter>>) 338 { 339 if (auto __d = __olast - __ofirst; __d < __n) 340 __n = static_cast<iter_difference_t<_Iter>>(__d); 341 return ranges::copy_n(std::move(__ifirst), __n, __ofirst); 342 } 343 else 344 { 345 auto __guard = __detail::_DestroyGuard(__ofirst); 346 for (; __n > 0 && __ofirst != __olast; 347 ++__ofirst, (void)++__ifirst, (void)--__n) 348 ::new (__detail::__voidify(*__ofirst)) _OutType(*__ifirst); 349 __guard.release(); 350 return {std::move(__ifirst), __ofirst}; 351 } 352 } 353 }; 354 355 inline constexpr __uninitialized_copy_n_fn uninitialized_copy_n{}; 356 357 template<typename _Iter, typename _Out> 358 using uninitialized_move_result = in_out_result<_Iter, _Out>; 359 360 struct __uninitialized_move_fn 361 { 362 template<input_iterator _Iter, sentinel_for<_Iter> _ISent, 363 __detail::__nothrow_forward_iterator _Out, 364 __detail::__nothrow_sentinel<_Out> _OSent> 365 requires constructible_from<iter_value_t<_Out>, 366 iter_rvalue_reference_t<_Iter>> 367 _GLIBCXX26_CONSTEXPR 368 uninitialized_move_result<_Iter, _Out> 369 operator()(_Iter __ifirst, _ISent __ilast, 370 _Out __ofirst, _OSent __olast) const 371 { 372 using _OutType = remove_reference_t<iter_reference_t<_Out>>; 373 if constexpr (sized_sentinel_for<_ISent, _Iter> 374 && sized_sentinel_for<_OSent, _Out> 375 && is_trivially_constructible_v<_OutType, iter_rvalue_reference_t<_Iter>> 376 && is_trivially_default_constructible_v<_OutType> 377 && is_trivially_assignable_v<_OutType&, 378 iter_rvalue_reference_t<_Iter>>) 379 { 380 auto __d = __ilast - __ifirst; 381 if (auto __d2 = __olast - __ofirst; __d2 < __d) 382 __d = static_cast<iter_difference_t<_Iter>>(__d2); 383 auto [__in, __out] 384 = ranges::copy_n(std::make_move_iterator(std::move(__ifirst)), 385 __d, __ofirst); 386 return {std::move(__in).base(), __out}; 387 } 388 else 389 { 390 auto __guard = __detail::_DestroyGuard(__ofirst); 391 for (; __ifirst != __ilast && __ofirst != __olast; 392 ++__ofirst, (void)++__ifirst) 393 ::new (__detail::__voidify(*__ofirst)) 394 _OutType(ranges::iter_move(__ifirst)); 395 __guard.release(); 396 return {std::move(__ifirst), __ofirst}; 397 } 398 } 399 400 template<input_range _IRange, __detail::__nothrow_forward_range _ORange> 401 requires constructible_from<range_value_t<_ORange>, 402 range_rvalue_reference_t<_IRange>> 403 _GLIBCXX26_CONSTEXPR 404 uninitialized_move_result<borrowed_iterator_t<_IRange>, 405 borrowed_iterator_t<_ORange>> 406 operator()(_IRange&& __inr, _ORange&& __outr) const 407 { 408 return (*this)(ranges::begin(__inr), ranges::end(__inr), 409 ranges::begin(__outr), ranges::end(__outr)); 410 } 411 }; 412 413 inline constexpr __uninitialized_move_fn uninitialized_move{}; 414 415 template<typename _Iter, typename _Out> 416 using uninitialized_move_n_result = in_out_result<_Iter, _Out>; 417 418 struct __uninitialized_move_n_fn 419 { 420 template<input_iterator _Iter, __detail::__nothrow_forward_iterator _Out, 421 __detail::__nothrow_sentinel<_Out> _Sent> 422 requires constructible_from<iter_value_t<_Out>, 423 iter_rvalue_reference_t<_Iter>> 424 _GLIBCXX26_CONSTEXPR 425 uninitialized_move_n_result<_Iter, _Out> 426 operator()(_Iter __ifirst, iter_difference_t<_Iter> __n, 427 _Out __ofirst, _Sent __olast) const 428 { 429 using _OutType = remove_reference_t<iter_reference_t<_Out>>; 430 if constexpr (sized_sentinel_for<_Sent, _Out> 431 && is_trivially_constructible_v<_OutType, iter_rvalue_reference_t<_Iter>> 432 && is_trivially_default_constructible_v<_OutType> 433 && is_trivially_assignable_v<_OutType&, 434 iter_rvalue_reference_t<_Iter>>) 435 { 436 if (auto __d = __olast - __ofirst; __d < __n) 437 __n = static_cast<iter_difference_t<_Iter>>(__d); 438 auto [__in, __out] 439 = ranges::copy_n(std::make_move_iterator(std::move(__ifirst)), 440 __n, __ofirst); 441 return {std::move(__in).base(), __out}; 442 } 443 else 444 { 445 auto __guard = __detail::_DestroyGuard(__ofirst); 446 for (; __n > 0 && __ofirst != __olast; 447 ++__ofirst, (void)++__ifirst, (void)--__n) 448 ::new (__detail::__voidify(*__ofirst)) 449 _OutType(ranges::iter_move(__ifirst)); 450 __guard.release(); 451 return {std::move(__ifirst), __ofirst}; 452 } 453 } 454 }; 455 456 inline constexpr __uninitialized_move_n_fn uninitialized_move_n{}; 457 458 struct __uninitialized_fill_fn 459 { 460 template<__detail::__nothrow_forward_iterator _Iter, 461 __detail::__nothrow_sentinel<_Iter> _Sent, typename _Tp> 462 requires constructible_from<iter_value_t<_Iter>, const _Tp&> 463 _GLIBCXX26_CONSTEXPR 464 _Iter 465 operator()(_Iter __first, _Sent __last, const _Tp& __x) const 466 { 467 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>; 468 if constexpr (is_trivially_constructible_v<_ValueType, const _Tp&> 469 && is_trivially_assignable_v<_ValueType&, const _Tp&>) 470 return ranges::fill(__first, __last, __x); 471 else 472 { 473 auto __guard = __detail::_DestroyGuard(__first); 474 for (; __first != __last; ++__first) 475 ::new (__detail::__voidify(*__first)) _ValueType(__x); 476 __guard.release(); 477 return __first; 478 } 479 } 480 481 template<__detail::__nothrow_forward_range _Range, typename _Tp> 482 requires constructible_from<range_value_t<_Range>, const _Tp&> 483 _GLIBCXX26_CONSTEXPR 484 borrowed_iterator_t<_Range> 485 operator()(_Range&& __r, const _Tp& __x) const 486 { 487 return (*this)(ranges::begin(__r), ranges::end(__r), __x); 488 } 489 }; 490 491 inline constexpr __uninitialized_fill_fn uninitialized_fill{}; 492 493 struct __uninitialized_fill_n_fn 494 { 495 template<__detail::__nothrow_forward_iterator _Iter, typename _Tp> 496 requires constructible_from<iter_value_t<_Iter>, const _Tp&> 497 _GLIBCXX26_CONSTEXPR 498 _Iter 499 operator()(_Iter __first, iter_difference_t<_Iter> __n, 500 const _Tp& __x) const 501 { 502 using _ValueType = remove_reference_t<iter_reference_t<_Iter>>; 503 if constexpr (is_trivially_constructible_v<_ValueType, const _Tp&> 504 && is_trivially_assignable_v<_ValueType&, const _Tp&>) 505 return ranges::fill_n(__first, __n, __x); 506 else 507 { 508 auto __guard = __detail::_DestroyGuard(__first); 509 for (; __n > 0; ++__first, (void)--__n) 510 ::new (__detail::__voidify(*__first)) _ValueType(__x); 511 __guard.release(); 512 return __first; 513 } 514 } 515 }; 516 517 inline constexpr __uninitialized_fill_n_fn uninitialized_fill_n{}; 518 519 struct __construct_at_fn 520 { 521 template<typename _Tp, typename... _Args> 522 requires requires { 523 ::new (std::declval<void*>()) _Tp(std::declval<_Args>()...); 524 } 525 constexpr _Tp* 526 operator()(_Tp* __location, _Args&&... __args) const 527 noexcept(noexcept(std::construct_at(__location, 528 std::forward<_Args>(__args)...))) 529 { 530 return std::construct_at(__location, 531 std::forward<_Args>(__args)...); 532 } 533 }; 534 535 inline constexpr __construct_at_fn construct_at{}; 536 537 struct __destroy_at_fn 538 { 539 template<destructible _Tp> 540 constexpr void 541 operator()(_Tp* __location) const noexcept 542 { 543 if constexpr (is_array_v<_Tp>) 544 ranges::destroy(ranges::begin(*__location), ranges::end(*__location)); 545 else 546 __location->~_Tp(); 547 } 548 }; 549 550 inline constexpr __destroy_at_fn destroy_at{}; 551 552 template<__detail::__nothrow_input_iterator _Iter, 553 __detail::__nothrow_sentinel<_Iter> _Sent> 554 requires destructible<iter_value_t<_Iter>> 555 constexpr _Iter 556 __destroy_fn::operator()(_Iter __first, _Sent __last) const noexcept 557 { 558 if constexpr (is_trivially_destructible_v<iter_value_t<_Iter>>) 559 return ranges::next(std::move(__first), __last); 560 else 561 { 562 for (; __first != __last; ++__first) 563 ranges::destroy_at(std::__addressof(*__first)); 564 return __first; 565 } 566 } 567 568 template<__detail::__nothrow_input_range _Range> 569 requires destructible<range_value_t<_Range>> 570 constexpr borrowed_iterator_t<_Range> 571 __destroy_fn::operator()(_Range&& __r) const noexcept 572 { 573 return (*this)(ranges::begin(__r), ranges::end(__r)); 574 } 575 576 struct __destroy_n_fn 577 { 578 template<__detail::__nothrow_input_iterator _Iter> 579 requires destructible<iter_value_t<_Iter>> 580 constexpr _Iter 581 operator()(_Iter __first, iter_difference_t<_Iter> __n) const noexcept 582 { 583 if constexpr (is_trivially_destructible_v<iter_value_t<_Iter>>) 584 return ranges::next(std::move(__first), __n); 585 else 586 { 587 for (; __n > 0; ++__first, (void)--__n) 588 ranges::destroy_at(std::__addressof(*__first)); 589 return __first; 590 } 591 } 592 }; 593 594 inline constexpr __destroy_n_fn destroy_n{}; 595 } 596 _GLIBCXX_END_NAMESPACE_VERSION 597 } // namespace std 598 599 #endif // concepts 600 #endif // C++20 601 #endif // _RANGES_UNINITIALIZED_H