2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
18 template <class T, size_t N >
22 typedef T & reference;
23 typedef const T & const_reference;
24 typedef implementation defined iterator;
25 typedef implementation defined const_iterator;
26 typedef size_t size_type;
27 typedef ptrdiff_t difference_type;
30 typedef const T* const_pointer;
31 typedef std::reverse_iterator<iterator> reverse_iterator;
32 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
34 // No explicit construct/copy/destroy for aggregate type
35 void fill(const T& u); // constexpr in C++20
36 void swap(array& a) noexcept(is_nothrow_swappable_v<T>); // constexpr in C++20
39 iterator begin() noexcept; // constexpr in C++17
40 const_iterator begin() const noexcept; // constexpr in C++17
41 iterator end() noexcept; // constexpr in C++17
42 const_iterator end() const noexcept; // constexpr in C++17
44 reverse_iterator rbegin() noexcept; // constexpr in C++17
45 const_reverse_iterator rbegin() const noexcept; // constexpr in C++17
46 reverse_iterator rend() noexcept; // constexpr in C++17
47 const_reverse_iterator rend() const noexcept; // constexpr in C++17
49 const_iterator cbegin() const noexcept; // constexpr in C++17
50 const_iterator cend() const noexcept; // constexpr in C++17
51 const_reverse_iterator crbegin() const noexcept; // constexpr in C++17
52 const_reverse_iterator crend() const noexcept; // constexpr in C++17
55 constexpr size_type size() const noexcept;
56 constexpr size_type max_size() const noexcept;
57 constexpr bool empty() const noexcept;
60 reference operator[](size_type n); // constexpr in C++17
61 const_reference operator[](size_type n) const; // constexpr in C++14
62 reference at(size_type n); // constexpr in C++17
63 const_reference at(size_type n) const; // constexpr in C++14
65 reference front(); // constexpr in C++17
66 const_reference front() const; // constexpr in C++14
67 reference back(); // constexpr in C++17
68 const_reference back() const; // constexpr in C++14
70 T* data() noexcept; // constexpr in C++17
71 const T* data() const noexcept; // constexpr in C++17
74 template <class T, class... U>
75 array(T, U...) -> array<T, 1 + sizeof...(U)>; // C++17
77 template <class T, size_t N>
78 bool operator==(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
79 template <class T, size_t N>
80 bool operator!=(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
81 template <class T, size_t N>
82 bool operator<(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
83 template <class T, size_t N>
84 bool operator>(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
85 template <class T, size_t N>
86 bool operator<=(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
87 template <class T, size_t N>
88 bool operator>=(const array<T,N>& x, const array<T,N>& y); // constexpr in C++20
90 template <class T, size_t N >
91 void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20
93 template <class T, size_t N>
94 constexpr array<remove_cv_t<T>, N> to_array(T (&a)[N]); // C++20
95 template <class T, size_t N>
96 constexpr array<remove_cv_t<T>, N> to_array(T (&&a)[N]); // C++20
98 template <class T> struct tuple_size;
99 template <size_t I, class T> struct tuple_element;
100 template <class T, size_t N> struct tuple_size<array<T, N>>;
101 template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>;
102 template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
103 template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
104 template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
105 template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14
115 #include <cstdlib> // for _LIBCPP_UNREACHABLE
118 #include <type_traits>
122 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
123 #pragma GCC system_header
126 _LIBCPP_BEGIN_NAMESPACE_STD
128 template <class _Tp, size_t _Size>
129 struct _LIBCPP_TEMPLATE_VIS array
132 typedef array __self;
133 typedef _Tp value_type;
134 typedef value_type& reference;
135 typedef const value_type& const_reference;
136 typedef value_type* iterator;
137 typedef const value_type* const_iterator;
138 typedef value_type* pointer;
139 typedef const value_type* const_pointer;
140 typedef size_t size_type;
141 typedef ptrdiff_t difference_type;
142 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
143 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
147 // No explicit construct/copy/destroy for aggregate type
148 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
149 void fill(const value_type& __u) {
150 _VSTD::fill_n(data(), _Size, __u);
153 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
154 void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
155 _VSTD::swap_ranges(data(), data() + _Size, __a.data());
159 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
160 iterator begin() _NOEXCEPT {return iterator(data());}
161 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
162 const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
163 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
164 iterator end() _NOEXCEPT {return iterator(data() + _Size);}
165 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
166 const_iterator end() const _NOEXCEPT {return const_iterator(data() + _Size);}
168 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
169 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
170 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
171 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
172 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
173 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
174 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
175 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
177 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
178 const_iterator cbegin() const _NOEXCEPT {return begin();}
179 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
180 const_iterator cend() const _NOEXCEPT {return end();}
181 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
182 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
183 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
184 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
187 _LIBCPP_INLINE_VISIBILITY
188 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;}
189 _LIBCPP_INLINE_VISIBILITY
190 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;}
191 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
192 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return _Size == 0;}
195 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
196 reference operator[](size_type __n) _NOEXCEPT {
197 _LIBCPP_ASSERT(__n < _Size, "out-of-bounds access in std::array<T, N>");
198 return __elems_[__n];
200 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
201 const_reference operator[](size_type __n) const _NOEXCEPT {
202 _LIBCPP_ASSERT(__n < _Size, "out-of-bounds access in std::array<T, N>");
203 return __elems_[__n];
206 _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n)
209 __throw_out_of_range("array::at");
210 return __elems_[__n];
213 _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const
216 __throw_out_of_range("array::at");
217 return __elems_[__n];
220 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() _NOEXCEPT {return (*this)[0];}
221 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const _NOEXCEPT {return (*this)[0];}
222 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() _NOEXCEPT {return (*this)[_Size - 1];}
223 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const _NOEXCEPT {return (*this)[_Size - 1];}
225 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
226 value_type* data() _NOEXCEPT {return __elems_;}
227 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
228 const value_type* data() const _NOEXCEPT {return __elems_;}
232 struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
235 typedef array __self;
236 typedef _Tp value_type;
237 typedef value_type& reference;
238 typedef const value_type& const_reference;
239 typedef value_type* iterator;
240 typedef const value_type* const_iterator;
241 typedef value_type* pointer;
242 typedef const value_type* const_pointer;
243 typedef size_t size_type;
244 typedef ptrdiff_t difference_type;
245 typedef _VSTD::reverse_iterator<iterator> reverse_iterator;
246 typedef _VSTD::reverse_iterator<const_iterator> const_reverse_iterator;
248 typedef typename conditional<is_const<_Tp>::value, const char,
249 char>::type _CharType;
251 struct _ArrayInStructT { _Tp __data_[1]; };
252 _ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)];
254 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
255 value_type* data() _NOEXCEPT {return nullptr;}
256 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
257 const value_type* data() const _NOEXCEPT {return nullptr;}
259 // No explicit construct/copy/destroy for aggregate type
260 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
261 void fill(const value_type&) {
262 static_assert(!is_const<_Tp>::value,
263 "cannot fill zero-sized array of type 'const T'");
266 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
267 void swap(array&) _NOEXCEPT {
268 static_assert(!is_const<_Tp>::value,
269 "cannot swap zero-sized array of type 'const T'");
273 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
274 iterator begin() _NOEXCEPT {return iterator(data());}
275 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
276 const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
277 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
278 iterator end() _NOEXCEPT {return iterator(data());}
279 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
280 const_iterator end() const _NOEXCEPT {return const_iterator(data());}
282 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
283 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
284 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
285 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
286 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
287 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
288 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
289 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
291 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
292 const_iterator cbegin() const _NOEXCEPT {return begin();}
293 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
294 const_iterator cend() const _NOEXCEPT {return end();}
295 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
296 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
297 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
298 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
301 _LIBCPP_INLINE_VISIBILITY
302 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return 0; }
303 _LIBCPP_INLINE_VISIBILITY
304 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return 0;}
305 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
306 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;}
309 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
310 reference operator[](size_type) _NOEXCEPT {
311 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
312 _LIBCPP_UNREACHABLE();
315 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
316 const_reference operator[](size_type) const _NOEXCEPT {
317 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
318 _LIBCPP_UNREACHABLE();
321 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
322 reference at(size_type) {
323 __throw_out_of_range("array<T, 0>::at");
324 _LIBCPP_UNREACHABLE();
327 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
328 const_reference at(size_type) const {
329 __throw_out_of_range("array<T, 0>::at");
330 _LIBCPP_UNREACHABLE();
333 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
334 reference front() _NOEXCEPT {
335 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
336 _LIBCPP_UNREACHABLE();
339 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
340 const_reference front() const _NOEXCEPT {
341 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
342 _LIBCPP_UNREACHABLE();
345 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
346 reference back() _NOEXCEPT {
347 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
348 _LIBCPP_UNREACHABLE();
351 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
352 const_reference back() const _NOEXCEPT {
353 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
354 _LIBCPP_UNREACHABLE();
359 #if _LIBCPP_STD_VER >= 17
360 template<class _Tp, class... _Args,
361 class = enable_if_t<__all<_IsSame<_Tp, _Args>::value...>::value>
364 -> array<_Tp, 1 + sizeof...(_Args)>;
367 template <class _Tp, size_t _Size>
368 inline _LIBCPP_INLINE_VISIBILITY
369 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
370 operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
372 return _VSTD::equal(__x.begin(), __x.end(), __y.begin());
375 template <class _Tp, size_t _Size>
376 inline _LIBCPP_INLINE_VISIBILITY
377 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
378 operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
380 return !(__x == __y);
383 template <class _Tp, size_t _Size>
384 inline _LIBCPP_INLINE_VISIBILITY
385 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
386 operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
388 return _VSTD::lexicographical_compare(__x.begin(), __x.end(),
389 __y.begin(), __y.end());
392 template <class _Tp, size_t _Size>
393 inline _LIBCPP_INLINE_VISIBILITY
394 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
395 operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
400 template <class _Tp, size_t _Size>
401 inline _LIBCPP_INLINE_VISIBILITY
402 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
403 operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
408 template <class _Tp, size_t _Size>
409 inline _LIBCPP_INLINE_VISIBILITY
410 _LIBCPP_CONSTEXPR_AFTER_CXX17 bool
411 operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
416 template <class _Tp, size_t _Size>
417 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX17
421 __is_swappable<_Tp>::value,
424 swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y)
425 _NOEXCEPT_(noexcept(__x.swap(__y)))
430 template <class _Tp, size_t _Size>
431 struct _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> >
432 : public integral_constant<size_t, _Size> {};
434 template <size_t _Ip, class _Tp, size_t _Size>
435 struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> >
437 static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)");
441 template <size_t _Ip, class _Tp, size_t _Size>
442 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
444 get(array<_Tp, _Size>& __a) _NOEXCEPT
446 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
447 return __a.__elems_[_Ip];
450 template <size_t _Ip, class _Tp, size_t _Size>
451 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
453 get(const array<_Tp, _Size>& __a) _NOEXCEPT
455 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
456 return __a.__elems_[_Ip];
459 template <size_t _Ip, class _Tp, size_t _Size>
460 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
462 get(array<_Tp, _Size>&& __a) _NOEXCEPT
464 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
465 return _VSTD::move(__a.__elems_[_Ip]);
468 template <size_t _Ip, class _Tp, size_t _Size>
469 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
471 get(const array<_Tp, _Size>&& __a) _NOEXCEPT
473 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)");
474 return _VSTD::move(__a.__elems_[_Ip]);
477 #if _LIBCPP_STD_VER > 17
479 template <typename _Tp, size_t _Size, size_t... _Index>
480 _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
481 __to_array_lvalue_impl(_Tp (&__arr)[_Size], index_sequence<_Index...>) {
482 return {{__arr[_Index]...}};
485 template <typename _Tp, size_t _Size, size_t... _Index>
486 _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
487 __to_array_rvalue_impl(_Tp(&&__arr)[_Size], index_sequence<_Index...>) {
488 return {{_VSTD::move(__arr[_Index])...}};
491 template <typename _Tp, size_t _Size>
492 _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
493 to_array(_Tp (&__arr)[_Size]) noexcept(is_nothrow_constructible_v<_Tp, _Tp&>) {
496 "[array.creation]/1: to_array does not accept multidimensional arrays.");
498 is_constructible_v<_Tp, _Tp&>,
499 "[array.creation]/1: to_array requires copy constructible elements.");
500 return _VSTD::__to_array_lvalue_impl(__arr, make_index_sequence<_Size>());
503 template <typename _Tp, size_t _Size>
504 _LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
505 to_array(_Tp(&&__arr)[_Size]) noexcept(is_nothrow_move_constructible_v<_Tp>) {
508 "[array.creation]/4: to_array does not accept multidimensional arrays.");
510 is_move_constructible_v<_Tp>,
511 "[array.creation]/4: to_array requires move constructible elements.");
512 return _VSTD::__to_array_rvalue_impl(_VSTD::move(__arr),
513 make_index_sequence<_Size>());
516 #endif // _LIBCPP_STD_VER > 17
518 _LIBCPP_END_NAMESPACE_STD
520 #endif // _LIBCPP_ARRAY