libstdc++: array Source File

libstdc++

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