stl_uninitialized.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  1. // Raw memory manipulators -*- C++ -*-
  2. // Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /*
  21. *
  22. * Copyright (c) 1994
  23. * Hewlett-Packard Company
  24. *
  25. * Permission to use, copy, modify, distribute and sell this software
  26. * and its documentation for any purpose is hereby granted without fee,
  27. * provided that the above copyright notice appear in all copies and
  28. * that both that copyright notice and this permission notice appear
  29. * in supporting documentation. Hewlett-Packard Company makes no
  30. * representations about the suitability of this software for any
  31. * purpose. It is provided "as is" without express or implied warranty.
  32. *
  33. *
  34. * Copyright (c) 1996,1997
  35. * Silicon Graphics Computer Systems, Inc.
  36. *
  37. * Permission to use, copy, modify, distribute and sell this software
  38. * and its documentation for any purpose is hereby granted without fee,
  39. * provided that the above copyright notice appear in all copies and
  40. * that both that copyright notice and this permission notice appear
  41. * in supporting documentation. Silicon Graphics makes no
  42. * representations about the suitability of this software for any
  43. * purpose. It is provided "as is" without express or implied warranty.
  44. */
  45. /** @file bits/stl_uninitialized.h
  46. * This is an internal header file, included by other library headers.
  47. * Do not attempt to use it directly. @headername{memory}
  48. */
  49. #ifndef _STL_UNINITIALIZED_H
  50. #define _STL_UNINITIALIZED_H 1
  51. #if __cplusplus >= 201103L
  52. #include <type_traits>
  53. #endif
  54. #include <bits/stl_algobase.h> // copy
  55. #include <ext/alloc_traits.h> // __alloc_traits
  56. #if __cplusplus >= 201703L
  57. #include <bits/stl_pair.h>
  58. #endif
  59. namespace std _GLIBCXX_VISIBILITY(default)
  60. {
  61. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  62. /** @addtogroup memory
  63. * @{
  64. */
  65. /// @cond undocumented
  66. #if __cplusplus >= 201103L
  67. template<typename _ValueType, typename _Tp>
  68. constexpr bool
  69. __check_constructible()
  70. {
  71. // Trivial types can have deleted constructors, but std::copy etc.
  72. // only use assignment (or memmove) not construction, so we need an
  73. // explicit check that construction from _Tp is actually valid,
  74. // otherwise some ill-formed uses of std::uninitialized_xxx would
  75. // compile without errors. This gives a nice clear error message.
  76. static_assert(is_constructible<_ValueType, _Tp>::value,
  77. "result type must be constructible from input type");
  78. return true;
  79. }
  80. // If the type is trivial we don't need to construct it, just assign to it.
  81. // But trivial types can still have deleted or inaccessible assignment,
  82. // so don't try to use std::copy or std::fill etc. if we can't assign.
  83. # define _GLIBCXX_USE_ASSIGN_FOR_INIT(T, U) \
  84. __is_trivial(T) && __is_assignable(T&, U) \
  85. && std::__check_constructible<T, U>()
  86. #else
  87. // No need to check if is_constructible<T, U> for C++98. Trivial types have
  88. // no user-declared constructors, so if the assignment is valid, construction
  89. // should be too.
  90. # define _GLIBCXX_USE_ASSIGN_FOR_INIT(T, U) \
  91. __is_trivial(T) && __is_assignable(T&, U)
  92. #endif
  93. template<typename _InputIterator, typename _ForwardIterator>
  94. _GLIBCXX20_CONSTEXPR
  95. _ForwardIterator
  96. __do_uninit_copy(_InputIterator __first, _InputIterator __last,
  97. _ForwardIterator __result)
  98. {
  99. _ForwardIterator __cur = __result;
  100. __try
  101. {
  102. for (; __first != __last; ++__first, (void)++__cur)
  103. std::_Construct(std::__addressof(*__cur), *__first);
  104. return __cur;
  105. }
  106. __catch(...)
  107. {
  108. std::_Destroy(__result, __cur);
  109. __throw_exception_again;
  110. }
  111. }
  112. template<bool _TrivialValueTypes>
  113. struct __uninitialized_copy
  114. {
  115. template<typename _InputIterator, typename _ForwardIterator>
  116. static _ForwardIterator
  117. __uninit_copy(_InputIterator __first, _InputIterator __last,
  118. _ForwardIterator __result)
  119. { return std::__do_uninit_copy(__first, __last, __result); }
  120. };
  121. template<>
  122. struct __uninitialized_copy<true>
  123. {
  124. template<typename _InputIterator, typename _ForwardIterator>
  125. static _ForwardIterator
  126. __uninit_copy(_InputIterator __first, _InputIterator __last,
  127. _ForwardIterator __result)
  128. { return std::copy(__first, __last, __result); }
  129. };
  130. /// @endcond
  131. /**
  132. * @brief Copies the range [first,last) into result.
  133. * @param __first An input iterator.
  134. * @param __last An input iterator.
  135. * @param __result An output iterator.
  136. * @return __result + (__first - __last)
  137. *
  138. * Like copy(), but does not require an initialized output range.
  139. */
  140. template<typename _InputIterator, typename _ForwardIterator>
  141. inline _ForwardIterator
  142. uninitialized_copy(_InputIterator __first, _InputIterator __last,
  143. _ForwardIterator __result)
  144. {
  145. typedef typename iterator_traits<_InputIterator>::value_type
  146. _ValueType1;
  147. typedef typename iterator_traits<_ForwardIterator>::value_type
  148. _ValueType2;
  149. // _ValueType1 must be trivially-copyable to use memmove, so don't
  150. // bother optimizing to std::copy if it isn't.
  151. // XXX Unnecessary because std::copy would check it anyway?
  152. const bool __can_memmove = __is_trivial(_ValueType1);
  153. #if __cplusplus < 201103L
  154. typedef typename iterator_traits<_InputIterator>::reference _From;
  155. #else
  156. using _From = decltype(*__first);
  157. #endif
  158. const bool __assignable
  159. = _GLIBCXX_USE_ASSIGN_FOR_INIT(_ValueType2, _From);
  160. return std::__uninitialized_copy<__can_memmove && __assignable>::
  161. __uninit_copy(__first, __last, __result);
  162. }
  163. /// @cond undocumented
  164. template<typename _ForwardIterator, typename _Tp>
  165. _GLIBCXX20_CONSTEXPR void
  166. __do_uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
  167. const _Tp& __x)
  168. {
  169. _ForwardIterator __cur = __first;
  170. __try
  171. {
  172. for (; __cur != __last; ++__cur)
  173. std::_Construct(std::__addressof(*__cur), __x);
  174. }
  175. __catch(...)
  176. {
  177. std::_Destroy(__first, __cur);
  178. __throw_exception_again;
  179. }
  180. }
  181. template<bool _TrivialValueType>
  182. struct __uninitialized_fill
  183. {
  184. template<typename _ForwardIterator, typename _Tp>
  185. static void
  186. __uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
  187. const _Tp& __x)
  188. { std::__do_uninit_fill(__first, __last, __x); }
  189. };
  190. template<>
  191. struct __uninitialized_fill<true>
  192. {
  193. template<typename _ForwardIterator, typename _Tp>
  194. static void
  195. __uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
  196. const _Tp& __x)
  197. { std::fill(__first, __last, __x); }
  198. };
  199. /// @endcond
  200. /**
  201. * @brief Copies the value x into the range [first,last).
  202. * @param __first An input iterator.
  203. * @param __last An input iterator.
  204. * @param __x The source value.
  205. * @return Nothing.
  206. *
  207. * Like fill(), but does not require an initialized output range.
  208. */
  209. template<typename _ForwardIterator, typename _Tp>
  210. inline void
  211. uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last,
  212. const _Tp& __x)
  213. {
  214. typedef typename iterator_traits<_ForwardIterator>::value_type
  215. _ValueType;
  216. // Trivial types do not need a constructor to begin their lifetime,
  217. // so try to use std::fill to benefit from its memset optimization.
  218. const bool __can_fill
  219. = _GLIBCXX_USE_ASSIGN_FOR_INIT(_ValueType, const _Tp&);
  220. std::__uninitialized_fill<__can_fill>::
  221. __uninit_fill(__first, __last, __x);
  222. }
  223. /// @cond undocumented
  224. template<typename _ForwardIterator, typename _Size, typename _Tp>
  225. _GLIBCXX20_CONSTEXPR
  226. _ForwardIterator
  227. __do_uninit_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
  228. {
  229. _ForwardIterator __cur = __first;
  230. __try
  231. {
  232. for (; __n > 0; --__n, (void) ++__cur)
  233. std::_Construct(std::__addressof(*__cur), __x);
  234. return __cur;
  235. }
  236. __catch(...)
  237. {
  238. std::_Destroy(__first, __cur);
  239. __throw_exception_again;
  240. }
  241. }
  242. template<bool _TrivialValueType>
  243. struct __uninitialized_fill_n
  244. {
  245. template<typename _ForwardIterator, typename _Size, typename _Tp>
  246. static _ForwardIterator
  247. __uninit_fill_n(_ForwardIterator __first, _Size __n,
  248. const _Tp& __x)
  249. { return std::__do_uninit_fill_n(__first, __n, __x); }
  250. };
  251. template<>
  252. struct __uninitialized_fill_n<true>
  253. {
  254. template<typename _ForwardIterator, typename _Size, typename _Tp>
  255. static _ForwardIterator
  256. __uninit_fill_n(_ForwardIterator __first, _Size __n,
  257. const _Tp& __x)
  258. { return std::fill_n(__first, __n, __x); }
  259. };
  260. /// @endcond
  261. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  262. // DR 1339. uninitialized_fill_n should return the end of its range
  263. /**
  264. * @brief Copies the value x into the range [first,first+n).
  265. * @param __first An input iterator.
  266. * @param __n The number of copies to make.
  267. * @param __x The source value.
  268. * @return Nothing.
  269. *
  270. * Like fill_n(), but does not require an initialized output range.
  271. */
  272. template<typename _ForwardIterator, typename _Size, typename _Tp>
  273. inline _ForwardIterator
  274. uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
  275. {
  276. typedef typename iterator_traits<_ForwardIterator>::value_type
  277. _ValueType;
  278. // Trivial types do not need a constructor to begin their lifetime,
  279. // so try to use std::fill_n to benefit from its optimizations.
  280. const bool __can_fill
  281. = _GLIBCXX_USE_ASSIGN_FOR_INIT(_ValueType, const _Tp&)
  282. // For arbitrary class types and floating point types we can't assume
  283. // that __n > 0 and std::__size_to_integer(__n) > 0 are equivalent,
  284. // so only use std::fill_n when _Size is already an integral type.
  285. && __is_integer<_Size>::__value;
  286. return __uninitialized_fill_n<__can_fill>::
  287. __uninit_fill_n(__first, __n, __x);
  288. }
  289. #undef _GLIBCXX_USE_ASSIGN_FOR_INIT
  290. /// @cond undocumented
  291. // Extensions: versions of uninitialized_copy, uninitialized_fill,
  292. // and uninitialized_fill_n that take an allocator parameter.
  293. // We dispatch back to the standard versions when we're given the
  294. // default allocator. For nondefault allocators we do not use
  295. // any of the POD optimizations.
  296. template<typename _InputIterator, typename _ForwardIterator,
  297. typename _Allocator>
  298. _GLIBCXX20_CONSTEXPR
  299. _ForwardIterator
  300. __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
  301. _ForwardIterator __result, _Allocator& __alloc)
  302. {
  303. _ForwardIterator __cur = __result;
  304. __try
  305. {
  306. typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
  307. for (; __first != __last; ++__first, (void)++__cur)
  308. __traits::construct(__alloc, std::__addressof(*__cur), *__first);
  309. return __cur;
  310. }
  311. __catch(...)
  312. {
  313. std::_Destroy(__result, __cur, __alloc);
  314. __throw_exception_again;
  315. }
  316. }
  317. template<typename _InputIterator, typename _ForwardIterator, typename _Tp>
  318. _GLIBCXX20_CONSTEXPR
  319. inline _ForwardIterator
  320. __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
  321. _ForwardIterator __result, allocator<_Tp>&)
  322. {
  323. #ifdef __cpp_lib_is_constant_evaluated
  324. if (std::is_constant_evaluated())
  325. return std::__do_uninit_copy(__first, __last, __result);
  326. #endif
  327. return std::uninitialized_copy(__first, __last, __result);
  328. }
  329. template<typename _InputIterator, typename _ForwardIterator,
  330. typename _Allocator>
  331. _GLIBCXX20_CONSTEXPR
  332. inline _ForwardIterator
  333. __uninitialized_move_a(_InputIterator __first, _InputIterator __last,
  334. _ForwardIterator __result, _Allocator& __alloc)
  335. {
  336. return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
  337. _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
  338. __result, __alloc);
  339. }
  340. template<typename _InputIterator, typename _ForwardIterator,
  341. typename _Allocator>
  342. _GLIBCXX20_CONSTEXPR
  343. inline _ForwardIterator
  344. __uninitialized_move_if_noexcept_a(_InputIterator __first,
  345. _InputIterator __last,
  346. _ForwardIterator __result,
  347. _Allocator& __alloc)
  348. {
  349. return std::__uninitialized_copy_a
  350. (_GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__first),
  351. _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__last), __result, __alloc);
  352. }
  353. template<typename _ForwardIterator, typename _Tp, typename _Allocator>
  354. _GLIBCXX20_CONSTEXPR
  355. void
  356. __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
  357. const _Tp& __x, _Allocator& __alloc)
  358. {
  359. _ForwardIterator __cur = __first;
  360. __try
  361. {
  362. typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
  363. for (; __cur != __last; ++__cur)
  364. __traits::construct(__alloc, std::__addressof(*__cur), __x);
  365. }
  366. __catch(...)
  367. {
  368. std::_Destroy(__first, __cur, __alloc);
  369. __throw_exception_again;
  370. }
  371. }
  372. template<typename _ForwardIterator, typename _Tp, typename _Tp2>
  373. _GLIBCXX20_CONSTEXPR
  374. inline void
  375. __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
  376. const _Tp& __x, allocator<_Tp2>&)
  377. {
  378. #ifdef __cpp_lib_is_constant_evaluated
  379. if (std::is_constant_evaluated())
  380. return std::__do_uninit_fill(__first, __last, __x);
  381. #endif
  382. std::uninitialized_fill(__first, __last, __x);
  383. }
  384. template<typename _ForwardIterator, typename _Size, typename _Tp,
  385. typename _Allocator>
  386. _GLIBCXX20_CONSTEXPR
  387. _ForwardIterator
  388. __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
  389. const _Tp& __x, _Allocator& __alloc)
  390. {
  391. _ForwardIterator __cur = __first;
  392. __try
  393. {
  394. typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
  395. for (; __n > 0; --__n, (void) ++__cur)
  396. __traits::construct(__alloc, std::__addressof(*__cur), __x);
  397. return __cur;
  398. }
  399. __catch(...)
  400. {
  401. std::_Destroy(__first, __cur, __alloc);
  402. __throw_exception_again;
  403. }
  404. }
  405. template<typename _ForwardIterator, typename _Size, typename _Tp,
  406. typename _Tp2>
  407. _GLIBCXX20_CONSTEXPR
  408. inline _ForwardIterator
  409. __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
  410. const _Tp& __x, allocator<_Tp2>&)
  411. {
  412. #ifdef __cpp_lib_is_constant_evaluated
  413. if (std::is_constant_evaluated())
  414. return std::__do_uninit_fill_n(__first, __n, __x);
  415. #endif
  416. return std::uninitialized_fill_n(__first, __n, __x);
  417. }
  418. // Extensions: __uninitialized_copy_move, __uninitialized_move_copy,
  419. // __uninitialized_fill_move, __uninitialized_move_fill.
  420. // All of these algorithms take a user-supplied allocator, which is used
  421. // for construction and destruction.
  422. // __uninitialized_copy_move
  423. // Copies [first1, last1) into [result, result + (last1 - first1)), and
  424. // move [first2, last2) into
  425. // [result, result + (last1 - first1) + (last2 - first2)).
  426. template<typename _InputIterator1, typename _InputIterator2,
  427. typename _ForwardIterator, typename _Allocator>
  428. inline _ForwardIterator
  429. __uninitialized_copy_move(_InputIterator1 __first1,
  430. _InputIterator1 __last1,
  431. _InputIterator2 __first2,
  432. _InputIterator2 __last2,
  433. _ForwardIterator __result,
  434. _Allocator& __alloc)
  435. {
  436. _ForwardIterator __mid = std::__uninitialized_copy_a(__first1, __last1,
  437. __result,
  438. __alloc);
  439. __try
  440. {
  441. return std::__uninitialized_move_a(__first2, __last2, __mid, __alloc);
  442. }
  443. __catch(...)
  444. {
  445. std::_Destroy(__result, __mid, __alloc);
  446. __throw_exception_again;
  447. }
  448. }
  449. // __uninitialized_move_copy
  450. // Moves [first1, last1) into [result, result + (last1 - first1)), and
  451. // copies [first2, last2) into
  452. // [result, result + (last1 - first1) + (last2 - first2)).
  453. template<typename _InputIterator1, typename _InputIterator2,
  454. typename _ForwardIterator, typename _Allocator>
  455. inline _ForwardIterator
  456. __uninitialized_move_copy(_InputIterator1 __first1,
  457. _InputIterator1 __last1,
  458. _InputIterator2 __first2,
  459. _InputIterator2 __last2,
  460. _ForwardIterator __result,
  461. _Allocator& __alloc)
  462. {
  463. _ForwardIterator __mid = std::__uninitialized_move_a(__first1, __last1,
  464. __result,
  465. __alloc);
  466. __try
  467. {
  468. return std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc);
  469. }
  470. __catch(...)
  471. {
  472. std::_Destroy(__result, __mid, __alloc);
  473. __throw_exception_again;
  474. }
  475. }
  476. // __uninitialized_fill_move
  477. // Fills [result, mid) with x, and moves [first, last) into
  478. // [mid, mid + (last - first)).
  479. template<typename _ForwardIterator, typename _Tp, typename _InputIterator,
  480. typename _Allocator>
  481. inline _ForwardIterator
  482. __uninitialized_fill_move(_ForwardIterator __result, _ForwardIterator __mid,
  483. const _Tp& __x, _InputIterator __first,
  484. _InputIterator __last, _Allocator& __alloc)
  485. {
  486. std::__uninitialized_fill_a(__result, __mid, __x, __alloc);
  487. __try
  488. {
  489. return std::__uninitialized_move_a(__first, __last, __mid, __alloc);
  490. }
  491. __catch(...)
  492. {
  493. std::_Destroy(__result, __mid, __alloc);
  494. __throw_exception_again;
  495. }
  496. }
  497. // __uninitialized_move_fill
  498. // Moves [first1, last1) into [first2, first2 + (last1 - first1)), and
  499. // fills [first2 + (last1 - first1), last2) with x.
  500. template<typename _InputIterator, typename _ForwardIterator, typename _Tp,
  501. typename _Allocator>
  502. inline void
  503. __uninitialized_move_fill(_InputIterator __first1, _InputIterator __last1,
  504. _ForwardIterator __first2,
  505. _ForwardIterator __last2, const _Tp& __x,
  506. _Allocator& __alloc)
  507. {
  508. _ForwardIterator __mid2 = std::__uninitialized_move_a(__first1, __last1,
  509. __first2,
  510. __alloc);
  511. __try
  512. {
  513. std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc);
  514. }
  515. __catch(...)
  516. {
  517. std::_Destroy(__first2, __mid2, __alloc);
  518. __throw_exception_again;
  519. }
  520. }
  521. /// @endcond
  522. #if __cplusplus >= 201103L
  523. /// @cond undocumented
  524. // Extensions: __uninitialized_default, __uninitialized_default_n,
  525. // __uninitialized_default_a, __uninitialized_default_n_a.
  526. template<bool _TrivialValueType>
  527. struct __uninitialized_default_1
  528. {
  529. template<typename _ForwardIterator>
  530. static void
  531. __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
  532. {
  533. _ForwardIterator __cur = __first;
  534. __try
  535. {
  536. for (; __cur != __last; ++__cur)
  537. std::_Construct(std::__addressof(*__cur));
  538. }
  539. __catch(...)
  540. {
  541. std::_Destroy(__first, __cur);
  542. __throw_exception_again;
  543. }
  544. }
  545. };
  546. template<>
  547. struct __uninitialized_default_1<true>
  548. {
  549. template<typename _ForwardIterator>
  550. static void
  551. __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
  552. {
  553. if (__first == __last)
  554. return;
  555. typename iterator_traits<_ForwardIterator>::value_type* __val
  556. = std::__addressof(*__first);
  557. std::_Construct(__val);
  558. if (++__first != __last)
  559. std::fill(__first, __last, *__val);
  560. }
  561. };
  562. template<bool _TrivialValueType>
  563. struct __uninitialized_default_n_1
  564. {
  565. template<typename _ForwardIterator, typename _Size>
  566. _GLIBCXX20_CONSTEXPR
  567. static _ForwardIterator
  568. __uninit_default_n(_ForwardIterator __first, _Size __n)
  569. {
  570. _ForwardIterator __cur = __first;
  571. __try
  572. {
  573. for (; __n > 0; --__n, (void) ++__cur)
  574. std::_Construct(std::__addressof(*__cur));
  575. return __cur;
  576. }
  577. __catch(...)
  578. {
  579. std::_Destroy(__first, __cur);
  580. __throw_exception_again;
  581. }
  582. }
  583. };
  584. template<>
  585. struct __uninitialized_default_n_1<true>
  586. {
  587. template<typename _ForwardIterator, typename _Size>
  588. _GLIBCXX20_CONSTEXPR
  589. static _ForwardIterator
  590. __uninit_default_n(_ForwardIterator __first, _Size __n)
  591. {
  592. if (__n > 0)
  593. {
  594. typename iterator_traits<_ForwardIterator>::value_type* __val
  595. = std::__addressof(*__first);
  596. std::_Construct(__val);
  597. ++__first;
  598. __first = std::fill_n(__first, __n - 1, *__val);
  599. }
  600. return __first;
  601. }
  602. };
  603. // __uninitialized_default
  604. // Fills [first, last) with value-initialized value_types.
  605. template<typename _ForwardIterator>
  606. inline void
  607. __uninitialized_default(_ForwardIterator __first,
  608. _ForwardIterator __last)
  609. {
  610. typedef typename iterator_traits<_ForwardIterator>::value_type
  611. _ValueType;
  612. // trivial types can have deleted assignment
  613. const bool __assignable = is_copy_assignable<_ValueType>::value;
  614. std::__uninitialized_default_1<__is_trivial(_ValueType)
  615. && __assignable>::
  616. __uninit_default(__first, __last);
  617. }
  618. // __uninitialized_default_n
  619. // Fills [first, first + n) with value-initialized value_types.
  620. template<typename _ForwardIterator, typename _Size>
  621. _GLIBCXX20_CONSTEXPR
  622. inline _ForwardIterator
  623. __uninitialized_default_n(_ForwardIterator __first, _Size __n)
  624. {
  625. typedef typename iterator_traits<_ForwardIterator>::value_type
  626. _ValueType;
  627. // See uninitialized_fill_n for the conditions for using std::fill_n.
  628. constexpr bool __can_fill
  629. = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
  630. return __uninitialized_default_n_1<__is_trivial(_ValueType)
  631. && __can_fill>::
  632. __uninit_default_n(__first, __n);
  633. }
  634. // __uninitialized_default_a
  635. // Fills [first, last) with value_types constructed by the allocator
  636. // alloc, with no arguments passed to the construct call.
  637. template<typename _ForwardIterator, typename _Allocator>
  638. void
  639. __uninitialized_default_a(_ForwardIterator __first,
  640. _ForwardIterator __last,
  641. _Allocator& __alloc)
  642. {
  643. _ForwardIterator __cur = __first;
  644. __try
  645. {
  646. typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
  647. for (; __cur != __last; ++__cur)
  648. __traits::construct(__alloc, std::__addressof(*__cur));
  649. }
  650. __catch(...)
  651. {
  652. std::_Destroy(__first, __cur, __alloc);
  653. __throw_exception_again;
  654. }
  655. }
  656. template<typename _ForwardIterator, typename _Tp>
  657. inline void
  658. __uninitialized_default_a(_ForwardIterator __first,
  659. _ForwardIterator __last,
  660. allocator<_Tp>&)
  661. { std::__uninitialized_default(__first, __last); }
  662. // __uninitialized_default_n_a
  663. // Fills [first, first + n) with value_types constructed by the allocator
  664. // alloc, with no arguments passed to the construct call.
  665. template<typename _ForwardIterator, typename _Size, typename _Allocator>
  666. _GLIBCXX20_CONSTEXPR _ForwardIterator
  667. __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
  668. _Allocator& __alloc)
  669. {
  670. _ForwardIterator __cur = __first;
  671. __try
  672. {
  673. typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
  674. for (; __n > 0; --__n, (void) ++__cur)
  675. __traits::construct(__alloc, std::__addressof(*__cur));
  676. return __cur;
  677. }
  678. __catch(...)
  679. {
  680. std::_Destroy(__first, __cur, __alloc);
  681. __throw_exception_again;
  682. }
  683. }
  684. // __uninitialized_default_n_a specialization for std::allocator,
  685. // which ignores the allocator and value-initializes the elements.
  686. template<typename _ForwardIterator, typename _Size, typename _Tp>
  687. _GLIBCXX20_CONSTEXPR
  688. inline _ForwardIterator
  689. __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
  690. allocator<_Tp>&)
  691. { return std::__uninitialized_default_n(__first, __n); }
  692. template<bool _TrivialValueType>
  693. struct __uninitialized_default_novalue_1
  694. {
  695. template<typename _ForwardIterator>
  696. static void
  697. __uninit_default_novalue(_ForwardIterator __first,
  698. _ForwardIterator __last)
  699. {
  700. _ForwardIterator __cur = __first;
  701. __try
  702. {
  703. for (; __cur != __last; ++__cur)
  704. std::_Construct_novalue(std::__addressof(*__cur));
  705. }
  706. __catch(...)
  707. {
  708. std::_Destroy(__first, __cur);
  709. __throw_exception_again;
  710. }
  711. }
  712. };
  713. template<>
  714. struct __uninitialized_default_novalue_1<true>
  715. {
  716. template<typename _ForwardIterator>
  717. static void
  718. __uninit_default_novalue(_ForwardIterator __first,
  719. _ForwardIterator __last)
  720. {
  721. }
  722. };
  723. template<bool _TrivialValueType>
  724. struct __uninitialized_default_novalue_n_1
  725. {
  726. template<typename _ForwardIterator, typename _Size>
  727. static _ForwardIterator
  728. __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
  729. {
  730. _ForwardIterator __cur = __first;
  731. __try
  732. {
  733. for (; __n > 0; --__n, (void) ++__cur)
  734. std::_Construct_novalue(std::__addressof(*__cur));
  735. return __cur;
  736. }
  737. __catch(...)
  738. {
  739. std::_Destroy(__first, __cur);
  740. __throw_exception_again;
  741. }
  742. }
  743. };
  744. template<>
  745. struct __uninitialized_default_novalue_n_1<true>
  746. {
  747. template<typename _ForwardIterator, typename _Size>
  748. static _ForwardIterator
  749. __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
  750. { return std::next(__first, __n); }
  751. };
  752. // __uninitialized_default_novalue
  753. // Fills [first, last) with default-initialized value_types.
  754. template<typename _ForwardIterator>
  755. inline void
  756. __uninitialized_default_novalue(_ForwardIterator __first,
  757. _ForwardIterator __last)
  758. {
  759. typedef typename iterator_traits<_ForwardIterator>::value_type
  760. _ValueType;
  761. std::__uninitialized_default_novalue_1<
  762. is_trivially_default_constructible<_ValueType>::value>::
  763. __uninit_default_novalue(__first, __last);
  764. }
  765. // __uninitialized_default_novalue_n
  766. // Fills [first, first + n) with default-initialized value_types.
  767. template<typename _ForwardIterator, typename _Size>
  768. inline _ForwardIterator
  769. __uninitialized_default_novalue_n(_ForwardIterator __first, _Size __n)
  770. {
  771. typedef typename iterator_traits<_ForwardIterator>::value_type
  772. _ValueType;
  773. return __uninitialized_default_novalue_n_1<
  774. is_trivially_default_constructible<_ValueType>::value>::
  775. __uninit_default_novalue_n(__first, __n);
  776. }
  777. template<typename _InputIterator, typename _Size,
  778. typename _ForwardIterator>
  779. _ForwardIterator
  780. __uninitialized_copy_n(_InputIterator __first, _Size __n,
  781. _ForwardIterator __result, input_iterator_tag)
  782. {
  783. _ForwardIterator __cur = __result;
  784. __try
  785. {
  786. for (; __n > 0; --__n, (void) ++__first, ++__cur)
  787. std::_Construct(std::__addressof(*__cur), *__first);
  788. return __cur;
  789. }
  790. __catch(...)
  791. {
  792. std::_Destroy(__result, __cur);
  793. __throw_exception_again;
  794. }
  795. }
  796. template<typename _RandomAccessIterator, typename _Size,
  797. typename _ForwardIterator>
  798. inline _ForwardIterator
  799. __uninitialized_copy_n(_RandomAccessIterator __first, _Size __n,
  800. _ForwardIterator __result,
  801. random_access_iterator_tag)
  802. { return std::uninitialized_copy(__first, __first + __n, __result); }
  803. template<typename _InputIterator, typename _Size,
  804. typename _ForwardIterator>
  805. pair<_InputIterator, _ForwardIterator>
  806. __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
  807. _ForwardIterator __result, input_iterator_tag)
  808. {
  809. _ForwardIterator __cur = __result;
  810. __try
  811. {
  812. for (; __n > 0; --__n, (void) ++__first, ++__cur)
  813. std::_Construct(std::__addressof(*__cur), *__first);
  814. return {__first, __cur};
  815. }
  816. __catch(...)
  817. {
  818. std::_Destroy(__result, __cur);
  819. __throw_exception_again;
  820. }
  821. }
  822. template<typename _RandomAccessIterator, typename _Size,
  823. typename _ForwardIterator>
  824. inline pair<_RandomAccessIterator, _ForwardIterator>
  825. __uninitialized_copy_n_pair(_RandomAccessIterator __first, _Size __n,
  826. _ForwardIterator __result,
  827. random_access_iterator_tag)
  828. {
  829. auto __second_res = uninitialized_copy(__first, __first + __n, __result);
  830. auto __first_res = std::next(__first, __n);
  831. return {__first_res, __second_res};
  832. }
  833. /// @endcond
  834. /**
  835. * @brief Copies the range [first,first+n) into result.
  836. * @param __first An input iterator.
  837. * @param __n The number of elements to copy.
  838. * @param __result An output iterator.
  839. * @return __result + __n
  840. * @since C++11
  841. *
  842. * Like copy_n(), but does not require an initialized output range.
  843. */
  844. template<typename _InputIterator, typename _Size, typename _ForwardIterator>
  845. inline _ForwardIterator
  846. uninitialized_copy_n(_InputIterator __first, _Size __n,
  847. _ForwardIterator __result)
  848. { return std::__uninitialized_copy_n(__first, __n, __result,
  849. std::__iterator_category(__first)); }
  850. /// @cond undocumented
  851. template<typename _InputIterator, typename _Size, typename _ForwardIterator>
  852. inline pair<_InputIterator, _ForwardIterator>
  853. __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
  854. _ForwardIterator __result)
  855. {
  856. return
  857. std::__uninitialized_copy_n_pair(__first, __n, __result,
  858. std::__iterator_category(__first));
  859. }
  860. /// @endcond
  861. #endif
  862. #if __cplusplus >= 201703L
  863. # define __cpp_lib_raw_memory_algorithms 201606L
  864. /**
  865. * @brief Default-initializes objects in the range [first,last).
  866. * @param __first A forward iterator.
  867. * @param __last A forward iterator.
  868. * @since C++17
  869. */
  870. template <typename _ForwardIterator>
  871. inline void
  872. uninitialized_default_construct(_ForwardIterator __first,
  873. _ForwardIterator __last)
  874. {
  875. __uninitialized_default_novalue(__first, __last);
  876. }
  877. /**
  878. * @brief Default-initializes objects in the range [first,first+count).
  879. * @param __first A forward iterator.
  880. * @param __count The number of objects to construct.
  881. * @return __first + __count
  882. * @since C++17
  883. */
  884. template <typename _ForwardIterator, typename _Size>
  885. inline _ForwardIterator
  886. uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
  887. {
  888. return __uninitialized_default_novalue_n(__first, __count);
  889. }
  890. /**
  891. * @brief Value-initializes objects in the range [first,last).
  892. * @param __first A forward iterator.
  893. * @param __last A forward iterator.
  894. * @since C++17
  895. */
  896. template <typename _ForwardIterator>
  897. inline void
  898. uninitialized_value_construct(_ForwardIterator __first,
  899. _ForwardIterator __last)
  900. {
  901. return __uninitialized_default(__first, __last);
  902. }
  903. /**
  904. * @brief Value-initializes objects in the range [first,first+count).
  905. * @param __first A forward iterator.
  906. * @param __count The number of objects to construct.
  907. * @return __result + __count
  908. * @since C++17
  909. */
  910. template <typename _ForwardIterator, typename _Size>
  911. inline _ForwardIterator
  912. uninitialized_value_construct_n(_ForwardIterator __first, _Size __count)
  913. {
  914. return __uninitialized_default_n(__first, __count);
  915. }
  916. /**
  917. * @brief Move-construct from the range [first,last) into result.
  918. * @param __first An input iterator.
  919. * @param __last An input iterator.
  920. * @param __result An output iterator.
  921. * @return __result + (__first - __last)
  922. * @since C++17
  923. */
  924. template <typename _InputIterator, typename _ForwardIterator>
  925. inline _ForwardIterator
  926. uninitialized_move(_InputIterator __first, _InputIterator __last,
  927. _ForwardIterator __result)
  928. {
  929. return std::uninitialized_copy
  930. (_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
  931. _GLIBCXX_MAKE_MOVE_ITERATOR(__last), __result);
  932. }
  933. /**
  934. * @brief Move-construct from the range [first,first+count) into result.
  935. * @param __first An input iterator.
  936. * @param __count The number of objects to initialize.
  937. * @param __result An output iterator.
  938. * @return __result + __count
  939. * @since C++17
  940. */
  941. template <typename _InputIterator, typename _Size, typename _ForwardIterator>
  942. inline pair<_InputIterator, _ForwardIterator>
  943. uninitialized_move_n(_InputIterator __first, _Size __count,
  944. _ForwardIterator __result)
  945. {
  946. auto __res = std::__uninitialized_copy_n_pair
  947. (_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
  948. __count, __result);
  949. return {__res.first.base(), __res.second};
  950. }
  951. #endif // C++17
  952. #if __cplusplus >= 201103L
  953. /// @cond undocumented
  954. template<typename _Tp, typename _Up, typename _Allocator>
  955. _GLIBCXX20_CONSTEXPR
  956. inline void
  957. __relocate_object_a(_Tp* __restrict __dest, _Up* __restrict __orig,
  958. _Allocator& __alloc)
  959. noexcept(noexcept(std::allocator_traits<_Allocator>::construct(__alloc,
  960. __dest, std::move(*__orig)))
  961. && noexcept(std::allocator_traits<_Allocator>::destroy(
  962. __alloc, std::__addressof(*__orig))))
  963. {
  964. typedef std::allocator_traits<_Allocator> __traits;
  965. __traits::construct(__alloc, __dest, std::move(*__orig));
  966. __traits::destroy(__alloc, std::__addressof(*__orig));
  967. }
  968. // This class may be specialized for specific types.
  969. // Also known as is_trivially_relocatable.
  970. template<typename _Tp, typename = void>
  971. struct __is_bitwise_relocatable
  972. : is_trivial<_Tp> { };
  973. template <typename _InputIterator, typename _ForwardIterator,
  974. typename _Allocator>
  975. _GLIBCXX20_CONSTEXPR
  976. inline _ForwardIterator
  977. __relocate_a_1(_InputIterator __first, _InputIterator __last,
  978. _ForwardIterator __result, _Allocator& __alloc)
  979. noexcept(noexcept(std::__relocate_object_a(std::addressof(*__result),
  980. std::addressof(*__first),
  981. __alloc)))
  982. {
  983. typedef typename iterator_traits<_InputIterator>::value_type
  984. _ValueType;
  985. typedef typename iterator_traits<_ForwardIterator>::value_type
  986. _ValueType2;
  987. static_assert(std::is_same<_ValueType, _ValueType2>::value,
  988. "relocation is only possible for values of the same type");
  989. _ForwardIterator __cur = __result;
  990. for (; __first != __last; ++__first, (void)++__cur)
  991. std::__relocate_object_a(std::__addressof(*__cur),
  992. std::__addressof(*__first), __alloc);
  993. return __cur;
  994. }
  995. template <typename _Tp, typename _Up>
  996. _GLIBCXX20_CONSTEXPR
  997. inline __enable_if_t<std::__is_bitwise_relocatable<_Tp>::value, _Tp*>
  998. __relocate_a_1(_Tp* __first, _Tp* __last,
  999. _Tp* __result,
  1000. [[__maybe_unused__]] allocator<_Up>& __alloc) noexcept
  1001. {
  1002. ptrdiff_t __count = __last - __first;
  1003. if (__count > 0)
  1004. {
  1005. #ifdef __cpp_lib_is_constant_evaluated
  1006. if (std::is_constant_evaluated())
  1007. {
  1008. // Can't use memmove. Wrap the pointer so that __relocate_a_1
  1009. // resolves to the non-trivial overload above.
  1010. __gnu_cxx::__normal_iterator<_Tp*, void> __out(__result);
  1011. __out = std::__relocate_a_1(__first, __last, __out, __alloc);
  1012. return __out.base();
  1013. }
  1014. #endif
  1015. __builtin_memmove(__result, __first, __count * sizeof(_Tp));
  1016. }
  1017. return __result + __count;
  1018. }
  1019. template <typename _InputIterator, typename _ForwardIterator,
  1020. typename _Allocator>
  1021. _GLIBCXX20_CONSTEXPR
  1022. inline _ForwardIterator
  1023. __relocate_a(_InputIterator __first, _InputIterator __last,
  1024. _ForwardIterator __result, _Allocator& __alloc)
  1025. noexcept(noexcept(__relocate_a_1(std::__niter_base(__first),
  1026. std::__niter_base(__last),
  1027. std::__niter_base(__result), __alloc)))
  1028. {
  1029. return std::__relocate_a_1(std::__niter_base(__first),
  1030. std::__niter_base(__last),
  1031. std::__niter_base(__result), __alloc);
  1032. }
  1033. /// @endcond
  1034. #endif
  1035. /// @} group memory
  1036. _GLIBCXX_END_NAMESPACE_VERSION
  1037. } // namespace
  1038. #endif /* _STL_UNINITIALIZED_H */