array 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_ARRAY
  10. #define _LIBCPP_ARRAY
  11. /*
  12. array synopsis
  13. namespace std
  14. {
  15. template <class T, size_t N >
  16. struct array
  17. {
  18. // types:
  19. typedef T & reference;
  20. typedef const T & const_reference;
  21. typedef implementation defined iterator;
  22. typedef implementation defined const_iterator;
  23. typedef size_t size_type;
  24. typedef ptrdiff_t difference_type;
  25. typedef T value_type;
  26. typedef T* pointer;
  27. typedef const T* const_pointer;
  28. typedef std::reverse_iterator<iterator> reverse_iterator;
  29. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  30. // No explicit construct/copy/destroy for aggregate type
  31. void fill(const T& u); // constexpr in C++20
  32. void swap(array& a) noexcept(is_nothrow_swappable_v<T>); // constexpr in C++20
  33. // iterators:
  34. iterator begin() noexcept; // constexpr in C++17
  35. const_iterator begin() const noexcept; // constexpr in C++17
  36. iterator end() noexcept; // constexpr in C++17
  37. const_iterator end() const noexcept; // constexpr in C++17
  38. reverse_iterator rbegin() noexcept; // constexpr in C++17
  39. const_reverse_iterator rbegin() const noexcept; // constexpr in C++17
  40. reverse_iterator rend() noexcept; // constexpr in C++17
  41. const_reverse_iterator rend() const noexcept; // constexpr in C++17
  42. const_iterator cbegin() const noexcept; // constexpr in C++17
  43. const_iterator cend() const noexcept; // constexpr in C++17
  44. const_reverse_iterator crbegin() const noexcept; // constexpr in C++17
  45. const_reverse_iterator crend() const noexcept; // constexpr in C++17
  46. // capacity:
  47. constexpr size_type size() const noexcept;
  48. constexpr size_type max_size() const noexcept;
  49. constexpr bool empty() const noexcept;
  50. // element access:
  51. reference operator[](size_type n); // constexpr in C++17
  52. const_reference operator[](size_type n) const; // constexpr in C++14
  53. reference at(size_type n); // constexpr in C++17
  54. const_reference at(size_type n) const; // constexpr in C++14
  55. reference front(); // constexpr in C++17
  56. const_reference front() const; // constexpr in C++14
  57. reference back(); // constexpr in C++17
  58. const_reference back() const; // constexpr in C++14
  59. T* data() noexcept; // constexpr in C++17
  60. const T* data() const noexcept; // constexpr in C++17
  61. };
  62. template <class T, class... U>
  63. array(T, U...) -> array<T, 1 + sizeof...(U)>; // C++17
  64. template <class T, size_t N>
  65. bool operator==(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
  66. template <class T, size_t N>
  67. bool operator!=(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
  68. template <class T, size_t N>
  69. bool operator<(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
  70. template <class T, size_t N>
  71. bool operator>(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
  72. template <class T, size_t N>
  73. bool operator<=(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
  74. template <class T, size_t N>
  75. bool operator>=(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
  76. template <class T, size_t N >
  77. void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20
  78. template <class T, size_t N>
  79. constexpr array<remove_cv_t<T>, N> to_array(T (&a)[N]); // C++20
  80. template <class T, size_t N>
  81. constexpr array<remove_cv_t<T>, N> to_array(T (&&a)[N]); // C++20
  82. template <class T> struct tuple_size;
  83. template <size_t I, class T> struct tuple_element;
  84. template <class T, size_t N> struct tuple_size<array<T, N>>;
  85. template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>;
  86. template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
  87. template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
  88. template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
  89. template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14
  90. } // std
  91. */
  92. #include <__algorithm/equal.h>
  93. #include <__algorithm/fill_n.h>
  94. #include <__algorithm/lexicographical_compare.h>
  95. #include <__algorithm/swap_ranges.h>
  96. #include <__assert>
  97. #include <__config>
  98. #include <__tuple>
  99. #include <__utility/unreachable.h>
  100. #include <iterator>
  101. #include <stdexcept>
  102. #include <type_traits>
  103. #include <utility>
  104. #include <version>
  105. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  106. # pragma GCC system_header
  107. #endif
  108. _LIBCPP_BEGIN_NAMESPACE_STD
  109. template <class _Tp, size_t _Size>
  110. struct _LIBCPP_TEMPLATE_VIS array
  111. {
  112. // types:
  113. typedef array __self;
  114. typedef _Tp value_type;
  115. typedef value_type& reference;
  116. typedef const value_type& const_reference;
  117. typedef value_type* iterator;
  118. typedef const value_type* const_iterator;
  119. typedef value_type* pointer;
  120. typedef const value_type* const_pointer;
  121. typedef size_t size_type;
  122. typedef ptrdiff_t difference_type;
  123. typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
  124. typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
  125. _Tp __elems_[_Size];
  126. // No explicit construct/copy/destroy for aggregate type
  127. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  128. void fill(const value_type& __u) {
  129. _VSTD::fill_n(data(), _Size, __u);
  130. }
  131. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  132. void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
  133. _VSTD::swap_ranges(data(), data() + _Size, __a.data());
  134. }
  135. // iterators:
  136. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  137. iterator begin() _NOEXCEPT {return iterator(data());}
  138. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  139. const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
  140. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  141. iterator end() _NOEXCEPT {return iterator(data() + _Size);}
  142. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  143. const_iterator end() const _NOEXCEPT {return const_iterator(data() + _Size);}
  144. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  145. reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
  146. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  147. const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
  148. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  149. reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
  150. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  151. const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
  152. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  153. const_iterator cbegin() const _NOEXCEPT {return begin();}
  154. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  155. const_iterator cend() const _NOEXCEPT {return end();}
  156. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  157. const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
  158. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  159. const_reverse_iterator crend() const _NOEXCEPT {return rend();}
  160. // capacity:
  161. _LIBCPP_INLINE_VISIBILITY
  162. _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;}
  163. _LIBCPP_INLINE_VISIBILITY
  164. _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;}
  165. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  166. _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return _Size == 0;}
  167. // element access:
  168. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  169. reference operator[](size_type __n) _NOEXCEPT {
  170. _LIBCPP_ASSERT(__n < _Size, "out-of-bounds access in std::array<T, N>");
  171. return __elems_[__n];
  172. }
  173. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  174. const_reference operator[](size_type __n) const _NOEXCEPT {
  175. _LIBCPP_ASSERT(__n < _Size, "out-of-bounds access in std::array<T, N>");
  176. return __elems_[__n];
  177. }
  178. _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n)
  179. {
  180. if (__n >= _Size)
  181. __throw_out_of_range("array::at");
  182. return __elems_[__n];
  183. }
  184. _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const
  185. {
  186. if (__n >= _Size)
  187. __throw_out_of_range("array::at");
  188. return __elems_[__n];
  189. }
  190. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() _NOEXCEPT {return (*this)[0];}
  191. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const _NOEXCEPT {return (*this)[0];}
  192. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() _NOEXCEPT {return (*this)[_Size - 1];}
  193. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const _NOEXCEPT {return (*this)[_Size - 1];}
  194. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  195. value_type* data() _NOEXCEPT {return __elems_;}
  196. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  197. const value_type* data() const _NOEXCEPT {return __elems_;}
  198. };
  199. template <class _Tp>
  200. struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
  201. {
  202. // types:
  203. typedef array __self;
  204. typedef _Tp value_type;
  205. typedef value_type& reference;
  206. typedef const value_type& const_reference;
  207. typedef value_type* iterator;
  208. typedef const value_type* const_iterator;
  209. typedef value_type* pointer;
  210. typedef const value_type* const_pointer;
  211. typedef size_t size_type;
  212. typedef ptrdiff_t difference_type;
  213. typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
  214. typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
  215. typedef typename conditional<is_const<_Tp>::value, const char,
  216. char>::type _CharType;
  217. struct _ArrayInStructT { _Tp __data_[1]; };
  218. _ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)];
  219. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  220. value_type* data() _NOEXCEPT {return nullptr;}
  221. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  222. const value_type* data() const _NOEXCEPT {return nullptr;}
  223. // No explicit construct/copy/destroy for aggregate type
  224. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  225. void fill(const value_type&) {
  226. static_assert(!is_const<_Tp>::value,
  227. "cannot fill zero-sized array of type 'const T'");
  228. }
  229. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  230. void swap(array&) _NOEXCEPT {
  231. static_assert(!is_const<_Tp>::value,
  232. "cannot swap zero-sized array of type 'const T'");
  233. }
  234. // iterators:
  235. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  236. iterator begin() _NOEXCEPT {return iterator(data());}
  237. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  238. const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
  239. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  240. iterator end() _NOEXCEPT {return iterator(data());}
  241. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  242. const_iterator end() const _NOEXCEPT {return const_iterator(data());}
  243. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  244. reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
  245. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  246. const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
  247. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  248. reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
  249. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  250. const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
  251. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  252. const_iterator cbegin() const _NOEXCEPT {return begin();}
  253. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  254. const_iterator cend() const _NOEXCEPT {return end();}
  255. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  256. const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
  257. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  258. const_reverse_iterator crend() const _NOEXCEPT {return rend();}
  259. // capacity:
  260. _LIBCPP_INLINE_VISIBILITY
  261. _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return 0; }
  262. _LIBCPP_INLINE_VISIBILITY
  263. _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return 0;}
  264. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  265. _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;}
  266. // element access:
  267. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  268. reference operator[](size_type) _NOEXCEPT {
  269. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
  270. __libcpp_unreachable();
  271. }
  272. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  273. const_reference operator[](size_type) const _NOEXCEPT {
  274. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
  275. __libcpp_unreachable();
  276. }
  277. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  278. reference at(size_type) {
  279. __throw_out_of_range("array<T, 0>::at");
  280. __libcpp_unreachable();
  281. }
  282. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  283. const_reference at(size_type) const {
  284. __throw_out_of_range("array<T, 0>::at");
  285. __libcpp_unreachable();
  286. }
  287. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  288. reference front() _NOEXCEPT {
  289. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
  290. __libcpp_unreachable();
  291. }
  292. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  293. const_reference front() const _NOEXCEPT {
  294. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
  295. __libcpp_unreachable();
  296. }
  297. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  298. reference back() _NOEXCEPT {
  299. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
  300. __libcpp_unreachable();
  301. }
  302. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  303. const_reference back() const _NOEXCEPT {
  304. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
  305. __libcpp_unreachable();
  306. }
  307. };
  308. #if _LIBCPP_STD_VER > 14
  309. template<class _Tp, class... _Args,
  310. class = enable_if_t<__all<_IsSame<_Tp, _Args>::value...>::value>
  311. >
  312. array(_Tp, _Args...)
  313. -> array<_Tp, 1 + sizeof...(_Args)>;
  314. #endif
  315. template <class _Tp, size_t _Size>
  316. inline _LIBCPP_INLINE_VISIBILITY
  317. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  318. operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  319. {
  320. return _VSTD::equal(__x.begin(), __x.end(), __y.begin());
  321. }
  322. template <class _Tp, size_t _Size>
  323. inline _LIBCPP_INLINE_VISIBILITY
  324. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  325. operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  326. {
  327. return !(__x == __y);
  328. }
  329. template <class _Tp, size_t _Size>
  330. inline _LIBCPP_INLINE_VISIBILITY
  331. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  332. operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  333. {
  334. return _VSTD::lexicographical_compare(__x.begin(), __x.end(),
  335. __y.begin(), __y.end());
  336. }
  337. template <class _Tp, size_t _Size>
  338. inline _LIBCPP_INLINE_VISIBILITY
  339. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  340. operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  341. {
  342. return __y < __x;
  343. }
  344. template <class _Tp, size_t _Size>
  345. inline _LIBCPP_INLINE_VISIBILITY
  346. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  347. operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  348. {
  349. return !(__y < __x);
  350. }
  351. template <class _Tp, size_t _Size>
  352. inline _LIBCPP_INLINE_VISIBILITY
  353. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  354. operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  355. {
  356. return !(__x < __y);
  357. }
  358. template <class _Tp, size_t _Size>
  359. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  360. typename enable_if
  361. <
  362. _Size == 0 ||
  363. __is_swappable<_Tp>::value,
  364. void
  365. >::type
  366. swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y)
  367. _NOEXCEPT_(noexcept(__x.swap(__y)))
  368. {
  369. __x.swap(__y);
  370. }
  371. template <class _Tp, size_t _Size>
  372. struct _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> >
  373. : public integral_constant<size_t, _Size> {};
  374. template <size_t _Ip, class _Tp, size_t _Size>
  375. struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> >
  376. {
  377. static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)");
  378. typedef _Tp type;
  379. };
  380. template <size_t _Ip, class _Tp, size_t _Size>
  381. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  382. _Tp&
  383. get(array<_Tp, _Size>& __a) _NOEXCEPT
  384. {
  385. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
  386. return __a.__elems_[_Ip];
  387. }
  388. template <size_t _Ip, class _Tp, size_t _Size>
  389. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  390. const _Tp&
  391. get(const array<_Tp, _Size>& __a) _NOEXCEPT
  392. {
  393. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
  394. return __a.__elems_[_Ip];
  395. }
  396. template <size_t _Ip, class _Tp, size_t _Size>
  397. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  398. _Tp&&
  399. get(array<_Tp, _Size>&& __a) _NOEXCEPT
  400. {
  401. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
  402. return _VSTD::move(__a.__elems_[_Ip]);
  403. }
  404. template <size_t _Ip, class _Tp, size_t _Size>
  405. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  406. const _Tp&&
  407. get(const array<_Tp, _Size>&& __a) _NOEXCEPT
  408. {
  409. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)");
  410. return _VSTD::move(__a.__elems_[_Ip]);
  411. }
  412. #if _LIBCPP_STD_VER > 17
  413. template <typename _Tp, size_t _Size, size_t... _Index>
  414. _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
  415. __to_array_lvalue_impl(_Tp (&__arr)[_Size], index_sequence<_Index...>) {
  416. return {{__arr[_Index]...}};
  417. }
  418. template <typename _Tp, size_t _Size, size_t... _Index>
  419. _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
  420. __to_array_rvalue_impl(_Tp(&&__arr)[_Size], index_sequence<_Index...>) {
  421. return {{_VSTD::move(__arr[_Index])...}};
  422. }
  423. template <typename _Tp, size_t _Size>
  424. _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
  425. to_array(_Tp (&__arr)[_Size]) noexcept(is_nothrow_constructible_v<_Tp, _Tp&>) {
  426. static_assert(
  427. !is_array_v<_Tp>,
  428. "[array.creation]/1: to_array does not accept multidimensional arrays.");
  429. static_assert(
  430. is_constructible_v<_Tp, _Tp&>,
  431. "[array.creation]/1: to_array requires copy constructible elements.");
  432. return _VSTD::__to_array_lvalue_impl(__arr, make_index_sequence<_Size>());
  433. }
  434. template <typename _Tp, size_t _Size>
  435. _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
  436. to_array(_Tp(&&__arr)[_Size]) noexcept(is_nothrow_move_constructible_v<_Tp>) {
  437. static_assert(
  438. !is_array_v<_Tp>,
  439. "[array.creation]/4: to_array does not accept multidimensional arrays.");
  440. static_assert(
  441. is_move_constructible_v<_Tp>,
  442. "[array.creation]/4: to_array requires move constructible elements.");
  443. return _VSTD::__to_array_rvalue_impl(_VSTD::move(__arr),
  444. make_index_sequence<_Size>());
  445. }
  446. #endif // _LIBCPP_STD_VER > 17
  447. _LIBCPP_END_NAMESPACE_STD
  448. #endif // _LIBCPP_ARRAY