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_nonmember_container_access
53#define __glibcxx_want_to_array
54#include <bits/version.h>
55
56namespace std _GLIBCXX_VISIBILITY(default)
57{
58_GLIBCXX_BEGIN_NAMESPACE_VERSION
59
60 template<typename _Tp, size_t _Nm>
61 struct __array_traits
62 {
63 using _Type = _Tp[_Nm];
64 using _Is_swappable = __is_swappable<_Tp>;
65 using _Is_nothrow_swappable = __is_nothrow_swappable<_Tp>;
66 };
67
68 template<typename _Tp>
69 struct __array_traits<_Tp, 0>
70 {
71 // Empty type used instead of _Tp[0] for std::array<_Tp, 0>.
72 struct _Type
73 {
74 // Indexing is undefined.
75 __attribute__((__always_inline__,__noreturn__))
76 _Tp& operator[](size_t) const noexcept { __builtin_trap(); }
77
78 // Conversion to a pointer produces a null pointer.
79 __attribute__((__always_inline__))
80 constexpr explicit operator _Tp*() const noexcept { return nullptr; }
81 };
82
83 using _Is_swappable = true_type;
84 using _Is_nothrow_swappable = true_type;
85 };
86
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 *
98 * @tparam Tp Type of element. Required to be a complete type.
99 * @tparam Nm Number of elements.
100 */
101 template<typename _Tp, std::size_t _Nm>
102 struct array
103 {
104 typedef _Tp value_type;
105 typedef value_type* pointer;
106 typedef const value_type* const_pointer;
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.
117 typename __array_traits<_Tp, _Nm>::_Type _M_elems;
118
119 // No explicit construct/copy/destroy for aggregate type.
120
121 // DR 776.
122 _GLIBCXX20_CONSTEXPR void
123 fill(const value_type& __u)
124 { std::fill_n(begin(), size(), __u); }
125
126 _GLIBCXX20_CONSTEXPR void
127 swap(array& __other)
128 noexcept(__array_traits<_Tp, _Nm>::_Is_nothrow_swappable::value)
129 { std::swap_ranges(begin(), end(), __other.begin()); }
130
131 // Iterators.
132 [[__gnu__::__const__, __nodiscard__]]
133 _GLIBCXX17_CONSTEXPR iterator
134 begin() noexcept
135 { return iterator(data()); }
136
137 [[__nodiscard__]]
138 _GLIBCXX17_CONSTEXPR const_iterator
139 begin() const noexcept
140 { return const_iterator(data()); }
141
142 [[__gnu__::__const__, __nodiscard__]]
143 _GLIBCXX17_CONSTEXPR iterator
144 end() noexcept
145 { return iterator(data() + _Nm); }
146
147 [[__nodiscard__]]
148 _GLIBCXX17_CONSTEXPR const_iterator
149 end() const noexcept
150 { return const_iterator(data() + _Nm); }
151
152 [[__gnu__::__const__, __nodiscard__]]
153 _GLIBCXX17_CONSTEXPR reverse_iterator
154 rbegin() noexcept
155 { return reverse_iterator(end()); }
156
157 [[__nodiscard__]]
158 _GLIBCXX17_CONSTEXPR const_reverse_iterator
159 rbegin() const noexcept
160 { return const_reverse_iterator(end()); }
161
162 [[__gnu__::__const__, __nodiscard__]]
163 _GLIBCXX17_CONSTEXPR reverse_iterator
164 rend() noexcept
165 { return reverse_iterator(begin()); }
166
167 [[__nodiscard__]]
168 _GLIBCXX17_CONSTEXPR const_reverse_iterator
169 rend() const noexcept
170 { return const_reverse_iterator(begin()); }
171
172 [[__nodiscard__]]
173 _GLIBCXX17_CONSTEXPR const_iterator
174 cbegin() const noexcept
175 { return const_iterator(data()); }
176
177 [[__nodiscard__]]
178 _GLIBCXX17_CONSTEXPR const_iterator
179 cend() const noexcept
180 { return const_iterator(data() + _Nm); }
181
182 [[__nodiscard__]]
183 _GLIBCXX17_CONSTEXPR const_reverse_iterator
184 crbegin() const noexcept
185 { return const_reverse_iterator(end()); }
186
187 [[__nodiscard__]]
188 _GLIBCXX17_CONSTEXPR const_reverse_iterator
189 crend() const noexcept
190 { return const_reverse_iterator(begin()); }
191
192 // Capacity.
193 [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
194 constexpr size_type
195 size() const noexcept { return _Nm; }
196
197 [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
198 constexpr size_type
199 max_size() const noexcept { return _Nm; }
200
201 [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
202 constexpr bool
203 empty() const noexcept { return size() == 0; }
204
205 // Element access.
206 [[__nodiscard__]]
207 _GLIBCXX17_CONSTEXPR reference
208 operator[](size_type __n) noexcept
209 {
210 __glibcxx_requires_subscript(__n);
211 return _M_elems[__n];
212 }
213
214 [[__nodiscard__]]
215 constexpr const_reference
216 operator[](size_type __n) const noexcept
217 {
218#if __cplusplus >= 201402L
219 __glibcxx_requires_subscript(__n);
220#endif
221 return _M_elems[__n];
222 }
223
224 _GLIBCXX17_CONSTEXPR reference
225 at(size_type __n)
226 {
227 if (__n >= _Nm)
228 std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
229 ">= _Nm (which is %zu)"),
230 __n, _Nm);
231 return _M_elems[__n];
232 }
233
234 constexpr const_reference
235 at(size_type __n) const
236 {
237 // Result of conditional expression must be an lvalue so use
238 // boolean ? lvalue : (throw-expr, lvalue)
239 return __n < _Nm ? _M_elems[__n]
240 : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
241 ">= _Nm (which is %zu)"),
242 __n, _Nm),
243 _M_elems[__n]);
244 }
245
246 [[__nodiscard__]]
247 _GLIBCXX17_CONSTEXPR reference
248 front() noexcept
249 {
250 __glibcxx_requires_nonempty();
251 return _M_elems[(size_type)0];
252 }
253
254 [[__nodiscard__]]
255 constexpr const_reference
256 front() const noexcept
257 {
258#if __cplusplus >= 201402L
259 __glibcxx_requires_nonempty();
260#endif
261 return _M_elems[(size_type)0];
262 }
263
264 [[__nodiscard__]]
265 _GLIBCXX17_CONSTEXPR reference
266 back() noexcept
267 {
268 __glibcxx_requires_nonempty();
269 return _M_elems[_Nm - 1];
270 }
271
272 [[__nodiscard__]]
273 constexpr const_reference
274 back() const noexcept
275 {
276#if __cplusplus >= 201402L
277 __glibcxx_requires_nonempty();
278#endif
279 return _M_elems[_Nm - 1];
280 }
281
282 [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
283 _GLIBCXX17_CONSTEXPR pointer
284 data() noexcept
285 { return static_cast<pointer>(_M_elems); }
286
287 [[__nodiscard__]]
288 _GLIBCXX17_CONSTEXPR const_pointer
289 data() const noexcept
290 { return static_cast<const_pointer>(_M_elems); }
291 };
292
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
300 // Array comparisons.
301 template<typename _Tp, std::size_t _Nm>
302 [[__nodiscard__]]
303 _GLIBCXX20_CONSTEXPR
304 inline bool
305 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
306 { return std::__equal_aux1(__one.begin(), __one.end(), __two.begin()); }
307
308#if __cpp_lib_three_way_comparison // C++ >= 20 && lib_concepts
309 template<typename _Tp, size_t _Nm>
310 [[nodiscard]]
311 constexpr __detail::__synth3way_t<_Tp>
312 operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
313 {
314 if constexpr (_Nm && __is_memcmp_ordered<_Tp>::__value)
315 if (!std::__is_constant_evaluated())
316 {
317 constexpr size_t __n = _Nm * sizeof(_Tp);
318 return __builtin_memcmp(__a.data(), __b.data(), __n) <=> 0;
319 }
320
321 for (size_t __i = 0; __i < _Nm; ++__i)
322 {
323 auto __c = __detail::__synth3way(__a[__i], __b[__i]);
324 if (__c != 0)
325 return __c;
326 }
327 return strong_ordering::equal;
328 }
329#else
330 template<typename _Tp, std::size_t _Nm>
331 [[__nodiscard__]]
332 _GLIBCXX20_CONSTEXPR
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>
338 [[__nodiscard__]]
339 _GLIBCXX20_CONSTEXPR
340 inline bool
341 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
342 {
343 return std::lexicographical_compare(__a.begin(), __a.end(),
344 __b.begin(), __b.end());
345 }
346
347 template<typename _Tp, std::size_t _Nm>
348 [[__nodiscard__]]
349 _GLIBCXX20_CONSTEXPR
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>
355 [[__nodiscard__]]
356 _GLIBCXX20_CONSTEXPR
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>
362 [[__nodiscard__]]
363 _GLIBCXX20_CONSTEXPR
364 inline bool
365 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
366 { return !(__one < __two); }
367#endif // three_way_comparison && concepts
368
369 // Specialized algorithms.
370 template<typename _Tp, std::size_t _Nm>
371 _GLIBCXX20_CONSTEXPR
372 inline
373#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
374 // Constrained free swap overload, see p0185r1
375 __enable_if_t<__array_traits<_Tp, _Nm>::_Is_swappable::value>
376#else
377 void
378#endif
379 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
380 noexcept(noexcept(__one.swap(__two)))
381 { __one.swap(__two); }
382
383#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
384 // _GLIBCXX_RESOLVE_LIB_DEFECTS
385 // 2766. Swapping non-swappable types
386 template<typename _Tp, std::size_t _Nm>
387 __enable_if_t<!__array_traits<_Tp, _Nm>::_Is_swappable::value>
388 swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete;
389#endif
390
391 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
392 [[__nodiscard__]]
393 constexpr _Tp&
394 get(array<_Tp, _Nm>& __arr) noexcept
395 {
396 static_assert(_Int < _Nm, "array index is within bounds");
397 return __arr._M_elems[_Int];
398 }
399
400 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
401 [[__nodiscard__]]
402 constexpr _Tp&&
403 get(array<_Tp, _Nm>&& __arr) noexcept
404 {
405 static_assert(_Int < _Nm, "array index is within bounds");
406 return std::move(std::get<_Int>(__arr));
407 }
408
409 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
410 [[__nodiscard__]]
411 constexpr const _Tp&
412 get(const array<_Tp, _Nm>& __arr) noexcept
413 {
414 static_assert(_Int < _Nm, "array index is within bounds");
415 return __arr._M_elems[_Int];
416 }
417
418 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
419 [[__nodiscard__]]
420 constexpr const _Tp&&
421 get(const array<_Tp, _Nm>&& __arr) noexcept
422 {
423 static_assert(_Int < _Nm, "array index is within bounds");
424 return std::move(std::get<_Int>(__arr));
425 }
426
427#ifdef __cpp_lib_to_array // C++ >= 20 && __cpp_generic_lambdas >= 201707L
428 template<typename _Tp, size_t _Nm>
429 [[nodiscard]]
430 constexpr array<remove_cv_t<_Tp>, _Nm>
431 to_array(_Tp (&__a)[_Nm])
432 noexcept(is_nothrow_constructible_v<_Tp, _Tp&>)
433 {
434 static_assert(!is_array_v<_Tp>);
435 static_assert(is_constructible_v<_Tp, _Tp&>);
436 if constexpr (is_constructible_v<_Tp, _Tp&>)
437 {
438 if constexpr (is_trivially_copyable_v<_Tp>
439 && is_trivially_default_constructible_v<_Tp>
440 && is_copy_assignable_v<_Tp>)
441 {
442 array<remove_cv_t<_Tp>, _Nm> __arr;
443 if (!__is_constant_evaluated() && _Nm != 0)
444 __builtin_memcpy((void*)__arr.data(), (void*)__a, sizeof(__a));
445 else
446 for (size_t __i = 0; __i < _Nm; ++__i)
447 __arr._M_elems[__i] = __a[__i];
448 return __arr;
449 }
450 else
451 return [&__a]<size_t... _Idx>(index_sequence<_Idx...>) {
452 return array<remove_cv_t<_Tp>, _Nm>{{ __a[_Idx]... }};
453 }(make_index_sequence<_Nm>{});
454 }
455 else
456 __builtin_unreachable(); // FIXME: see PR c++/91388
457 }
458
459 template<typename _Tp, size_t _Nm>
460 [[nodiscard]]
461 constexpr array<remove_cv_t<_Tp>, _Nm>
462 to_array(_Tp (&&__a)[_Nm])
463 noexcept(is_nothrow_move_constructible_v<_Tp>)
464 {
465 static_assert(!is_array_v<_Tp>);
466 static_assert(is_move_constructible_v<_Tp>);
467 if constexpr (is_move_constructible_v<_Tp>)
468 {
469 if constexpr (is_trivially_copyable_v<_Tp>
470 && is_trivially_default_constructible_v<_Tp>
471 && is_copy_assignable_v<_Tp>)
472 {
473 array<remove_cv_t<_Tp>, _Nm> __arr;
474 if (!__is_constant_evaluated() && _Nm != 0)
475 __builtin_memcpy((void*)__arr.data(), (void*)__a, sizeof(__a));
476 else
477 for (size_t __i = 0; __i < _Nm; ++__i)
478 __arr._M_elems[__i] = __a[__i];
479 return __arr;
480 }
481 else
482 return [&__a]<size_t... _Idx>(index_sequence<_Idx...>) {
483 return array<remove_cv_t<_Tp>, _Nm>{{ std::move(__a[_Idx])... }};
484 }(make_index_sequence<_Nm>{});
485 }
486 else
487 __builtin_unreachable(); // FIXME: see PR c++/91388
488 }
489#endif // __cpp_lib_to_array
490
491 // Tuple interface to class template array.
492
493 /// Partial specialization for std::array
494 template<typename _Tp, size_t _Nm>
495 struct tuple_size<array<_Tp, _Nm>>
496 : public integral_constant<size_t, _Nm> { };
497
498 /// Partial specialization for std::array
499 template<size_t _Ind, typename _Tp, size_t _Nm>
500 struct tuple_element<_Ind, array<_Tp, _Nm>>
501 {
502 static_assert(_Ind < _Nm, "array index is in range");
503 using type = _Tp;
504 };
505
506#if __cplusplus >= 201703L
507 template<typename _Tp, size_t _Nm>
508 inline constexpr size_t tuple_size_v<array<_Tp, _Nm>> = _Nm;
509
510 template<typename _Tp, size_t _Nm>
511 inline constexpr size_t tuple_size_v<const array<_Tp, _Nm>> = _Nm;
512#endif
513
514 template<typename _Tp, size_t _Nm>
515 struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
516 { };
517
518_GLIBCXX_END_NAMESPACE_VERSION
519} // namespace std
520
521#endif // C++11
522
523#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:2944
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:559
std::index_sequence
integer_sequence< size_t, _Idx... > index_sequence
Alias template index_sequence.
Definition utility.h:555
std::array
A standard container for storing a fixed size sequence of elements.
Definition array:103
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