選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

stl_uninitialized.h 32KB

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