stl_uninitialized.h 31 KB

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