array 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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 <__config>
  93. #include <__debug>
  94. #include <__tuple>
  95. #include <algorithm>
  96. #include <cstdlib> // for _LIBCPP_UNREACHABLE
  97. #include <iterator>
  98. #include <stdexcept>
  99. #include <type_traits>
  100. #include <utility>
  101. #include <version>
  102. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  103. #pragma GCC system_header
  104. #endif
  105. _LIBCPP_BEGIN_NAMESPACE_STD
  106. template <class _Tp, size_t _Size>
  107. struct _LIBCPP_TEMPLATE_VIS array
  108. {
  109. // types:
  110. typedef array __self;
  111. typedef _Tp value_type;
  112. typedef value_type& reference;
  113. typedef const value_type& const_reference;
  114. typedef value_type* iterator;
  115. typedef const value_type* const_iterator;
  116. typedef value_type* pointer;
  117. typedef const value_type* const_pointer;
  118. typedef size_t size_type;
  119. typedef ptrdiff_t difference_type;
  120. typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
  121. typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
  122. _Tp __elems_[_Size];
  123. // No explicit construct/copy/destroy for aggregate type
  124. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  125. void fill(const value_type& __u) {
  126. _VSTD::fill_n(data(), _Size, __u);
  127. }
  128. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  129. void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
  130. _VSTD::swap_ranges(data(), data() + _Size, __a.data());
  131. }
  132. // iterators:
  133. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  134. iterator begin() _NOEXCEPT {return iterator(data());}
  135. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  136. const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
  137. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  138. iterator end() _NOEXCEPT {return iterator(data() + _Size);}
  139. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  140. const_iterator end() const _NOEXCEPT {return const_iterator(data() + _Size);}
  141. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  142. reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
  143. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  144. const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
  145. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  146. reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
  147. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  148. const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
  149. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  150. const_iterator cbegin() const _NOEXCEPT {return begin();}
  151. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  152. const_iterator cend() const _NOEXCEPT {return end();}
  153. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  154. const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
  155. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  156. const_reverse_iterator crend() const _NOEXCEPT {return rend();}
  157. // capacity:
  158. _LIBCPP_INLINE_VISIBILITY
  159. _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;}
  160. _LIBCPP_INLINE_VISIBILITY
  161. _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;}
  162. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  163. _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return _Size == 0;}
  164. // element access:
  165. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  166. reference operator[](size_type __n) _NOEXCEPT {
  167. _LIBCPP_ASSERT(__n < _Size, "out-of-bounds access in std::array<T, N>");
  168. return __elems_[__n];
  169. }
  170. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  171. const_reference operator[](size_type __n) const _NOEXCEPT {
  172. _LIBCPP_ASSERT(__n < _Size, "out-of-bounds access in std::array<T, N>");
  173. return __elems_[__n];
  174. }
  175. _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n)
  176. {
  177. if (__n >= _Size)
  178. __throw_out_of_range("array::at");
  179. return __elems_[__n];
  180. }
  181. _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const
  182. {
  183. if (__n >= _Size)
  184. __throw_out_of_range("array::at");
  185. return __elems_[__n];
  186. }
  187. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() _NOEXCEPT {return (*this)[0];}
  188. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const _NOEXCEPT {return (*this)[0];}
  189. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() _NOEXCEPT {return (*this)[_Size - 1];}
  190. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const _NOEXCEPT {return (*this)[_Size - 1];}
  191. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  192. value_type* data() _NOEXCEPT {return __elems_;}
  193. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  194. const value_type* data() const _NOEXCEPT {return __elems_;}
  195. };
  196. template <class _Tp>
  197. struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
  198. {
  199. // types:
  200. typedef array __self;
  201. typedef _Tp value_type;
  202. typedef value_type& reference;
  203. typedef const value_type& const_reference;
  204. typedef value_type* iterator;
  205. typedef const value_type* const_iterator;
  206. typedef value_type* pointer;
  207. typedef const value_type* const_pointer;
  208. typedef size_t size_type;
  209. typedef ptrdiff_t difference_type;
  210. typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
  211. typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
  212. typedef typename conditional<is_const<_Tp>::value, const char,
  213. char>::type _CharType;
  214. struct _ArrayInStructT { _Tp __data_[1]; };
  215. _ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)];
  216. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  217. value_type* data() _NOEXCEPT {return nullptr;}
  218. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  219. const value_type* data() const _NOEXCEPT {return nullptr;}
  220. // No explicit construct/copy/destroy for aggregate type
  221. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  222. void fill(const value_type&) {
  223. static_assert(!is_const<_Tp>::value,
  224. "cannot fill zero-sized array of type 'const T'");
  225. }
  226. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  227. void swap(array&) _NOEXCEPT {
  228. static_assert(!is_const<_Tp>::value,
  229. "cannot swap zero-sized array of type 'const T'");
  230. }
  231. // iterators:
  232. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  233. iterator begin() _NOEXCEPT {return iterator(data());}
  234. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  235. const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
  236. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  237. iterator end() _NOEXCEPT {return iterator(data());}
  238. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  239. const_iterator end() const _NOEXCEPT {return const_iterator(data());}
  240. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  241. reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
  242. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  243. const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
  244. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  245. reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
  246. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  247. const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
  248. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  249. const_iterator cbegin() const _NOEXCEPT {return begin();}
  250. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  251. const_iterator cend() const _NOEXCEPT {return end();}
  252. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  253. const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
  254. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  255. const_reverse_iterator crend() const _NOEXCEPT {return rend();}
  256. // capacity:
  257. _LIBCPP_INLINE_VISIBILITY
  258. _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return 0; }
  259. _LIBCPP_INLINE_VISIBILITY
  260. _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return 0;}
  261. _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
  262. _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;}
  263. // element access:
  264. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  265. reference operator[](size_type) _NOEXCEPT {
  266. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
  267. _LIBCPP_UNREACHABLE();
  268. }
  269. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  270. const_reference operator[](size_type) const _NOEXCEPT {
  271. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
  272. _LIBCPP_UNREACHABLE();
  273. }
  274. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  275. reference at(size_type) {
  276. __throw_out_of_range("array<T, 0>::at");
  277. _LIBCPP_UNREACHABLE();
  278. }
  279. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  280. const_reference at(size_type) const {
  281. __throw_out_of_range("array<T, 0>::at");
  282. _LIBCPP_UNREACHABLE();
  283. }
  284. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  285. reference front() _NOEXCEPT {
  286. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
  287. _LIBCPP_UNREACHABLE();
  288. }
  289. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  290. const_reference front() const _NOEXCEPT {
  291. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
  292. _LIBCPP_UNREACHABLE();
  293. }
  294. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
  295. reference back() _NOEXCEPT {
  296. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
  297. _LIBCPP_UNREACHABLE();
  298. }
  299. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  300. const_reference back() const _NOEXCEPT {
  301. _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
  302. _LIBCPP_UNREACHABLE();
  303. }
  304. };
  305. #if _LIBCPP_STD_VER >= 17
  306. template<class _Tp, class... _Args,
  307. class = enable_if_t<__all<_IsSame<_Tp, _Args>::value...>::value>
  308. >
  309. array(_Tp, _Args...)
  310. -> array<_Tp, 1 + sizeof...(_Args)>;
  311. #endif
  312. template <class _Tp, size_t _Size>
  313. inline _LIBCPP_INLINE_VISIBILITY
  314. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  315. operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  316. {
  317. return _VSTD::equal(__x.begin(), __x.end(), __y.begin());
  318. }
  319. template <class _Tp, size_t _Size>
  320. inline _LIBCPP_INLINE_VISIBILITY
  321. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  322. operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  323. {
  324. return !(__x == __y);
  325. }
  326. template <class _Tp, size_t _Size>
  327. inline _LIBCPP_INLINE_VISIBILITY
  328. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  329. operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  330. {
  331. return _VSTD::lexicographical_compare(__x.begin(), __x.end(),
  332. __y.begin(), __y.end());
  333. }
  334. template <class _Tp, size_t _Size>
  335. inline _LIBCPP_INLINE_VISIBILITY
  336. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  337. operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  338. {
  339. return __y < __x;
  340. }
  341. template <class _Tp, size_t _Size>
  342. inline _LIBCPP_INLINE_VISIBILITY
  343. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  344. operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  345. {
  346. return !(__y < __x);
  347. }
  348. template <class _Tp, size_t _Size>
  349. inline _LIBCPP_INLINE_VISIBILITY
  350. _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
  351. operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
  352. {
  353. return !(__x < __y);
  354. }
  355. template <class _Tp, size_t _Size>
  356. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
  357. typename enable_if
  358. <
  359. _Size == 0 ||
  360. __is_swappable<_Tp>::value,
  361. void
  362. >::type
  363. swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y)
  364. _NOEXCEPT_(noexcept(__x.swap(__y)))
  365. {
  366. __x.swap(__y);
  367. }
  368. template <class _Tp, size_t _Size>
  369. struct _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> >
  370. : public integral_constant<size_t, _Size> {};
  371. template <size_t _Ip, class _Tp, size_t _Size>
  372. struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> >
  373. {
  374. static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)");
  375. typedef _Tp type;
  376. };
  377. template <size_t _Ip, class _Tp, size_t _Size>
  378. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  379. _Tp&
  380. get(array<_Tp, _Size>& __a) _NOEXCEPT
  381. {
  382. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
  383. return __a.__elems_[_Ip];
  384. }
  385. template <size_t _Ip, class _Tp, size_t _Size>
  386. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  387. const _Tp&
  388. get(const array<_Tp, _Size>& __a) _NOEXCEPT
  389. {
  390. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
  391. return __a.__elems_[_Ip];
  392. }
  393. template <size_t _Ip, class _Tp, size_t _Size>
  394. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  395. _Tp&&
  396. get(array<_Tp, _Size>&& __a) _NOEXCEPT
  397. {
  398. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
  399. return _VSTD::move(__a.__elems_[_Ip]);
  400. }
  401. template <size_t _Ip, class _Tp, size_t _Size>
  402. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  403. const _Tp&&
  404. get(const array<_Tp, _Size>&& __a) _NOEXCEPT
  405. {
  406. static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)");
  407. return _VSTD::move(__a.__elems_[_Ip]);
  408. }
  409. #if _LIBCPP_STD_VER > 17
  410. template <typename _Tp, size_t _Size, size_t... _Index>
  411. _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
  412. __to_array_lvalue_impl(_Tp (&__arr)[_Size], index_sequence<_Index...>) {
  413. return {{__arr[_Index]...}};
  414. }
  415. template <typename _Tp, size_t _Size, size_t... _Index>
  416. _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
  417. __to_array_rvalue_impl(_Tp(&&__arr)[_Size], index_sequence<_Index...>) {
  418. return {{_VSTD::move(__arr[_Index])...}};
  419. }
  420. template <typename _Tp, size_t _Size>
  421. _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
  422. to_array(_Tp (&__arr)[_Size]) noexcept(is_nothrow_constructible_v<_Tp, _Tp&>) {
  423. static_assert(
  424. !is_array_v<_Tp>,
  425. "[array.creation]/1: to_array does not accept multidimensional arrays.");
  426. static_assert(
  427. is_constructible_v<_Tp, _Tp&>,
  428. "[array.creation]/1: to_array requires copy constructible elements.");
  429. return _VSTD::__to_array_lvalue_impl(__arr, make_index_sequence<_Size>());
  430. }
  431. template <typename _Tp, size_t _Size>
  432. _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
  433. to_array(_Tp(&&__arr)[_Size]) noexcept(is_nothrow_move_constructible_v<_Tp>) {
  434. static_assert(
  435. !is_array_v<_Tp>,
  436. "[array.creation]/4: to_array does not accept multidimensional arrays.");
  437. static_assert(
  438. is_move_constructible_v<_Tp>,
  439. "[array.creation]/4: to_array requires move constructible elements.");
  440. return _VSTD::__to_array_rvalue_impl(_VSTD::move(__arr),
  441. make_index_sequence<_Size>());
  442. }
  443. #endif // _LIBCPP_STD_VER > 17
  444. _LIBCPP_END_NAMESPACE_STD
  445. #endif // _LIBCPP_ARRAY