blob: 7ffa825a9652c127673b3853cb354af471e8c2d6 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- array -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_ARRAY
11#define _LIBCPP_ARRAY
12
13/*
14 array synopsis
15
16namespace std
17{
Howard Hinnant3b6579a2010-08-22 00:02:43 +000018template <class T, size_t N >
Howard Hinnantc51e1022010-05-11 19:42:16 +000019struct array
Howard Hinnant3b6579a2010-08-22 00:02:43 +000020{
21 // types:
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;
28 typedef T value_type;
Howard Hinnantc51e1022010-05-11 19:42:16 +000029 typedef T* pointer;
30 typedef const T* const_pointer;
Howard Hinnant3b6579a2010-08-22 00:02:43 +000031 typedef std::reverse_iterator<iterator> reverse_iterator;
32 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +000033
Howard Hinnant3b6579a2010-08-22 00:02:43 +000034 // No explicit construct/copy/destroy for aggregate type
35 void fill(const T& u);
Eric Fiselier6bfed252016-04-21 23:38:59 +000036 void swap(array& a) noexcept(is_nothrow_swappable_v<T>);
Howard Hinnantc51e1022010-05-11 19:42:16 +000037
Howard Hinnant3b6579a2010-08-22 00:02:43 +000038 // iterators:
Howard Hinnant32477e82011-05-31 21:06:33 +000039 iterator begin() noexcept;
40 const_iterator begin() const noexcept;
41 iterator end() noexcept;
42 const_iterator end() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000043
Howard Hinnant32477e82011-05-31 21:06:33 +000044 reverse_iterator rbegin() noexcept;
45 const_reverse_iterator rbegin() const noexcept;
46 reverse_iterator rend() noexcept;
47 const_reverse_iterator rend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000048
Howard Hinnant32477e82011-05-31 21:06:33 +000049 const_iterator cbegin() const noexcept;
50 const_iterator cend() const noexcept;
51 const_reverse_iterator crbegin() const noexcept;
52 const_reverse_iterator crend() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000053
Howard Hinnant3b6579a2010-08-22 00:02:43 +000054 // capacity:
Howard Hinnant32477e82011-05-31 21:06:33 +000055 constexpr size_type size() const noexcept;
56 constexpr size_type max_size() const noexcept;
Howard Hinnant12ff42f2012-07-20 19:20:49 +000057 constexpr bool empty() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000058
Howard Hinnant3b6579a2010-08-22 00:02:43 +000059 // element access:
60 reference operator[](size_type n);
Marshall Clow0abb1042013-07-17 18:25:36 +000061 const_reference operator[](size_type n) const; // constexpr in C++14
62 const_reference at(size_type n) const; // constexpr in C++14
Howard Hinnant3b6579a2010-08-22 00:02:43 +000063 reference at(size_type n);
Howard Hinnantc51e1022010-05-11 19:42:16 +000064
Howard Hinnant3b6579a2010-08-22 00:02:43 +000065 reference front();
Marshall Clow0abb1042013-07-17 18:25:36 +000066 const_reference front() const; // constexpr in C++14
Howard Hinnant3b6579a2010-08-22 00:02:43 +000067 reference back();
Marshall Clow0abb1042013-07-17 18:25:36 +000068 const_reference back() const; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000069
Howard Hinnant32477e82011-05-31 21:06:33 +000070 T* data() noexcept;
71 const T* data() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072};
73
Marshall Clow8847d032018-05-18 21:01:04 +000074 template <class T, class... U>
75 array(T, U...) -> array<T, 1 + sizeof...(U)>;
76
Howard Hinnant3b6579a2010-08-22 00:02:43 +000077template <class T, size_t N>
78 bool operator==(const array<T,N>& x, const array<T,N>& y);
79template <class T, size_t N>
80 bool operator!=(const array<T,N>& x, const array<T,N>& y);
81template <class T, size_t N>
82 bool operator<(const array<T,N>& x, const array<T,N>& y);
83template <class T, size_t N>
84 bool operator>(const array<T,N>& x, const array<T,N>& y);
85template <class T, size_t N>
86 bool operator<=(const array<T,N>& x, const array<T,N>& y);
87template <class T, size_t N>
88 bool operator>=(const array<T,N>& x, const array<T,N>& y);
Howard Hinnantc51e1022010-05-11 19:42:16 +000089
Howard Hinnant3b6579a2010-08-22 00:02:43 +000090template <class T, size_t N >
Marshall Clow8847d032018-05-18 21:01:04 +000091 void swap(array<T,N>& x, array<T,N>& y) noexcept(noexcept(x.swap(y))); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +000092
Marshall Clowce62b4d2019-01-11 21:57:12 +000093template <class T> struct tuple_size;
Louis Dionnee684aa62019-04-01 16:39:34 +000094template <size_t I, class T> struct tuple_element;
Howard Hinnantc51e1022010-05-11 19:42:16 +000095template <class T, size_t N> struct tuple_size<array<T, N>>;
Marshall Clow291a3512015-11-19 19:41:04 +000096template <size_t I, class T, size_t N> struct tuple_element<I, array<T, N>>;
97template <size_t I, class T, size_t N> T& get(array<T, N>&) noexcept; // constexpr in C++14
98template <size_t I, class T, size_t N> const T& get(const array<T, N>&) noexcept; // constexpr in C++14
99template <size_t I, class T, size_t N> T&& get(array<T, N>&&) noexcept; // constexpr in C++14
Eric Fiselier6dea8092015-12-18 00:36:55 +0000100template <size_t I, class T, size_t N> const T&& get(const array<T, N>&&) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000101
102} // std
103
104*/
105
106#include <__config>
107#include <__tuple>
108#include <type_traits>
109#include <utility>
110#include <iterator>
111#include <algorithm>
112#include <stdexcept>
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000113#include <cstdlib> // for _LIBCPP_UNREACHABLE
Marshall Clow0a1e7502018-09-12 19:41:40 +0000114#include <version>
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000115#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000117#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000119#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120
Eric Fiselierf4433a32017-05-31 22:07:49 +0000121
122
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123_LIBCPP_BEGIN_NAMESPACE_STD
124
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000125
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000126template <class _Tp, size_t _Size>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000127struct _LIBCPP_TEMPLATE_VIS array
Howard Hinnantc51e1022010-05-11 19:42:16 +0000128{
129 // types:
130 typedef array __self;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000131 typedef _Tp value_type;
132 typedef value_type& reference;
133 typedef const value_type& const_reference;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000134 typedef value_type* iterator;
135 typedef const value_type* const_iterator;
136 typedef value_type* pointer;
137 typedef const value_type* const_pointer;
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000138 typedef size_t size_type;
139 typedef ptrdiff_t difference_type;
140 typedef std::reverse_iterator<iterator> reverse_iterator;
141 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000142
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000143 _Tp __elems_[_Size];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000145 // No explicit construct/copy/destroy for aggregate type
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000146 _LIBCPP_INLINE_VISIBILITY void fill(const value_type& __u) {
147 _VSTD::fill_n(__elems_, _Size, __u);
148 }
Eric Fiselier55659e42018-02-04 01:03:08 +0000149
Howard Hinnant32477e82011-05-31 21:06:33 +0000150 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000151 void swap(array& __a) _NOEXCEPT_(__is_nothrow_swappable<_Tp>::value) {
152 std::swap_ranges(__elems_, __elems_ + _Size, __a.__elems_);
153 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000155 // iterators:
Marshall Clow99ba0022017-01-04 17:58:17 +0000156 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000157 iterator begin() _NOEXCEPT {return iterator(data());}
Marshall Clow99ba0022017-01-04 17:58:17 +0000158 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000159 const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
Marshall Clow99ba0022017-01-04 17:58:17 +0000160 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000161 iterator end() _NOEXCEPT {return iterator(data() + _Size);}
Marshall Clow99ba0022017-01-04 17:58:17 +0000162 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000163 const_iterator end() const _NOEXCEPT {return const_iterator(data() + _Size);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164
Marshall Clow99ba0022017-01-04 17:58:17 +0000165 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant32477e82011-05-31 21:06:33 +0000166 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
Marshall Clow99ba0022017-01-04 17:58:17 +0000167 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant32477e82011-05-31 21:06:33 +0000168 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
Marshall Clow99ba0022017-01-04 17:58:17 +0000169 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant32477e82011-05-31 21:06:33 +0000170 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
Marshall Clow99ba0022017-01-04 17:58:17 +0000171 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant32477e82011-05-31 21:06:33 +0000172 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000173
Marshall Clow99ba0022017-01-04 17:58:17 +0000174 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant32477e82011-05-31 21:06:33 +0000175 const_iterator cbegin() const _NOEXCEPT {return begin();}
Marshall Clow99ba0022017-01-04 17:58:17 +0000176 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant32477e82011-05-31 21:06:33 +0000177 const_iterator cend() const _NOEXCEPT {return end();}
Marshall Clow99ba0022017-01-04 17:58:17 +0000178 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant32477e82011-05-31 21:06:33 +0000179 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
Marshall Clow99ba0022017-01-04 17:58:17 +0000180 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Howard Hinnant32477e82011-05-31 21:06:33 +0000181 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000182
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000183 // capacity:
Howard Hinnant32477e82011-05-31 21:06:33 +0000184 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant12ff42f2012-07-20 19:20:49 +0000185 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return _Size;}
Howard Hinnant32477e82011-05-31 21:06:33 +0000186 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant12ff42f2012-07-20 19:20:49 +0000187 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return _Size;}
Marshall Clow425f5752017-11-15 05:51:26 +0000188 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000189 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return false; }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000191 // element access:
Marshall Clowac63f412017-01-16 03:02:10 +0000192 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Marshall Clow8d7c9f12019-03-14 21:56:57 +0000193 reference operator[](size_type __n) _NOEXCEPT {return __elems_[__n];}
Marshall Clowac63f412017-01-16 03:02:10 +0000194 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8d7c9f12019-03-14 21:56:57 +0000195 const_reference operator[](size_type __n) const _NOEXCEPT {return __elems_[__n];}
Marshall Clowac63f412017-01-16 03:02:10 +0000196
Louis Dionne8468f972020-05-22 09:23:32 -0400197 _LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n)
198 {
199 if (__n >= _Size)
200 __throw_out_of_range("array::at");
201 return __elems_[__n];
202 }
203
204 _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const
205 {
206 if (__n >= _Size)
207 __throw_out_of_range("array::at");
208 return __elems_[__n];
209 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210
Marshall Clow05cf6692019-03-19 03:30:07 +0000211 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference front() _NOEXCEPT {return __elems_[0];}
212 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference front() const _NOEXCEPT {return __elems_[0];}
213 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 reference back() _NOEXCEPT {return __elems_[_Size - 1];}
214 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference back() const _NOEXCEPT {return __elems_[_Size - 1];}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215
Marshall Clow99ba0022017-01-04 17:58:17 +0000216 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Nirav Dave129fb5c2018-02-06 03:03:37 +0000217 value_type* data() _NOEXCEPT {return __elems_;}
Marshall Clow99ba0022017-01-04 17:58:17 +0000218 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
Nirav Dave129fb5c2018-02-06 03:03:37 +0000219 const value_type* data() const _NOEXCEPT {return __elems_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000220};
221
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000222template <class _Tp>
223struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
224{
225 // types:
226 typedef array __self;
227 typedef _Tp value_type;
228 typedef value_type& reference;
229 typedef const value_type& const_reference;
230 typedef value_type* iterator;
231 typedef const value_type* const_iterator;
232 typedef value_type* pointer;
233 typedef const value_type* const_pointer;
234 typedef size_t size_type;
235 typedef ptrdiff_t difference_type;
236 typedef std::reverse_iterator<iterator> reverse_iterator;
237 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
238
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000239 typedef typename conditional<is_const<_Tp>::value, const char,
240 char>::type _CharType;
Eric Fiselier6a537582018-02-07 23:50:25 +0000241
242 struct _ArrayInStructT { _Tp __data_[1]; };
243 _ALIGNAS_TYPE(_ArrayInStructT) _CharType __elems_[sizeof(_ArrayInStructT)];
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000244
245 // No explicit construct/copy/destroy for aggregate type
246 _LIBCPP_INLINE_VISIBILITY void fill(const value_type&) {
247 static_assert(!is_const<_Tp>::value,
248 "cannot fill zero-sized array of type 'const T'");
249 }
250
251 _LIBCPP_INLINE_VISIBILITY
252 void swap(array&) _NOEXCEPT {
253 static_assert(!is_const<_Tp>::value,
254 "cannot swap zero-sized array of type 'const T'");
255 }
256
257 // iterators:
258 _LIBCPP_INLINE_VISIBILITY
259 iterator begin() _NOEXCEPT {return iterator(data());}
260 _LIBCPP_INLINE_VISIBILITY
261 const_iterator begin() const _NOEXCEPT {return const_iterator(data());}
262 _LIBCPP_INLINE_VISIBILITY
263 iterator end() _NOEXCEPT {return iterator(data());}
264 _LIBCPP_INLINE_VISIBILITY
265 const_iterator end() const _NOEXCEPT {return const_iterator(data());}
266
267 _LIBCPP_INLINE_VISIBILITY
268 reverse_iterator rbegin() _NOEXCEPT {return reverse_iterator(end());}
269 _LIBCPP_INLINE_VISIBILITY
270 const_reverse_iterator rbegin() const _NOEXCEPT {return const_reverse_iterator(end());}
271 _LIBCPP_INLINE_VISIBILITY
272 reverse_iterator rend() _NOEXCEPT {return reverse_iterator(begin());}
273 _LIBCPP_INLINE_VISIBILITY
274 const_reverse_iterator rend() const _NOEXCEPT {return const_reverse_iterator(begin());}
275
276 _LIBCPP_INLINE_VISIBILITY
277 const_iterator cbegin() const _NOEXCEPT {return begin();}
278 _LIBCPP_INLINE_VISIBILITY
279 const_iterator cend() const _NOEXCEPT {return end();}
280 _LIBCPP_INLINE_VISIBILITY
281 const_reverse_iterator crbegin() const _NOEXCEPT {return rbegin();}
282 _LIBCPP_INLINE_VISIBILITY
283 const_reverse_iterator crend() const _NOEXCEPT {return rend();}
284
285 // capacity:
286 _LIBCPP_INLINE_VISIBILITY
287 _LIBCPP_CONSTEXPR size_type size() const _NOEXCEPT {return 0; }
288 _LIBCPP_INLINE_VISIBILITY
289 _LIBCPP_CONSTEXPR size_type max_size() const _NOEXCEPT {return 0;}
290 _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_INLINE_VISIBILITY
291 _LIBCPP_CONSTEXPR bool empty() const _NOEXCEPT {return true;}
292
293 // element access:
294 _LIBCPP_INLINE_VISIBILITY
Marshall Clow8d7c9f12019-03-14 21:56:57 +0000295 reference operator[](size_type) _NOEXCEPT {
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000296 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
297 _LIBCPP_UNREACHABLE();
298 }
299
300 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Marshall Clow8d7c9f12019-03-14 21:56:57 +0000301 const_reference operator[](size_type) const _NOEXCEPT {
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000302 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
303 _LIBCPP_UNREACHABLE();
304 }
305
306 _LIBCPP_INLINE_VISIBILITY
307 reference at(size_type) {
308 __throw_out_of_range("array<T, 0>::at");
309 _LIBCPP_UNREACHABLE();
310 }
311
312 _LIBCPP_INLINE_VISIBILITY
313 const_reference at(size_type) const {
314 __throw_out_of_range("array<T, 0>::at");
315 _LIBCPP_UNREACHABLE();
316 }
317
318 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000319 reference front() _NOEXCEPT {
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000320 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
321 _LIBCPP_UNREACHABLE();
322 }
323
324 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000325 const_reference front() const _NOEXCEPT {
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000326 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::front() on a zero-sized array");
327 _LIBCPP_UNREACHABLE();
328 }
329
330 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000331 reference back() _NOEXCEPT {
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000332 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
333 _LIBCPP_UNREACHABLE();
334 }
335
336 _LIBCPP_INLINE_VISIBILITY
Marshall Clow05cf6692019-03-19 03:30:07 +0000337 const_reference back() const _NOEXCEPT {
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000338 _LIBCPP_ASSERT(false, "cannot call array<T, 0>::back() on a zero-sized array");
339 _LIBCPP_UNREACHABLE();
340 }
341
342 _LIBCPP_INLINE_VISIBILITY
343 value_type* data() _NOEXCEPT {return reinterpret_cast<value_type*>(__elems_);}
344 _LIBCPP_INLINE_VISIBILITY
345 const value_type* data() const _NOEXCEPT {return reinterpret_cast<const value_type*>(__elems_);}
346};
347
348
Marshall Clow8847d032018-05-18 21:01:04 +0000349#ifndef _LIBCPP_HAS_NO_DEDUCTION_GUIDES
350template<class _Tp, class... _Args,
Eric Fiselierd8ec8902020-04-09 17:39:40 -0400351 class = _EnableIf<__all<_IsSame<_Tp, _Args>::value...>::value>
Marshall Clow8847d032018-05-18 21:01:04 +0000352 >
353array(_Tp, _Args...)
354 -> array<_Tp, 1 + sizeof...(_Args)>;
355#endif
356
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000357template <class _Tp, size_t _Size>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000358inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow35a45042018-08-02 02:11:06 +0000359_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +0000360operator==(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
361{
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000362 return _VSTD::equal(__x.begin(), __x.end(), __y.begin());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000363}
364
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000365template <class _Tp, size_t _Size>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000366inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow35a45042018-08-02 02:11:06 +0000367_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368operator!=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
369{
370 return !(__x == __y);
371}
372
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000373template <class _Tp, size_t _Size>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000374inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow35a45042018-08-02 02:11:06 +0000375_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +0000376operator<(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
377{
Eric Fiselier5037a3b2018-02-07 21:06:13 +0000378 return _VSTD::lexicographical_compare(__x.begin(), __x.end(),
379 __y.begin(), __y.end());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000380}
381
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000382template <class _Tp, size_t _Size>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000383inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow35a45042018-08-02 02:11:06 +0000384_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +0000385operator>(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
386{
387 return __y < __x;
388}
389
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000390template <class _Tp, size_t _Size>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000391inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow35a45042018-08-02 02:11:06 +0000392_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +0000393operator<=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
394{
395 return !(__y < __x);
396}
397
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000398template <class _Tp, size_t _Size>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000399inline _LIBCPP_INLINE_VISIBILITY
Marshall Clow35a45042018-08-02 02:11:06 +0000400_LIBCPP_CONSTEXPR_AFTER_CXX17 bool
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401operator>=(const array<_Tp, _Size>& __x, const array<_Tp, _Size>& __y)
402{
403 return !(__x < __y);
404}
405
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000406template <class _Tp, size_t _Size>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000407inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant44267242011-06-01 19:59:32 +0000408typename enable_if
409<
Eric Fiselier6bfed252016-04-21 23:38:59 +0000410 _Size == 0 ||
Howard Hinnant44267242011-06-01 19:59:32 +0000411 __is_swappable<_Tp>::value,
412 void
413>::type
Marshall Clowff1aa3d2016-03-07 21:57:10 +0000414swap(array<_Tp, _Size>& __x, array<_Tp, _Size>& __y)
Eric Fiselier6bfed252016-04-21 23:38:59 +0000415 _NOEXCEPT_(noexcept(__x.swap(__y)))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000416{
417 __x.swap(__y);
418}
419
420template <class _Tp, size_t _Size>
Marshall Clowce62b4d2019-01-11 21:57:12 +0000421struct _LIBCPP_TEMPLATE_VIS tuple_size<array<_Tp, _Size> >
Howard Hinnant8e29cda2010-09-21 20:16:37 +0000422 : public integral_constant<size_t, _Size> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424template <size_t _Ip, class _Tp, size_t _Size>
Louis Dionnee684aa62019-04-01 16:39:34 +0000425struct _LIBCPP_TEMPLATE_VIS tuple_element<_Ip, array<_Tp, _Size> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426{
Marshall Clow6f77fa12017-06-12 14:41:37 +0000427 static_assert(_Ip < _Size, "Index out of bounds in std::tuple_element<> (std::array)");
Howard Hinnantc51e1022010-05-11 19:42:16 +0000428 typedef _Tp type;
429};
430
431template <size_t _Ip, class _Tp, size_t _Size>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000432inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433_Tp&
Howard Hinnant32477e82011-05-31 21:06:33 +0000434get(array<_Tp, _Size>& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000435{
Marshall Clow66c49ca2012-12-18 16:46:30 +0000436 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array)");
Marshall Clow0abb1042013-07-17 18:25:36 +0000437 return __a.__elems_[_Ip];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438}
439
440template <size_t _Ip, class _Tp, size_t _Size>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000441inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000442const _Tp&
Howard Hinnant32477e82011-05-31 21:06:33 +0000443get(const array<_Tp, _Size>& __a) _NOEXCEPT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000444{
Marshall Clow66c49ca2012-12-18 16:46:30 +0000445 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array)");
Marshall Clow0abb1042013-07-17 18:25:36 +0000446 return __a.__elems_[_Ip];
Howard Hinnantc51e1022010-05-11 19:42:16 +0000447}
448
Eric Fiselier73abed72017-04-16 02:50:40 +0000449#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000450
451template <size_t _Ip, class _Tp, size_t _Size>
Howard Hinnantb8a8ca22013-10-04 22:09:00 +0000452inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnant22e97242010-11-17 19:52:17 +0000453_Tp&&
Howard Hinnant32477e82011-05-31 21:06:33 +0000454get(array<_Tp, _Size>&& __a) _NOEXCEPT
Howard Hinnant22e97242010-11-17 19:52:17 +0000455{
Marshall Clow66c49ca2012-12-18 16:46:30 +0000456 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (std::array &&)");
Marshall Clow0abb1042013-07-17 18:25:36 +0000457 return _VSTD::move(__a.__elems_[_Ip]);
Howard Hinnant22e97242010-11-17 19:52:17 +0000458}
459
Eric Fiselier6dea8092015-12-18 00:36:55 +0000460template <size_t _Ip, class _Tp, size_t _Size>
461inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
462const _Tp&&
463get(const array<_Tp, _Size>&& __a) _NOEXCEPT
464{
465 static_assert(_Ip < _Size, "Index out of bounds in std::get<> (const std::array &&)");
466 return _VSTD::move(__a.__elems_[_Ip]);
467}
468
Eric Fiselier73abed72017-04-16 02:50:40 +0000469#endif // !_LIBCPP_CXX03_LANG
Howard Hinnant22e97242010-11-17 19:52:17 +0000470
Marek Kurdej0d715672020-01-30 13:27:35 +0100471#if _LIBCPP_STD_VER > 17
472
473template <typename _Tp, size_t _Size, size_t... _Index>
474_LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
475__to_array_lvalue_impl(_Tp (&__arr)[_Size], index_sequence<_Index...>) {
476 return {{__arr[_Index]...}};
477}
478
479template <typename _Tp, size_t _Size, size_t... _Index>
480_LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
481__to_array_rvalue_impl(_Tp(&&__arr)[_Size], index_sequence<_Index...>) {
482 return {{_VSTD::move(__arr[_Index])...}};
483}
484
485template <typename _Tp, size_t _Size>
486_LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
487to_array(_Tp (&__arr)[_Size]) noexcept(is_nothrow_constructible_v<_Tp, _Tp&>) {
488 static_assert(
489 !is_array_v<_Tp>,
490 "[array.creation]/1: to_array does not accept multidimensional arrays.");
491 static_assert(
492 is_constructible_v<_Tp, _Tp&>,
493 "[array.creation]/1: to_array requires copy constructible elements.");
494 return __to_array_lvalue_impl(__arr, make_index_sequence<_Size>());
495}
496
497template <typename _Tp, size_t _Size>
498_LIBCPP_INLINE_VISIBILITY constexpr array<remove_cv_t<_Tp>, _Size>
499to_array(_Tp(&&__arr)[_Size]) noexcept(is_nothrow_move_constructible_v<_Tp>) {
500 static_assert(
501 !is_array_v<_Tp>,
502 "[array.creation]/4: to_array does not accept multidimensional arrays.");
503 static_assert(
504 is_move_constructible_v<_Tp>,
505 "[array.creation]/4: to_array requires move constructible elements.");
506 return __to_array_rvalue_impl(_VSTD::move(__arr),
507 make_index_sequence<_Size>());
508}
509
510#endif // _LIBCPP_STD_VER > 17
511
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512_LIBCPP_END_NAMESPACE_STD
513
514#endif // _LIBCPP_ARRAY