stl_uninitialized.h 27 KB

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