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