]>
Commit | Line | Data |
---|---|---|
af13a7a6 BK |
1 | // <array> -*- C++ -*- |
2 | ||
6441eb6d | 3 | // Copyright (C) 2007-2025 Free Software Foundation, Inc. |
af13a7a6 BK |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
af13a7a6 BK |
9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
af13a7a6 | 19 | |
748086b7 JJ |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
af13a7a6 BK |
24 | |
25 | /** @file include/array | |
26 | * This is a Standard C++ Library header. | |
27 | */ | |
28 | ||
4514bed6 BK |
29 | #ifndef _GLIBCXX_ARRAY |
30 | #define _GLIBCXX_ARRAY 1 | |
af13a7a6 | 31 | |
63a598de | 32 | #ifdef _GLIBCXX_SYSHDR |
af13a7a6 | 33 | #pragma GCC system_header |
63a598de | 34 | #endif |
af13a7a6 | 35 | |
734f5023 | 36 | #if __cplusplus < 201103L |
ab65a4c7 | 37 | # include <bits/c++0x_warning.h> |
57317d2a | 38 | #else |
af13a7a6 | 39 | |
261d5a4a JW |
40 | #include <compare> |
41 | #include <initializer_list> | |
42 | ||
43 | #include <type_traits> | |
beb0086f | 44 | #include <bits/functexcept.h> |
e133ace8 | 45 | #include <bits/stl_algobase.h> |
261d5a4a JW |
46 | #include <bits/range_access.h> // std::begin, std::end etc. |
47 | #include <bits/utility.h> // std::index_sequence, std::tuple_size | |
6db08247 | 48 | #include <debug/assertions.h> |
e133ace8 | 49 | |
7ffa63df | 50 | #define __glibcxx_want_array_constexpr |
43626143 | 51 | #define __glibcxx_want_freestanding_array |
7ffa63df | 52 | #define __glibcxx_want_nonmember_container_access |
083b7f28 AA |
53 | #define __glibcxx_want_to_array |
54 | #include <bits/version.h> | |
55 | ||
12ffa228 BK |
56 | namespace std _GLIBCXX_VISIBILITY(default) |
57 | { | |
6db08247 | 58 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
53dc5044 | 59 | |
ef8d5ac0 | 60 | template<typename _Tp, size_t _Nm> |
90f9c94e PC |
61 | struct __array_traits |
62 | { | |
ef8d5ac0 JW |
63 | using _Type = _Tp[_Nm]; |
64 | using _Is_swappable = __is_swappable<_Tp>; | |
65 | using _Is_nothrow_swappable = __is_nothrow_swappable<_Tp>; | |
90f9c94e PC |
66 | }; |
67 | ||
68 | template<typename _Tp> | |
69 | struct __array_traits<_Tp, 0> | |
70 | { | |
ef8d5ac0 JW |
71 | // Empty type used instead of _Tp[0] for std::array<_Tp, 0>. |
72 | struct _Type | |
73 | { | |
74 | // Indexing is undefined. | |
8f6d25f1 | 75 | __attribute__((__always_inline__,__noreturn__)) |
ef8d5ac0 JW |
76 | _Tp& operator[](size_t) const noexcept { __builtin_trap(); } |
77 | ||
78 | // Conversion to a pointer produces a null pointer. | |
8f6d25f1 | 79 | __attribute__((__always_inline__)) |
56001fad | 80 | constexpr explicit operator _Tp*() const noexcept { return nullptr; } |
ef8d5ac0 JW |
81 | }; |
82 | ||
83 | using _Is_swappable = true_type; | |
84 | using _Is_nothrow_swappable = true_type; | |
90f9c94e PC |
85 | }; |
86 | ||
53dc5044 PC |
87 | /** |
88 | * @brief A standard container for storing a fixed size sequence of elements. | |
89 | * | |
90 | * @ingroup sequences | |
91 | * | |
92 | * Meets the requirements of a <a href="tables.html#65">container</a>, a | |
93 | * <a href="tables.html#66">reversible container</a>, and a | |
94 | * <a href="tables.html#67">sequence</a>. | |
95 | * | |
96 | * Sets support random access iterators. | |
97 | * | |
d632488a | 98 | * @tparam Tp Type of element. Required to be a complete type. |
cb0de9b6 | 99 | * @tparam Nm Number of elements. |
53dc5044 PC |
100 | */ |
101 | template<typename _Tp, std::size_t _Nm> | |
102 | struct array | |
103 | { | |
104 | typedef _Tp value_type; | |
a7d0c94e BK |
105 | typedef value_type* pointer; |
106 | typedef const value_type* const_pointer; | |
53dc5044 PC |
107 | typedef value_type& reference; |
108 | typedef const value_type& const_reference; | |
109 | typedef value_type* iterator; | |
110 | typedef const value_type* const_iterator; | |
111 | typedef std::size_t size_type; | |
112 | typedef std::ptrdiff_t difference_type; | |
113 | typedef std::reverse_iterator<iterator> reverse_iterator; | |
114 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; | |
115 | ||
116 | // Support for zero-sized arrays mandatory. | |
ef8d5ac0 | 117 | typename __array_traits<_Tp, _Nm>::_Type _M_elems; |
53dc5044 PC |
118 | |
119 | // No explicit construct/copy/destroy for aggregate type. | |
120 | ||
121 | // DR 776. | |
1c09b664 | 122 | _GLIBCXX20_CONSTEXPR void |
53dc5044 PC |
123 | fill(const value_type& __u) |
124 | { std::fill_n(begin(), size(), __u); } | |
125 | ||
1c09b664 | 126 | _GLIBCXX20_CONSTEXPR void |
53dc5044 | 127 | swap(array& __other) |
ef8d5ac0 | 128 | noexcept(__array_traits<_Tp, _Nm>::_Is_nothrow_swappable::value) |
53dc5044 PC |
129 | { std::swap_ranges(begin(), end(), __other.begin()); } |
130 | ||
131 | // Iterators. | |
756eabac | 132 | [[__gnu__::__const__, __nodiscard__]] |
06db9920 | 133 | _GLIBCXX17_CONSTEXPR iterator |
18eeaec4 | 134 | begin() noexcept |
a7d0c94e | 135 | { return iterator(data()); } |
53dc5044 | 136 | |
0d04fe49 | 137 | [[__nodiscard__]] |
06db9920 | 138 | _GLIBCXX17_CONSTEXPR const_iterator |
18eeaec4 | 139 | begin() const noexcept |
a7d0c94e | 140 | { return const_iterator(data()); } |
53dc5044 | 141 | |
756eabac | 142 | [[__gnu__::__const__, __nodiscard__]] |
06db9920 | 143 | _GLIBCXX17_CONSTEXPR iterator |
18eeaec4 | 144 | end() noexcept |
a7d0c94e | 145 | { return iterator(data() + _Nm); } |
53dc5044 | 146 | |
0d04fe49 | 147 | [[__nodiscard__]] |
06db9920 | 148 | _GLIBCXX17_CONSTEXPR const_iterator |
18eeaec4 | 149 | end() const noexcept |
a7d0c94e | 150 | { return const_iterator(data() + _Nm); } |
53dc5044 | 151 | |
756eabac | 152 | [[__gnu__::__const__, __nodiscard__]] |
06db9920 | 153 | _GLIBCXX17_CONSTEXPR reverse_iterator |
18eeaec4 | 154 | rbegin() noexcept |
53dc5044 PC |
155 | { return reverse_iterator(end()); } |
156 | ||
0d04fe49 | 157 | [[__nodiscard__]] |
06db9920 | 158 | _GLIBCXX17_CONSTEXPR const_reverse_iterator |
18eeaec4 | 159 | rbegin() const noexcept |
53dc5044 PC |
160 | { return const_reverse_iterator(end()); } |
161 | ||
756eabac | 162 | [[__gnu__::__const__, __nodiscard__]] |
06db9920 | 163 | _GLIBCXX17_CONSTEXPR reverse_iterator |
18eeaec4 | 164 | rend() noexcept |
53dc5044 PC |
165 | { return reverse_iterator(begin()); } |
166 | ||
0d04fe49 | 167 | [[__nodiscard__]] |
06db9920 | 168 | _GLIBCXX17_CONSTEXPR const_reverse_iterator |
18eeaec4 | 169 | rend() const noexcept |
53dc5044 PC |
170 | { return const_reverse_iterator(begin()); } |
171 | ||
0d04fe49 | 172 | [[__nodiscard__]] |
06db9920 | 173 | _GLIBCXX17_CONSTEXPR const_iterator |
18eeaec4 | 174 | cbegin() const noexcept |
90f9c94e | 175 | { return const_iterator(data()); } |
53dc5044 | 176 | |
0d04fe49 | 177 | [[__nodiscard__]] |
06db9920 | 178 | _GLIBCXX17_CONSTEXPR const_iterator |
18eeaec4 | 179 | cend() const noexcept |
90f9c94e | 180 | { return const_iterator(data() + _Nm); } |
53dc5044 | 181 | |
0d04fe49 | 182 | [[__nodiscard__]] |
06db9920 | 183 | _GLIBCXX17_CONSTEXPR const_reverse_iterator |
18eeaec4 | 184 | crbegin() const noexcept |
53dc5044 PC |
185 | { return const_reverse_iterator(end()); } |
186 | ||
0d04fe49 | 187 | [[__nodiscard__]] |
06db9920 | 188 | _GLIBCXX17_CONSTEXPR const_reverse_iterator |
18eeaec4 | 189 | crend() const noexcept |
53dc5044 PC |
190 | { return const_reverse_iterator(begin()); } |
191 | ||
192 | // Capacity. | |
22399ad6 | 193 | [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]] |
33ac58d5 | 194 | constexpr size_type |
18eeaec4 | 195 | size() const noexcept { return _Nm; } |
53dc5044 | 196 | |
22399ad6 | 197 | [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]] |
33ac58d5 | 198 | constexpr size_type |
18eeaec4 | 199 | max_size() const noexcept { return _Nm; } |
53dc5044 | 200 | |
22399ad6 | 201 | [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]] |
0d04fe49 | 202 | constexpr bool |
18eeaec4 | 203 | empty() const noexcept { return size() == 0; } |
53dc5044 PC |
204 | |
205 | // Element access. | |
0d04fe49 | 206 | [[__nodiscard__]] |
06db9920 | 207 | _GLIBCXX17_CONSTEXPR reference |
b4efa80e | 208 | operator[](size_type __n) noexcept |
6db08247 FD |
209 | { |
210 | __glibcxx_requires_subscript(__n); | |
ef8d5ac0 | 211 | return _M_elems[__n]; |
6db08247 | 212 | } |
53dc5044 | 213 | |
0d04fe49 | 214 | [[__nodiscard__]] |
bfef3a71 BK |
215 | constexpr const_reference |
216 | operator[](size_type __n) const noexcept | |
6db08247 | 217 | { |
91cfacc4 | 218 | #if __cplusplus >= 201402L |
6db08247 | 219 | __glibcxx_requires_subscript(__n); |
91cfacc4 | 220 | #endif |
ef8d5ac0 | 221 | return _M_elems[__n]; |
6db08247 | 222 | } |
53dc5044 | 223 | |
06db9920 | 224 | _GLIBCXX17_CONSTEXPR reference |
53dc5044 PC |
225 | at(size_type __n) |
226 | { | |
227 | if (__n >= _Nm) | |
9779c871 PP |
228 | std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) " |
229 | ">= _Nm (which is %zu)"), | |
230 | __n, _Nm); | |
ef8d5ac0 | 231 | return _M_elems[__n]; |
53dc5044 PC |
232 | } |
233 | ||
bfef3a71 | 234 | constexpr const_reference |
53dc5044 PC |
235 | at(size_type __n) const |
236 | { | |
885e8121 JW |
237 | // Result of conditional expression must be an lvalue so use |
238 | // boolean ? lvalue : (throw-expr, lvalue) | |
ef8d5ac0 | 239 | return __n < _Nm ? _M_elems[__n] |
9779c871 PP |
240 | : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) " |
241 | ">= _Nm (which is %zu)"), | |
242 | __n, _Nm), | |
ef8d5ac0 | 243 | _M_elems[__n]); |
9bc13c23 | 244 | } |
53dc5044 | 245 | |
0d04fe49 | 246 | [[__nodiscard__]] |
06db9920 | 247 | _GLIBCXX17_CONSTEXPR reference |
b4efa80e | 248 | front() noexcept |
6db08247 FD |
249 | { |
250 | __glibcxx_requires_nonempty(); | |
56001fad | 251 | return _M_elems[(size_type)0]; |
6db08247 | 252 | } |
53dc5044 | 253 | |
0d04fe49 | 254 | [[__nodiscard__]] |
33ac58d5 | 255 | constexpr const_reference |
b4efa80e | 256 | front() const noexcept |
6db08247 | 257 | { |
91cfacc4 | 258 | #if __cplusplus >= 201402L |
6db08247 | 259 | __glibcxx_requires_nonempty(); |
91cfacc4 | 260 | #endif |
56001fad | 261 | return _M_elems[(size_type)0]; |
6db08247 | 262 | } |
53dc5044 | 263 | |
0d04fe49 | 264 | [[__nodiscard__]] |
06db9920 | 265 | _GLIBCXX17_CONSTEXPR reference |
b4efa80e | 266 | back() noexcept |
6db08247 FD |
267 | { |
268 | __glibcxx_requires_nonempty(); | |
ef8d5ac0 | 269 | return _M_elems[_Nm - 1]; |
6db08247 | 270 | } |
53dc5044 | 271 | |
0d04fe49 | 272 | [[__nodiscard__]] |
33ac58d5 | 273 | constexpr const_reference |
b4efa80e | 274 | back() const noexcept |
33ac58d5 | 275 | { |
91cfacc4 | 276 | #if __cplusplus >= 201402L |
6db08247 | 277 | __glibcxx_requires_nonempty(); |
91cfacc4 | 278 | #endif |
ef8d5ac0 | 279 | return _M_elems[_Nm - 1]; |
a9ba8ba5 | 280 | } |
53dc5044 | 281 | |
22399ad6 | 282 | [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]] |
06db9920 | 283 | _GLIBCXX17_CONSTEXPR pointer |
18eeaec4 | 284 | data() noexcept |
56001fad | 285 | { return static_cast<pointer>(_M_elems); } |
53dc5044 | 286 | |
0d04fe49 | 287 | [[__nodiscard__]] |
06db9920 | 288 | _GLIBCXX17_CONSTEXPR const_pointer |
18eeaec4 | 289 | data() const noexcept |
56001fad | 290 | { return static_cast<const_pointer>(_M_elems); } |
53dc5044 PC |
291 | }; |
292 | ||
af181c91 JW |
293 | #if __cpp_deduction_guides >= 201606 |
294 | template<typename _Tp, typename... _Up> | |
295 | array(_Tp, _Up...) | |
296 | -> array<enable_if_t<(is_same_v<_Tp, _Up> && ...), _Tp>, | |
297 | 1 + sizeof...(_Up)>; | |
298 | #endif | |
299 | ||
53dc5044 PC |
300 | // Array comparisons. |
301 | template<typename _Tp, std::size_t _Nm> | |
0d04fe49 | 302 | [[__nodiscard__]] |
3a66e68a | 303 | _GLIBCXX20_CONSTEXPR |
33ac58d5 | 304 | inline bool |
53dc5044 | 305 | operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) |
3aeb2ede | 306 | { return std::__equal_aux1(__one.begin(), __one.end(), __two.begin()); } |
53dc5044 | 307 | |
083b7f28 | 308 | #if __cpp_lib_three_way_comparison // C++ >= 20 && lib_concepts |
3a4cc628 | 309 | template<typename _Tp, size_t _Nm> |
0d04fe49 | 310 | [[nodiscard]] |
3a4cc628 JW |
311 | constexpr __detail::__synth3way_t<_Tp> |
312 | operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b) | |
313 | { | |
2f983fa6 | 314 | if constexpr (_Nm && __is_memcmp_ordered<_Tp>::__value) |
74d14778 | 315 | if (!std::__is_constant_evaluated()) |
2f983fa6 JW |
316 | { |
317 | constexpr size_t __n = _Nm * sizeof(_Tp); | |
318 | return __builtin_memcmp(__a.data(), __b.data(), __n) <=> 0; | |
319 | } | |
2f983fa6 JW |
320 | |
321 | for (size_t __i = 0; __i < _Nm; ++__i) | |
3a4cc628 | 322 | { |
2f983fa6 JW |
323 | auto __c = __detail::__synth3way(__a[__i], __b[__i]); |
324 | if (__c != 0) | |
325 | return __c; | |
3a4cc628 JW |
326 | } |
327 | return strong_ordering::equal; | |
328 | } | |
329 | #else | |
53dc5044 | 330 | template<typename _Tp, std::size_t _Nm> |
0d04fe49 | 331 | [[__nodiscard__]] |
3a66e68a | 332 | _GLIBCXX20_CONSTEXPR |
53dc5044 PC |
333 | inline bool |
334 | operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
335 | { return !(__one == __two); } | |
336 | ||
337 | template<typename _Tp, std::size_t _Nm> | |
0d04fe49 | 338 | [[__nodiscard__]] |
3a66e68a | 339 | _GLIBCXX20_CONSTEXPR |
53dc5044 PC |
340 | inline bool |
341 | operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b) | |
33ac58d5 | 342 | { |
53dc5044 | 343 | return std::lexicographical_compare(__a.begin(), __a.end(), |
33ac58d5 | 344 | __b.begin(), __b.end()); |
53dc5044 PC |
345 | } |
346 | ||
347 | template<typename _Tp, std::size_t _Nm> | |
0d04fe49 | 348 | [[__nodiscard__]] |
3a66e68a | 349 | _GLIBCXX20_CONSTEXPR |
53dc5044 PC |
350 | inline bool |
351 | operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
352 | { return __two < __one; } | |
353 | ||
354 | template<typename _Tp, std::size_t _Nm> | |
0d04fe49 | 355 | [[__nodiscard__]] |
3a66e68a | 356 | _GLIBCXX20_CONSTEXPR |
53dc5044 PC |
357 | inline bool |
358 | operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
359 | { return !(__one > __two); } | |
360 | ||
361 | template<typename _Tp, std::size_t _Nm> | |
0d04fe49 | 362 | [[__nodiscard__]] |
3a66e68a | 363 | _GLIBCXX20_CONSTEXPR |
53dc5044 PC |
364 | inline bool |
365 | operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two) | |
366 | { return !(__one < __two); } | |
3a4cc628 | 367 | #endif // three_way_comparison && concepts |
53dc5044 | 368 | |
18eeaec4 | 369 | // Specialized algorithms. |
53dc5044 | 370 | template<typename _Tp, std::size_t _Nm> |
1c09b664 | 371 | _GLIBCXX20_CONSTEXPR |
6b9539e2 DK |
372 | inline |
373 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 | |
374 | // Constrained free swap overload, see p0185r1 | |
ef8d5ac0 | 375 | __enable_if_t<__array_traits<_Tp, _Nm>::_Is_swappable::value> |
6b9539e2 DK |
376 | #else |
377 | void | |
378 | #endif | |
53dc5044 | 379 | swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two) |
18eeaec4 | 380 | noexcept(noexcept(__one.swap(__two))) |
53dc5044 PC |
381 | { __one.swap(__two); } |
382 | ||
a2863bde VV |
383 | #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 |
384 | template<typename _Tp, std::size_t _Nm> | |
ef8d5ac0 | 385 | __enable_if_t<!__array_traits<_Tp, _Nm>::_Is_swappable::value> |
a2863bde VV |
386 | swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete; |
387 | #endif | |
388 | ||
53dc5044 | 389 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> |
0d04fe49 | 390 | [[__nodiscard__]] |
9480716c | 391 | constexpr _Tp& |
18eeaec4 | 392 | get(array<_Tp, _Nm>& __arr) noexcept |
9771644a | 393 | { |
3c040fa4 | 394 | static_assert(_Int < _Nm, "array index is within bounds"); |
ef8d5ac0 | 395 | return __arr._M_elems[_Int]; |
9771644a | 396 | } |
53dc5044 | 397 | |
18eeaec4 | 398 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> |
0d04fe49 | 399 | [[__nodiscard__]] |
9480716c | 400 | constexpr _Tp&& |
18eeaec4 | 401 | get(array<_Tp, _Nm>&& __arr) noexcept |
9771644a | 402 | { |
3c040fa4 | 403 | static_assert(_Int < _Nm, "array index is within bounds"); |
6db08247 | 404 | return std::move(std::get<_Int>(__arr)); |
9771644a | 405 | } |
18eeaec4 | 406 | |
53dc5044 | 407 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> |
0d04fe49 | 408 | [[__nodiscard__]] |
9480716c | 409 | constexpr const _Tp& |
18eeaec4 | 410 | get(const array<_Tp, _Nm>& __arr) noexcept |
9771644a | 411 | { |
3c040fa4 | 412 | static_assert(_Int < _Nm, "array index is within bounds"); |
ef8d5ac0 | 413 | return __arr._M_elems[_Int]; |
9771644a | 414 | } |
53dc5044 | 415 | |
ccbbf8df | 416 | template<std::size_t _Int, typename _Tp, std::size_t _Nm> |
0d04fe49 | 417 | [[__nodiscard__]] |
ccbbf8df VV |
418 | constexpr const _Tp&& |
419 | get(const array<_Tp, _Nm>&& __arr) noexcept | |
420 | { | |
421 | static_assert(_Int < _Nm, "array index is within bounds"); | |
6db08247 | 422 | return std::move(std::get<_Int>(__arr)); |
ccbbf8df VV |
423 | } |
424 | ||
083b7f28 | 425 | #ifdef __cpp_lib_to_array // C++ >= 20 && __cpp_generic_lambdas >= 201707L |
cb0de9b6 | 426 | template<typename _Tp, size_t _Nm> |
0d04fe49 | 427 | [[nodiscard]] |
cb0de9b6 JW |
428 | constexpr array<remove_cv_t<_Tp>, _Nm> |
429 | to_array(_Tp (&__a)[_Nm]) | |
430 | noexcept(is_nothrow_constructible_v<_Tp, _Tp&>) | |
431 | { | |
432 | static_assert(!is_array_v<_Tp>); | |
433 | static_assert(is_constructible_v<_Tp, _Tp&>); | |
434 | if constexpr (is_constructible_v<_Tp, _Tp&>) | |
960de5dd | 435 | { |
510ce5ee JW |
436 | if constexpr (is_trivially_copyable_v<_Tp> |
437 | && is_trivially_default_constructible_v<_Tp> | |
438 | && is_copy_assignable_v<_Tp>) | |
960de5dd JW |
439 | { |
440 | array<remove_cv_t<_Tp>, _Nm> __arr; | |
441 | if (!__is_constant_evaluated() && _Nm != 0) | |
77cf3773 | 442 | __builtin_memcpy((void*)__arr.data(), (void*)__a, sizeof(__a)); |
960de5dd JW |
443 | else |
444 | for (size_t __i = 0; __i < _Nm; ++__i) | |
445 | __arr._M_elems[__i] = __a[__i]; | |
446 | return __arr; | |
447 | } | |
448 | else | |
449 | return [&__a]<size_t... _Idx>(index_sequence<_Idx...>) { | |
450 | return array<remove_cv_t<_Tp>, _Nm>{{ __a[_Idx]... }}; | |
451 | }(make_index_sequence<_Nm>{}); | |
452 | } | |
453 | else | |
454 | __builtin_unreachable(); // FIXME: see PR c++/91388 | |
cb0de9b6 JW |
455 | } |
456 | ||
457 | template<typename _Tp, size_t _Nm> | |
0d04fe49 | 458 | [[nodiscard]] |
cb0de9b6 JW |
459 | constexpr array<remove_cv_t<_Tp>, _Nm> |
460 | to_array(_Tp (&&__a)[_Nm]) | |
461 | noexcept(is_nothrow_move_constructible_v<_Tp>) | |
462 | { | |
463 | static_assert(!is_array_v<_Tp>); | |
464 | static_assert(is_move_constructible_v<_Tp>); | |
465 | if constexpr (is_move_constructible_v<_Tp>) | |
960de5dd | 466 | { |
510ce5ee JW |
467 | if constexpr (is_trivially_copyable_v<_Tp> |
468 | && is_trivially_default_constructible_v<_Tp> | |
469 | && is_copy_assignable_v<_Tp>) | |
960de5dd JW |
470 | { |
471 | array<remove_cv_t<_Tp>, _Nm> __arr; | |
472 | if (!__is_constant_evaluated() && _Nm != 0) | |
77cf3773 | 473 | __builtin_memcpy((void*)__arr.data(), (void*)__a, sizeof(__a)); |
960de5dd JW |
474 | else |
475 | for (size_t __i = 0; __i < _Nm; ++__i) | |
b4f1e4a6 | 476 | __arr._M_elems[__i] = __a[__i]; |
960de5dd JW |
477 | return __arr; |
478 | } | |
479 | else | |
480 | return [&__a]<size_t... _Idx>(index_sequence<_Idx...>) { | |
481 | return array<remove_cv_t<_Tp>, _Nm>{{ std::move(__a[_Idx])... }}; | |
482 | }(make_index_sequence<_Nm>{}); | |
483 | } | |
484 | else | |
485 | __builtin_unreachable(); // FIXME: see PR c++/91388 | |
cb0de9b6 | 486 | } |
083b7f28 | 487 | #endif // __cpp_lib_to_array |
cb0de9b6 | 488 | |
741aacaf PC |
489 | // Tuple interface to class template array. |
490 | ||
3a004764 | 491 | /// Partial specialization for std::array |
261d5a4a | 492 | template<typename _Tp, size_t _Nm> |
6db08247 | 493 | struct tuple_size<array<_Tp, _Nm>> |
261d5a4a | 494 | : public integral_constant<size_t, _Nm> { }; |
741aacaf | 495 | |
3a004764 | 496 | /// Partial specialization for std::array |
261d5a4a JW |
497 | template<size_t _Ind, typename _Tp, size_t _Nm> |
498 | struct tuple_element<_Ind, array<_Tp, _Nm>> | |
741aacaf | 499 | { |
261d5a4a JW |
500 | static_assert(_Ind < _Nm, "array index is in range"); |
501 | using type = _Tp; | |
741aacaf PC |
502 | }; |
503 | ||
09aab7e6 JW |
504 | #if __cplusplus >= 201703L |
505 | template<typename _Tp, size_t _Nm> | |
506 | inline constexpr size_t tuple_size_v<array<_Tp, _Nm>> = _Nm; | |
507 | ||
508 | template<typename _Tp, size_t _Nm> | |
509 | inline constexpr size_t tuple_size_v<const array<_Tp, _Nm>> = _Nm; | |
510 | #endif | |
511 | ||
261d5a4a | 512 | template<typename _Tp, size_t _Nm> |
6db08247 | 513 | struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type |
7d17de7f FD |
514 | { }; |
515 | ||
741aacaf PC |
516 | _GLIBCXX_END_NAMESPACE_VERSION |
517 | } // namespace std | |
518 | ||
734f5023 | 519 | #endif // C++11 |
57317d2a | 520 | |
4514bed6 | 521 | #endif // _GLIBCXX_ARRAY |