stl_uninitialized.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. // Raw memory manipulators -*- C++ -*-
  2. // Copyright (C) 2001-2021 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. template<bool _TrivialValueTypes>
  67. struct __uninitialized_copy
  68. {
  69. template<typename _InputIterator, typename _ForwardIterator>
  70. static _ForwardIterator
  71. __uninit_copy(_InputIterator __first, _InputIterator __last,
  72. _ForwardIterator __result)
  73. {
  74. _ForwardIterator __cur = __result;
  75. __try
  76. {
  77. for (; __first != __last; ++__first, (void)++__cur)
  78. std::_Construct(std::__addressof(*__cur), *__first);
  79. return __cur;
  80. }
  81. __catch(...)
  82. {
  83. std::_Destroy(__result, __cur);
  84. __throw_exception_again;
  85. }
  86. }
  87. };
  88. template<>
  89. struct __uninitialized_copy<true>
  90. {
  91. template<typename _InputIterator, typename _ForwardIterator>
  92. static _ForwardIterator
  93. __uninit_copy(_InputIterator __first, _InputIterator __last,
  94. _ForwardIterator __result)
  95. { return std::copy(__first, __last, __result); }
  96. };
  97. /// @endcond
  98. /**
  99. * @brief Copies the range [first,last) into result.
  100. * @param __first An input iterator.
  101. * @param __last An input iterator.
  102. * @param __result An output iterator.
  103. * @return __result + (__first - __last)
  104. *
  105. * Like copy(), but does not require an initialized output range.
  106. */
  107. template<typename _InputIterator, typename _ForwardIterator>
  108. inline _ForwardIterator
  109. uninitialized_copy(_InputIterator __first, _InputIterator __last,
  110. _ForwardIterator __result)
  111. {
  112. typedef typename iterator_traits<_InputIterator>::value_type
  113. _ValueType1;
  114. typedef typename iterator_traits<_ForwardIterator>::value_type
  115. _ValueType2;
  116. #if __cplusplus < 201103L
  117. const bool __assignable = true;
  118. #else
  119. // Trivial types can have deleted copy constructor, but the std::copy
  120. // optimization that uses memmove would happily "copy" them anyway.
  121. static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
  122. "result type must be constructible from value type of input range");
  123. typedef typename iterator_traits<_InputIterator>::reference _RefType1;
  124. typedef typename iterator_traits<_ForwardIterator>::reference _RefType2;
  125. // Trivial types can have deleted assignment, so using std::copy
  126. // would be ill-formed. Require assignability before using std::copy:
  127. const bool __assignable = is_assignable<_RefType2, _RefType1>::value;
  128. #endif
  129. return std::__uninitialized_copy<__is_trivial(_ValueType1)
  130. && __is_trivial(_ValueType2)
  131. && __assignable>::
  132. __uninit_copy(__first, __last, __result);
  133. }
  134. /// @cond undocumented
  135. template<bool _TrivialValueType>
  136. struct __uninitialized_fill
  137. {
  138. template<typename _ForwardIterator, typename _Tp>
  139. static void
  140. __uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
  141. const _Tp& __x)
  142. {
  143. _ForwardIterator __cur = __first;
  144. __try
  145. {
  146. for (; __cur != __last; ++__cur)
  147. std::_Construct(std::__addressof(*__cur), __x);
  148. }
  149. __catch(...)
  150. {
  151. std::_Destroy(__first, __cur);
  152. __throw_exception_again;
  153. }
  154. }
  155. };
  156. template<>
  157. struct __uninitialized_fill<true>
  158. {
  159. template<typename _ForwardIterator, typename _Tp>
  160. static void
  161. __uninit_fill(_ForwardIterator __first, _ForwardIterator __last,
  162. const _Tp& __x)
  163. { std::fill(__first, __last, __x); }
  164. };
  165. /// @endcond
  166. /**
  167. * @brief Copies the value x into the range [first,last).
  168. * @param __first An input iterator.
  169. * @param __last An input iterator.
  170. * @param __x The source value.
  171. * @return Nothing.
  172. *
  173. * Like fill(), but does not require an initialized output range.
  174. */
  175. template<typename _ForwardIterator, typename _Tp>
  176. inline void
  177. uninitialized_fill(_ForwardIterator __first, _ForwardIterator __last,
  178. const _Tp& __x)
  179. {
  180. typedef typename iterator_traits<_ForwardIterator>::value_type
  181. _ValueType;
  182. #if __cplusplus < 201103L
  183. const bool __assignable = true;
  184. #else
  185. // Trivial types can have deleted copy constructor, but the std::fill
  186. // optimization that uses memmove would happily "copy" them anyway.
  187. static_assert(is_constructible<_ValueType, const _Tp&>::value,
  188. "result type must be constructible from input type");
  189. // Trivial types can have deleted assignment, so using std::fill
  190. // would be ill-formed. Require assignability before using std::fill:
  191. const bool __assignable = is_copy_assignable<_ValueType>::value;
  192. #endif
  193. std::__uninitialized_fill<__is_trivial(_ValueType) && __assignable>::
  194. __uninit_fill(__first, __last, __x);
  195. }
  196. /// @cond undocumented
  197. template<bool _TrivialValueType>
  198. struct __uninitialized_fill_n
  199. {
  200. template<typename _ForwardIterator, typename _Size, typename _Tp>
  201. static _ForwardIterator
  202. __uninit_fill_n(_ForwardIterator __first, _Size __n,
  203. const _Tp& __x)
  204. {
  205. _ForwardIterator __cur = __first;
  206. __try
  207. {
  208. for (; __n > 0; --__n, (void) ++__cur)
  209. std::_Construct(std::__addressof(*__cur), __x);
  210. return __cur;
  211. }
  212. __catch(...)
  213. {
  214. std::_Destroy(__first, __cur);
  215. __throw_exception_again;
  216. }
  217. }
  218. };
  219. template<>
  220. struct __uninitialized_fill_n<true>
  221. {
  222. template<typename _ForwardIterator, typename _Size, typename _Tp>
  223. static _ForwardIterator
  224. __uninit_fill_n(_ForwardIterator __first, _Size __n,
  225. const _Tp& __x)
  226. { return std::fill_n(__first, __n, __x); }
  227. };
  228. /// @endcond
  229. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  230. // DR 1339. uninitialized_fill_n should return the end of its range
  231. /**
  232. * @brief Copies the value x into the range [first,first+n).
  233. * @param __first An input iterator.
  234. * @param __n The number of copies to make.
  235. * @param __x The source value.
  236. * @return Nothing.
  237. *
  238. * Like fill_n(), but does not require an initialized output range.
  239. */
  240. template<typename _ForwardIterator, typename _Size, typename _Tp>
  241. inline _ForwardIterator
  242. uninitialized_fill_n(_ForwardIterator __first, _Size __n, const _Tp& __x)
  243. {
  244. typedef typename iterator_traits<_ForwardIterator>::value_type
  245. _ValueType;
  246. // Trivial types do not need a constructor to begin their lifetime,
  247. // so try to use std::fill_n to benefit from its memmove optimization.
  248. // For arbitrary class types and floating point types we can't assume
  249. // that __n > 0 and std::__size_to_integer(__n) > 0 are equivalent,
  250. // so only use std::fill_n when _Size is already an integral type.
  251. #if __cplusplus < 201103L
  252. const bool __can_fill = __is_integer<_Size>::__value;
  253. #else
  254. // Trivial types can have deleted copy constructor, but the std::fill_n
  255. // optimization that uses memmove would happily "copy" them anyway.
  256. static_assert(is_constructible<_ValueType, const _Tp&>::value,
  257. "result type must be constructible from input type");
  258. // Trivial types can have deleted assignment, so using std::fill_n
  259. // would be ill-formed. Require assignability before using std::fill_n:
  260. constexpr bool __can_fill
  261. = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
  262. #endif
  263. return __uninitialized_fill_n<__is_trivial(_ValueType) && __can_fill>::
  264. __uninit_fill_n(__first, __n, __x);
  265. }
  266. /// @cond undocumented
  267. // Extensions: versions of uninitialized_copy, uninitialized_fill,
  268. // and uninitialized_fill_n that take an allocator parameter.
  269. // We dispatch back to the standard versions when we're given the
  270. // default allocator. For nondefault allocators we do not use
  271. // any of the POD optimizations.
  272. template<typename _InputIterator, typename _ForwardIterator,
  273. typename _Allocator>
  274. _ForwardIterator
  275. __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
  276. _ForwardIterator __result, _Allocator& __alloc)
  277. {
  278. _ForwardIterator __cur = __result;
  279. __try
  280. {
  281. typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
  282. for (; __first != __last; ++__first, (void)++__cur)
  283. __traits::construct(__alloc, std::__addressof(*__cur), *__first);
  284. return __cur;
  285. }
  286. __catch(...)
  287. {
  288. std::_Destroy(__result, __cur, __alloc);
  289. __throw_exception_again;
  290. }
  291. }
  292. template<typename _InputIterator, typename _ForwardIterator, typename _Tp>
  293. inline _ForwardIterator
  294. __uninitialized_copy_a(_InputIterator __first, _InputIterator __last,
  295. _ForwardIterator __result, allocator<_Tp>&)
  296. { return std::uninitialized_copy(__first, __last, __result); }
  297. template<typename _InputIterator, typename _ForwardIterator,
  298. typename _Allocator>
  299. inline _ForwardIterator
  300. __uninitialized_move_a(_InputIterator __first, _InputIterator __last,
  301. _ForwardIterator __result, _Allocator& __alloc)
  302. {
  303. return std::__uninitialized_copy_a(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
  304. _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
  305. __result, __alloc);
  306. }
  307. template<typename _InputIterator, typename _ForwardIterator,
  308. typename _Allocator>
  309. inline _ForwardIterator
  310. __uninitialized_move_if_noexcept_a(_InputIterator __first,
  311. _InputIterator __last,
  312. _ForwardIterator __result,
  313. _Allocator& __alloc)
  314. {
  315. return std::__uninitialized_copy_a
  316. (_GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__first),
  317. _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(__last), __result, __alloc);
  318. }
  319. template<typename _ForwardIterator, typename _Tp, typename _Allocator>
  320. void
  321. __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
  322. const _Tp& __x, _Allocator& __alloc)
  323. {
  324. _ForwardIterator __cur = __first;
  325. __try
  326. {
  327. typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
  328. for (; __cur != __last; ++__cur)
  329. __traits::construct(__alloc, std::__addressof(*__cur), __x);
  330. }
  331. __catch(...)
  332. {
  333. std::_Destroy(__first, __cur, __alloc);
  334. __throw_exception_again;
  335. }
  336. }
  337. template<typename _ForwardIterator, typename _Tp, typename _Tp2>
  338. inline void
  339. __uninitialized_fill_a(_ForwardIterator __first, _ForwardIterator __last,
  340. const _Tp& __x, allocator<_Tp2>&)
  341. { std::uninitialized_fill(__first, __last, __x); }
  342. template<typename _ForwardIterator, typename _Size, typename _Tp,
  343. typename _Allocator>
  344. _ForwardIterator
  345. __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
  346. const _Tp& __x, _Allocator& __alloc)
  347. {
  348. _ForwardIterator __cur = __first;
  349. __try
  350. {
  351. typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
  352. for (; __n > 0; --__n, (void) ++__cur)
  353. __traits::construct(__alloc, std::__addressof(*__cur), __x);
  354. return __cur;
  355. }
  356. __catch(...)
  357. {
  358. std::_Destroy(__first, __cur, __alloc);
  359. __throw_exception_again;
  360. }
  361. }
  362. template<typename _ForwardIterator, typename _Size, typename _Tp,
  363. typename _Tp2>
  364. inline _ForwardIterator
  365. __uninitialized_fill_n_a(_ForwardIterator __first, _Size __n,
  366. const _Tp& __x, allocator<_Tp2>&)
  367. { return std::uninitialized_fill_n(__first, __n, __x); }
  368. // Extensions: __uninitialized_copy_move, __uninitialized_move_copy,
  369. // __uninitialized_fill_move, __uninitialized_move_fill.
  370. // All of these algorithms take a user-supplied allocator, which is used
  371. // for construction and destruction.
  372. // __uninitialized_copy_move
  373. // Copies [first1, last1) into [result, result + (last1 - first1)), and
  374. // move [first2, last2) into
  375. // [result, result + (last1 - first1) + (last2 - first2)).
  376. template<typename _InputIterator1, typename _InputIterator2,
  377. typename _ForwardIterator, typename _Allocator>
  378. inline _ForwardIterator
  379. __uninitialized_copy_move(_InputIterator1 __first1,
  380. _InputIterator1 __last1,
  381. _InputIterator2 __first2,
  382. _InputIterator2 __last2,
  383. _ForwardIterator __result,
  384. _Allocator& __alloc)
  385. {
  386. _ForwardIterator __mid = std::__uninitialized_copy_a(__first1, __last1,
  387. __result,
  388. __alloc);
  389. __try
  390. {
  391. return std::__uninitialized_move_a(__first2, __last2, __mid, __alloc);
  392. }
  393. __catch(...)
  394. {
  395. std::_Destroy(__result, __mid, __alloc);
  396. __throw_exception_again;
  397. }
  398. }
  399. // __uninitialized_move_copy
  400. // Moves [first1, last1) into [result, result + (last1 - first1)), and
  401. // copies [first2, last2) into
  402. // [result, result + (last1 - first1) + (last2 - first2)).
  403. template<typename _InputIterator1, typename _InputIterator2,
  404. typename _ForwardIterator, typename _Allocator>
  405. inline _ForwardIterator
  406. __uninitialized_move_copy(_InputIterator1 __first1,
  407. _InputIterator1 __last1,
  408. _InputIterator2 __first2,
  409. _InputIterator2 __last2,
  410. _ForwardIterator __result,
  411. _Allocator& __alloc)
  412. {
  413. _ForwardIterator __mid = std::__uninitialized_move_a(__first1, __last1,
  414. __result,
  415. __alloc);
  416. __try
  417. {
  418. return std::__uninitialized_copy_a(__first2, __last2, __mid, __alloc);
  419. }
  420. __catch(...)
  421. {
  422. std::_Destroy(__result, __mid, __alloc);
  423. __throw_exception_again;
  424. }
  425. }
  426. // __uninitialized_fill_move
  427. // Fills [result, mid) with x, and moves [first, last) into
  428. // [mid, mid + (last - first)).
  429. template<typename _ForwardIterator, typename _Tp, typename _InputIterator,
  430. typename _Allocator>
  431. inline _ForwardIterator
  432. __uninitialized_fill_move(_ForwardIterator __result, _ForwardIterator __mid,
  433. const _Tp& __x, _InputIterator __first,
  434. _InputIterator __last, _Allocator& __alloc)
  435. {
  436. std::__uninitialized_fill_a(__result, __mid, __x, __alloc);
  437. __try
  438. {
  439. return std::__uninitialized_move_a(__first, __last, __mid, __alloc);
  440. }
  441. __catch(...)
  442. {
  443. std::_Destroy(__result, __mid, __alloc);
  444. __throw_exception_again;
  445. }
  446. }
  447. // __uninitialized_move_fill
  448. // Moves [first1, last1) into [first2, first2 + (last1 - first1)), and
  449. // fills [first2 + (last1 - first1), last2) with x.
  450. template<typename _InputIterator, typename _ForwardIterator, typename _Tp,
  451. typename _Allocator>
  452. inline void
  453. __uninitialized_move_fill(_InputIterator __first1, _InputIterator __last1,
  454. _ForwardIterator __first2,
  455. _ForwardIterator __last2, const _Tp& __x,
  456. _Allocator& __alloc)
  457. {
  458. _ForwardIterator __mid2 = std::__uninitialized_move_a(__first1, __last1,
  459. __first2,
  460. __alloc);
  461. __try
  462. {
  463. std::__uninitialized_fill_a(__mid2, __last2, __x, __alloc);
  464. }
  465. __catch(...)
  466. {
  467. std::_Destroy(__first2, __mid2, __alloc);
  468. __throw_exception_again;
  469. }
  470. }
  471. /// @endcond
  472. #if __cplusplus >= 201103L
  473. /// @cond undocumented
  474. // Extensions: __uninitialized_default, __uninitialized_default_n,
  475. // __uninitialized_default_a, __uninitialized_default_n_a.
  476. template<bool _TrivialValueType>
  477. struct __uninitialized_default_1
  478. {
  479. template<typename _ForwardIterator>
  480. static void
  481. __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
  482. {
  483. _ForwardIterator __cur = __first;
  484. __try
  485. {
  486. for (; __cur != __last; ++__cur)
  487. std::_Construct(std::__addressof(*__cur));
  488. }
  489. __catch(...)
  490. {
  491. std::_Destroy(__first, __cur);
  492. __throw_exception_again;
  493. }
  494. }
  495. };
  496. template<>
  497. struct __uninitialized_default_1<true>
  498. {
  499. template<typename _ForwardIterator>
  500. static void
  501. __uninit_default(_ForwardIterator __first, _ForwardIterator __last)
  502. {
  503. if (__first == __last)
  504. return;
  505. typename iterator_traits<_ForwardIterator>::value_type* __val
  506. = std::__addressof(*__first);
  507. std::_Construct(__val);
  508. if (++__first != __last)
  509. std::fill(__first, __last, *__val);
  510. }
  511. };
  512. template<bool _TrivialValueType>
  513. struct __uninitialized_default_n_1
  514. {
  515. template<typename _ForwardIterator, typename _Size>
  516. static _ForwardIterator
  517. __uninit_default_n(_ForwardIterator __first, _Size __n)
  518. {
  519. _ForwardIterator __cur = __first;
  520. __try
  521. {
  522. for (; __n > 0; --__n, (void) ++__cur)
  523. std::_Construct(std::__addressof(*__cur));
  524. return __cur;
  525. }
  526. __catch(...)
  527. {
  528. std::_Destroy(__first, __cur);
  529. __throw_exception_again;
  530. }
  531. }
  532. };
  533. template<>
  534. struct __uninitialized_default_n_1<true>
  535. {
  536. template<typename _ForwardIterator, typename _Size>
  537. static _ForwardIterator
  538. __uninit_default_n(_ForwardIterator __first, _Size __n)
  539. {
  540. if (__n > 0)
  541. {
  542. typename iterator_traits<_ForwardIterator>::value_type* __val
  543. = std::__addressof(*__first);
  544. std::_Construct(__val);
  545. ++__first;
  546. __first = std::fill_n(__first, __n - 1, *__val);
  547. }
  548. return __first;
  549. }
  550. };
  551. // __uninitialized_default
  552. // Fills [first, last) with value-initialized value_types.
  553. template<typename _ForwardIterator>
  554. inline void
  555. __uninitialized_default(_ForwardIterator __first,
  556. _ForwardIterator __last)
  557. {
  558. typedef typename iterator_traits<_ForwardIterator>::value_type
  559. _ValueType;
  560. // trivial types can have deleted assignment
  561. const bool __assignable = is_copy_assignable<_ValueType>::value;
  562. std::__uninitialized_default_1<__is_trivial(_ValueType)
  563. && __assignable>::
  564. __uninit_default(__first, __last);
  565. }
  566. // __uninitialized_default_n
  567. // Fills [first, first + n) with value-initialized value_types.
  568. template<typename _ForwardIterator, typename _Size>
  569. inline _ForwardIterator
  570. __uninitialized_default_n(_ForwardIterator __first, _Size __n)
  571. {
  572. typedef typename iterator_traits<_ForwardIterator>::value_type
  573. _ValueType;
  574. // See uninitialized_fill_n for the conditions for using std::fill_n.
  575. constexpr bool __can_fill
  576. = __and_<is_integral<_Size>, is_copy_assignable<_ValueType>>::value;
  577. return __uninitialized_default_n_1<__is_trivial(_ValueType)
  578. && __can_fill>::
  579. __uninit_default_n(__first, __n);
  580. }
  581. // __uninitialized_default_a
  582. // Fills [first, last) with value_types constructed by the allocator
  583. // alloc, with no arguments passed to the construct call.
  584. template<typename _ForwardIterator, typename _Allocator>
  585. void
  586. __uninitialized_default_a(_ForwardIterator __first,
  587. _ForwardIterator __last,
  588. _Allocator& __alloc)
  589. {
  590. _ForwardIterator __cur = __first;
  591. __try
  592. {
  593. typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
  594. for (; __cur != __last; ++__cur)
  595. __traits::construct(__alloc, std::__addressof(*__cur));
  596. }
  597. __catch(...)
  598. {
  599. std::_Destroy(__first, __cur, __alloc);
  600. __throw_exception_again;
  601. }
  602. }
  603. template<typename _ForwardIterator, typename _Tp>
  604. inline void
  605. __uninitialized_default_a(_ForwardIterator __first,
  606. _ForwardIterator __last,
  607. allocator<_Tp>&)
  608. { std::__uninitialized_default(__first, __last); }
  609. // __uninitialized_default_n_a
  610. // Fills [first, first + n) with value_types constructed by the allocator
  611. // alloc, with no arguments passed to the construct call.
  612. template<typename _ForwardIterator, typename _Size, typename _Allocator>
  613. _ForwardIterator
  614. __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
  615. _Allocator& __alloc)
  616. {
  617. _ForwardIterator __cur = __first;
  618. __try
  619. {
  620. typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
  621. for (; __n > 0; --__n, (void) ++__cur)
  622. __traits::construct(__alloc, std::__addressof(*__cur));
  623. return __cur;
  624. }
  625. __catch(...)
  626. {
  627. std::_Destroy(__first, __cur, __alloc);
  628. __throw_exception_again;
  629. }
  630. }
  631. // __uninitialized_default_n_a specialization for std::allocator,
  632. // which ignores the allocator and value-initializes the elements.
  633. template<typename _ForwardIterator, typename _Size, typename _Tp>
  634. inline _ForwardIterator
  635. __uninitialized_default_n_a(_ForwardIterator __first, _Size __n,
  636. allocator<_Tp>&)
  637. { return std::__uninitialized_default_n(__first, __n); }
  638. template<bool _TrivialValueType>
  639. struct __uninitialized_default_novalue_1
  640. {
  641. template<typename _ForwardIterator>
  642. static void
  643. __uninit_default_novalue(_ForwardIterator __first,
  644. _ForwardIterator __last)
  645. {
  646. _ForwardIterator __cur = __first;
  647. __try
  648. {
  649. for (; __cur != __last; ++__cur)
  650. std::_Construct_novalue(std::__addressof(*__cur));
  651. }
  652. __catch(...)
  653. {
  654. std::_Destroy(__first, __cur);
  655. __throw_exception_again;
  656. }
  657. }
  658. };
  659. template<>
  660. struct __uninitialized_default_novalue_1<true>
  661. {
  662. template<typename _ForwardIterator>
  663. static void
  664. __uninit_default_novalue(_ForwardIterator __first,
  665. _ForwardIterator __last)
  666. {
  667. }
  668. };
  669. template<bool _TrivialValueType>
  670. struct __uninitialized_default_novalue_n_1
  671. {
  672. template<typename _ForwardIterator, typename _Size>
  673. static _ForwardIterator
  674. __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
  675. {
  676. _ForwardIterator __cur = __first;
  677. __try
  678. {
  679. for (; __n > 0; --__n, (void) ++__cur)
  680. std::_Construct_novalue(std::__addressof(*__cur));
  681. return __cur;
  682. }
  683. __catch(...)
  684. {
  685. std::_Destroy(__first, __cur);
  686. __throw_exception_again;
  687. }
  688. }
  689. };
  690. template<>
  691. struct __uninitialized_default_novalue_n_1<true>
  692. {
  693. template<typename _ForwardIterator, typename _Size>
  694. static _ForwardIterator
  695. __uninit_default_novalue_n(_ForwardIterator __first, _Size __n)
  696. { return std::next(__first, __n); }
  697. };
  698. // __uninitialized_default_novalue
  699. // Fills [first, last) with default-initialized value_types.
  700. template<typename _ForwardIterator>
  701. inline void
  702. __uninitialized_default_novalue(_ForwardIterator __first,
  703. _ForwardIterator __last)
  704. {
  705. typedef typename iterator_traits<_ForwardIterator>::value_type
  706. _ValueType;
  707. std::__uninitialized_default_novalue_1<
  708. is_trivially_default_constructible<_ValueType>::value>::
  709. __uninit_default_novalue(__first, __last);
  710. }
  711. // __uninitialized_default_novalue_n
  712. // Fills [first, first + n) with default-initialized value_types.
  713. template<typename _ForwardIterator, typename _Size>
  714. inline _ForwardIterator
  715. __uninitialized_default_novalue_n(_ForwardIterator __first, _Size __n)
  716. {
  717. typedef typename iterator_traits<_ForwardIterator>::value_type
  718. _ValueType;
  719. return __uninitialized_default_novalue_n_1<
  720. is_trivially_default_constructible<_ValueType>::value>::
  721. __uninit_default_novalue_n(__first, __n);
  722. }
  723. template<typename _InputIterator, typename _Size,
  724. typename _ForwardIterator>
  725. _ForwardIterator
  726. __uninitialized_copy_n(_InputIterator __first, _Size __n,
  727. _ForwardIterator __result, input_iterator_tag)
  728. {
  729. _ForwardIterator __cur = __result;
  730. __try
  731. {
  732. for (; __n > 0; --__n, (void) ++__first, ++__cur)
  733. std::_Construct(std::__addressof(*__cur), *__first);
  734. return __cur;
  735. }
  736. __catch(...)
  737. {
  738. std::_Destroy(__result, __cur);
  739. __throw_exception_again;
  740. }
  741. }
  742. template<typename _RandomAccessIterator, typename _Size,
  743. typename _ForwardIterator>
  744. inline _ForwardIterator
  745. __uninitialized_copy_n(_RandomAccessIterator __first, _Size __n,
  746. _ForwardIterator __result,
  747. random_access_iterator_tag)
  748. { return std::uninitialized_copy(__first, __first + __n, __result); }
  749. template<typename _InputIterator, typename _Size,
  750. typename _ForwardIterator>
  751. pair<_InputIterator, _ForwardIterator>
  752. __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
  753. _ForwardIterator __result, input_iterator_tag)
  754. {
  755. _ForwardIterator __cur = __result;
  756. __try
  757. {
  758. for (; __n > 0; --__n, (void) ++__first, ++__cur)
  759. std::_Construct(std::__addressof(*__cur), *__first);
  760. return {__first, __cur};
  761. }
  762. __catch(...)
  763. {
  764. std::_Destroy(__result, __cur);
  765. __throw_exception_again;
  766. }
  767. }
  768. template<typename _RandomAccessIterator, typename _Size,
  769. typename _ForwardIterator>
  770. inline pair<_RandomAccessIterator, _ForwardIterator>
  771. __uninitialized_copy_n_pair(_RandomAccessIterator __first, _Size __n,
  772. _ForwardIterator __result,
  773. random_access_iterator_tag)
  774. {
  775. auto __second_res = uninitialized_copy(__first, __first + __n, __result);
  776. auto __first_res = std::next(__first, __n);
  777. return {__first_res, __second_res};
  778. }
  779. /// @endcond
  780. /**
  781. * @brief Copies the range [first,first+n) into result.
  782. * @param __first An input iterator.
  783. * @param __n The number of elements to copy.
  784. * @param __result An output iterator.
  785. * @return __result + __n
  786. *
  787. * Like copy_n(), but does not require an initialized output range.
  788. */
  789. template<typename _InputIterator, typename _Size, typename _ForwardIterator>
  790. inline _ForwardIterator
  791. uninitialized_copy_n(_InputIterator __first, _Size __n,
  792. _ForwardIterator __result)
  793. { return std::__uninitialized_copy_n(__first, __n, __result,
  794. std::__iterator_category(__first)); }
  795. /// @cond undocumented
  796. template<typename _InputIterator, typename _Size, typename _ForwardIterator>
  797. inline pair<_InputIterator, _ForwardIterator>
  798. __uninitialized_copy_n_pair(_InputIterator __first, _Size __n,
  799. _ForwardIterator __result)
  800. {
  801. return
  802. std::__uninitialized_copy_n_pair(__first, __n, __result,
  803. std::__iterator_category(__first));
  804. }
  805. /// @endcond
  806. #endif
  807. #if __cplusplus >= 201703L
  808. # define __cpp_lib_raw_memory_algorithms 201606L
  809. /**
  810. * @brief Default-initializes objects in the range [first,last).
  811. * @param __first A forward iterator.
  812. * @param __last A forward iterator.
  813. */
  814. template <typename _ForwardIterator>
  815. inline void
  816. uninitialized_default_construct(_ForwardIterator __first,
  817. _ForwardIterator __last)
  818. {
  819. __uninitialized_default_novalue(__first, __last);
  820. }
  821. /**
  822. * @brief Default-initializes objects in the range [first,first+count).
  823. * @param __first A forward iterator.
  824. * @param __count The number of objects to construct.
  825. * @return __first + __count
  826. */
  827. template <typename _ForwardIterator, typename _Size>
  828. inline _ForwardIterator
  829. uninitialized_default_construct_n(_ForwardIterator __first, _Size __count)
  830. {
  831. return __uninitialized_default_novalue_n(__first, __count);
  832. }
  833. /**
  834. * @brief Value-initializes objects in the range [first,last).
  835. * @param __first A forward iterator.
  836. * @param __last A forward iterator.
  837. */
  838. template <typename _ForwardIterator>
  839. inline void
  840. uninitialized_value_construct(_ForwardIterator __first,
  841. _ForwardIterator __last)
  842. {
  843. return __uninitialized_default(__first, __last);
  844. }
  845. /**
  846. * @brief Value-initializes objects in the range [first,first+count).
  847. * @param __first A forward iterator.
  848. * @param __count The number of objects to construct.
  849. * @return __result + __count
  850. */
  851. template <typename _ForwardIterator, typename _Size>
  852. inline _ForwardIterator
  853. uninitialized_value_construct_n(_ForwardIterator __first, _Size __count)
  854. {
  855. return __uninitialized_default_n(__first, __count);
  856. }
  857. /**
  858. * @brief Move-construct from the range [first,last) into result.
  859. * @param __first An input iterator.
  860. * @param __last An input iterator.
  861. * @param __result An output iterator.
  862. * @return __result + (__first - __last)
  863. */
  864. template <typename _InputIterator, typename _ForwardIterator>
  865. inline _ForwardIterator
  866. uninitialized_move(_InputIterator __first, _InputIterator __last,
  867. _ForwardIterator __result)
  868. {
  869. return std::uninitialized_copy
  870. (_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
  871. _GLIBCXX_MAKE_MOVE_ITERATOR(__last), __result);
  872. }
  873. /**
  874. * @brief Move-construct from the range [first,first+count) into result.
  875. * @param __first An input iterator.
  876. * @param __count The number of objects to initialize.
  877. * @param __result An output iterator.
  878. * @return __result + __count
  879. */
  880. template <typename _InputIterator, typename _Size, typename _ForwardIterator>
  881. inline pair<_InputIterator, _ForwardIterator>
  882. uninitialized_move_n(_InputIterator __first, _Size __count,
  883. _ForwardIterator __result)
  884. {
  885. auto __res = std::__uninitialized_copy_n_pair
  886. (_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
  887. __count, __result);
  888. return {__res.first.base(), __res.second};
  889. }
  890. #endif // C++17
  891. #if __cplusplus >= 201103L
  892. /// @cond undocumented
  893. template<typename _Tp, typename _Up, typename _Allocator>
  894. inline void
  895. __relocate_object_a(_Tp* __restrict __dest, _Up* __restrict __orig,
  896. _Allocator& __alloc)
  897. noexcept(noexcept(std::allocator_traits<_Allocator>::construct(__alloc,
  898. __dest, std::move(*__orig)))
  899. && noexcept(std::allocator_traits<_Allocator>::destroy(
  900. __alloc, std::__addressof(*__orig))))
  901. {
  902. typedef std::allocator_traits<_Allocator> __traits;
  903. __traits::construct(__alloc, __dest, std::move(*__orig));
  904. __traits::destroy(__alloc, std::__addressof(*__orig));
  905. }
  906. // This class may be specialized for specific types.
  907. // Also known as is_trivially_relocatable.
  908. template<typename _Tp, typename = void>
  909. struct __is_bitwise_relocatable
  910. : is_trivial<_Tp> { };
  911. template <typename _Tp, typename _Up>
  912. inline __enable_if_t<std::__is_bitwise_relocatable<_Tp>::value, _Tp*>
  913. __relocate_a_1(_Tp* __first, _Tp* __last,
  914. _Tp* __result, allocator<_Up>&) noexcept
  915. {
  916. ptrdiff_t __count = __last - __first;
  917. if (__count > 0)
  918. __builtin_memmove(__result, __first, __count * sizeof(_Tp));
  919. return __result + __count;
  920. }
  921. template <typename _InputIterator, typename _ForwardIterator,
  922. typename _Allocator>
  923. inline _ForwardIterator
  924. __relocate_a_1(_InputIterator __first, _InputIterator __last,
  925. _ForwardIterator __result, _Allocator& __alloc)
  926. noexcept(noexcept(std::__relocate_object_a(std::addressof(*__result),
  927. std::addressof(*__first),
  928. __alloc)))
  929. {
  930. typedef typename iterator_traits<_InputIterator>::value_type
  931. _ValueType;
  932. typedef typename iterator_traits<_ForwardIterator>::value_type
  933. _ValueType2;
  934. static_assert(std::is_same<_ValueType, _ValueType2>::value,
  935. "relocation is only possible for values of the same type");
  936. _ForwardIterator __cur = __result;
  937. for (; __first != __last; ++__first, (void)++__cur)
  938. std::__relocate_object_a(std::__addressof(*__cur),
  939. std::__addressof(*__first), __alloc);
  940. return __cur;
  941. }
  942. template <typename _InputIterator, typename _ForwardIterator,
  943. typename _Allocator>
  944. inline _ForwardIterator
  945. __relocate_a(_InputIterator __first, _InputIterator __last,
  946. _ForwardIterator __result, _Allocator& __alloc)
  947. noexcept(noexcept(__relocate_a_1(std::__niter_base(__first),
  948. std::__niter_base(__last),
  949. std::__niter_base(__result), __alloc)))
  950. {
  951. return __relocate_a_1(std::__niter_base(__first),
  952. std::__niter_base(__last),
  953. std::__niter_base(__result), __alloc);
  954. }
  955. /// @endcond
  956. #endif
  957. /// @} group memory
  958. _GLIBCXX_END_NAMESPACE_VERSION
  959. } // namespace
  960. #endif /* _STL_UNINITIALIZED_H */