3// Copyright (C) 2007-2023 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25/** @file include/array
26 * This is a Standard C++ Library header.
30#define _GLIBCXX_ARRAY 1
32#pragma GCC system_header
34#if __cplusplus < 201103L
35# include <bits/c++0x_warning.h>
39#include <initializer_list>
42#include <bits/functexcept.h>
43#include <bits/stl_algobase.h>
44#include <bits/range_access.h> // std::begin, std::end etc.
45#include <bits/utility.h> // std::index_sequence, std::tuple_size
46#include <debug/assertions.h>
48namespace std _GLIBCXX_VISIBILITY(default)
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
52 template<typename _Tp, size_t _Nm>
55 using _Type = _Tp[_Nm];
56 using _Is_swappable = __is_swappable<_Tp>;
57 using _Is_nothrow_swappable = __is_nothrow_swappable<_Tp>;
60 template<typename _Tp>
61 struct __array_traits<_Tp, 0>
63 // Empty type used instead of _Tp[0] for std::array<_Tp, 0>.
66 // Indexing is undefined.
67 __attribute__((__always_inline__,__noreturn__))
68 _Tp& operator[](size_t) const noexcept { __builtin_trap(); }
70 // Conversion to a pointer produces a null pointer.
71 __attribute__((__always_inline__))
72 constexpr operator _Tp*() const noexcept { return nullptr; }
75 using _Is_swappable = true_type;
76 using _Is_nothrow_swappable = true_type;
80 * @brief A standard container for storing a fixed size sequence of elements.
84 * Meets the requirements of a <a href="tables.html#65">container</a>, a
85 * <a href="tables.html#66">reversible container</a>, and a
86 * <a href="tables.html#67">sequence</a>.
88 * Sets support random access iterators.
90 * @tparam Tp Type of element. Required to be a complete type.
91 * @tparam Nm Number of elements.
93 template<typename _Tp, std::size_t _Nm>
96 typedef _Tp value_type;
97 typedef value_type* pointer;
98 typedef const value_type* const_pointer;
99 typedef value_type& reference;
100 typedef const value_type& const_reference;
101 typedef value_type* iterator;
102 typedef const value_type* const_iterator;
103 typedef std::size_t size_type;
104 typedef std::ptrdiff_t difference_type;
105 typedef std::reverse_iterator<iterator> reverse_iterator;
106 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
108 // Support for zero-sized arrays mandatory.
109 typename __array_traits<_Tp, _Nm>::_Type _M_elems;
111 // No explicit construct/copy/destroy for aggregate type.
114 _GLIBCXX20_CONSTEXPR void
115 fill(const value_type& __u)
116 { std::fill_n(begin(), size(), __u); }
118 _GLIBCXX20_CONSTEXPR void
120 noexcept(__array_traits<_Tp, _Nm>::_Is_nothrow_swappable::value)
121 { std::swap_ranges(begin(), end(), __other.begin()); }
124 [[__gnu__::__const__, __nodiscard__]]
125 _GLIBCXX17_CONSTEXPR iterator
127 { return iterator(data()); }
130 _GLIBCXX17_CONSTEXPR const_iterator
131 begin() const noexcept
132 { return const_iterator(data()); }
134 [[__gnu__::__const__, __nodiscard__]]
135 _GLIBCXX17_CONSTEXPR iterator
137 { return iterator(data() + _Nm); }
140 _GLIBCXX17_CONSTEXPR const_iterator
142 { return const_iterator(data() + _Nm); }
144 [[__gnu__::__const__, __nodiscard__]]
145 _GLIBCXX17_CONSTEXPR reverse_iterator
147 { return reverse_iterator(end()); }
150 _GLIBCXX17_CONSTEXPR const_reverse_iterator
151 rbegin() const noexcept
152 { return const_reverse_iterator(end()); }
154 [[__gnu__::__const__, __nodiscard__]]
155 _GLIBCXX17_CONSTEXPR reverse_iterator
157 { return reverse_iterator(begin()); }
160 _GLIBCXX17_CONSTEXPR const_reverse_iterator
161 rend() const noexcept
162 { return const_reverse_iterator(begin()); }
165 _GLIBCXX17_CONSTEXPR const_iterator
166 cbegin() const noexcept
167 { return const_iterator(data()); }
170 _GLIBCXX17_CONSTEXPR const_iterator
171 cend() const noexcept
172 { return const_iterator(data() + _Nm); }
175 _GLIBCXX17_CONSTEXPR const_reverse_iterator
176 crbegin() const noexcept
177 { return const_reverse_iterator(end()); }
180 _GLIBCXX17_CONSTEXPR const_reverse_iterator
181 crend() const noexcept
182 { return const_reverse_iterator(begin()); }
185 [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
187 size() const noexcept { return _Nm; }
189 [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
191 max_size() const noexcept { return _Nm; }
193 [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
195 empty() const noexcept { return size() == 0; }
199 _GLIBCXX17_CONSTEXPR reference
200 operator[](size_type __n) noexcept
202 __glibcxx_requires_subscript(__n);
203 return _M_elems[__n];
207 constexpr const_reference
208 operator[](size_type __n) const noexcept
210#if __cplusplus >= 201402L
211 __glibcxx_requires_subscript(__n);
213 return _M_elems[__n];
216 _GLIBCXX17_CONSTEXPR reference
220 std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
221 ">= _Nm (which is %zu)"),
223 return _M_elems[__n];
226 constexpr const_reference
227 at(size_type __n) const
229 // Result of conditional expression must be an lvalue so use
230 // boolean ? lvalue : (throw-expr, lvalue)
231 return __n < _Nm ? _M_elems[__n]
232 : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
233 ">= _Nm (which is %zu)"),
239 _GLIBCXX17_CONSTEXPR reference
242 __glibcxx_requires_nonempty();
247 constexpr const_reference
248 front() const noexcept
250#if __cplusplus >= 201402L
251 __glibcxx_requires_nonempty();
257 _GLIBCXX17_CONSTEXPR reference
260 __glibcxx_requires_nonempty();
261 return _M_elems[_Nm - 1];
265 constexpr const_reference
266 back() const noexcept
268#if __cplusplus >= 201402L
269 __glibcxx_requires_nonempty();
271 return _M_elems[_Nm - 1];
274 [[__nodiscard__, __gnu__::__const__, __gnu__::__always_inline__]]
275 _GLIBCXX17_CONSTEXPR pointer
280 _GLIBCXX17_CONSTEXPR const_pointer
281 data() const noexcept
285#if __cpp_deduction_guides >= 201606
286 template<typename _Tp, typename... _Up>
288 -> array<enable_if_t<(is_same_v<_Tp, _Up> && ...), _Tp>,
292 // Array comparisons.
293 template<typename _Tp, std::size_t _Nm>
297 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
298 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
300#if __cpp_lib_three_way_comparison && __cpp_lib_concepts
301 template<typename _Tp, size_t _Nm>
303 constexpr __detail::__synth3way_t<_Tp>
304 operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
306 if constexpr (_Nm && __is_memcmp_ordered<_Tp>::__value)
307 if (!std::__is_constant_evaluated())
309 constexpr size_t __n = _Nm * sizeof(_Tp);
310 return __builtin_memcmp(__a.data(), __b.data(), __n) <=> 0;
313 for (size_t __i = 0; __i < _Nm; ++__i)
315 auto __c = __detail::__synth3way(__a[__i], __b[__i]);
319 return strong_ordering::equal;
322 template<typename _Tp, std::size_t _Nm>
326 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
327 { return !(__one == __two); }
329 template<typename _Tp, std::size_t _Nm>
333 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
335 return std::lexicographical_compare(__a.begin(), __a.end(),
336 __b.begin(), __b.end());
339 template<typename _Tp, std::size_t _Nm>
343 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
344 { return __two < __one; }
346 template<typename _Tp, std::size_t _Nm>
350 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
351 { return !(__one > __two); }
353 template<typename _Tp, std::size_t _Nm>
357 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
358 { return !(__one < __two); }
359#endif // three_way_comparison && concepts
361 // Specialized algorithms.
362 template<typename _Tp, std::size_t _Nm>
365#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
366 // Constrained free swap overload, see p0185r1
367 __enable_if_t<__array_traits<_Tp, _Nm>::_Is_swappable::value>
371 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
372 noexcept(noexcept(__one.swap(__two)))
373 { __one.swap(__two); }
375#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
376 template<typename _Tp, std::size_t _Nm>
377 __enable_if_t<!__array_traits<_Tp, _Nm>::_Is_swappable::value>
378 swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete;
381 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
384 get(array<_Tp, _Nm>& __arr) noexcept
386 static_assert(_Int < _Nm, "array index is within bounds");
387 return __arr._M_elems[_Int];
390 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
393 get(array<_Tp, _Nm>&& __arr) noexcept
395 static_assert(_Int < _Nm, "array index is within bounds");
396 return std::move(std::get<_Int>(__arr));
399 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
402 get(const array<_Tp, _Nm>& __arr) noexcept
404 static_assert(_Int < _Nm, "array index is within bounds");
405 return __arr._M_elems[_Int];
408 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
410 constexpr const _Tp&&
411 get(const array<_Tp, _Nm>&& __arr) noexcept
413 static_assert(_Int < _Nm, "array index is within bounds");
414 return std::move(std::get<_Int>(__arr));
417#if __cplusplus > 201703L
418#define __cpp_lib_to_array 201907L
420 template<bool _Move = false, typename _Tp, size_t... _Idx>
421 constexpr array<remove_cv_t<_Tp>, sizeof...(_Idx)>
422 __to_array(_Tp (&__a)[sizeof...(_Idx)], index_sequence<_Idx...>)
425 return {{std::move(__a[_Idx])...}};
427 return {{__a[_Idx]...}};
430 template<typename _Tp, size_t _Nm>
432 constexpr array<remove_cv_t<_Tp>, _Nm>
433 to_array(_Tp (&__a)[_Nm])
434 noexcept(is_nothrow_constructible_v<_Tp, _Tp&>)
436 static_assert(!is_array_v<_Tp>);
437 static_assert(is_constructible_v<_Tp, _Tp&>);
438 if constexpr (is_constructible_v<_Tp, _Tp&>)
439 return __to_array(__a, make_index_sequence<_Nm>{});
440 __builtin_unreachable(); // FIXME: see PR c++/91388
443 template<typename _Tp, size_t _Nm>
445 constexpr array<remove_cv_t<_Tp>, _Nm>
446 to_array(_Tp (&&__a)[_Nm])
447 noexcept(is_nothrow_move_constructible_v<_Tp>)
449 static_assert(!is_array_v<_Tp>);
450 static_assert(is_move_constructible_v<_Tp>);
451 if constexpr (is_move_constructible_v<_Tp>)
452 return __to_array<1>(__a, make_index_sequence<_Nm>{});
453 __builtin_unreachable(); // FIXME: see PR c++/91388
457 // Tuple interface to class template array.
459 /// Partial specialization for std::array
460 template<typename _Tp, size_t _Nm>
461 struct tuple_size<array<_Tp, _Nm>>
462 : public integral_constant<size_t, _Nm> { };
464 /// Partial specialization for std::array
465 template<size_t _Ind, typename _Tp, size_t _Nm>
466 struct tuple_element<_Ind, array<_Tp, _Nm>>
468 static_assert(_Ind < _Nm, "array index is in range");
472#if __cplusplus >= 201703L
473 template<typename _Tp, size_t _Nm>
474 inline constexpr size_t tuple_size_v<array<_Tp, _Nm>> = _Nm;
476 template<typename _Tp, size_t _Nm>
477 inline constexpr size_t tuple_size_v<const array<_Tp, _Nm>> = _Nm;
480 template<typename _Tp, size_t _Nm>
481 struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
484_GLIBCXX_END_NAMESPACE_VERSION
489#endif // _GLIBCXX_ARRAY