3// Copyright (C) 2007-2021 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 <bits/functexcept.h>
40#include <bits/stl_algobase.h>
41#include <bits/range_access.h>
42#include <debug/assertions.h>
44namespace std _GLIBCXX_VISIBILITY(default)
46_GLIBCXX_BEGIN_NAMESPACE_VERSION
48 template<typename _Tp, std::size_t _Nm>
51 typedef _Tp _Type[_Nm];
52 typedef __is_swappable<_Tp> _Is_swappable;
53 typedef __is_nothrow_swappable<_Tp> _Is_nothrow_swappable;
56 _S_ref(const _Type& __t, std::size_t __n) noexcept
57 { return const_cast<_Tp&>(__t[__n]); }
60 _S_ptr(const _Type& __t) noexcept
61 { return const_cast<_Tp*>(__t); }
64 template<typename _Tp>
65 struct __array_traits<_Tp, 0>
68 typedef true_type _Is_swappable;
69 typedef true_type _Is_nothrow_swappable;
72 _S_ref(const _Type&, std::size_t) noexcept
73 { return *static_cast<_Tp*>(nullptr); }
76 _S_ptr(const _Type&) noexcept
81 * @brief A standard container for storing a fixed size sequence of elements.
85 * Meets the requirements of a <a href="tables.html#65">container</a>, a
86 * <a href="tables.html#66">reversible container</a>, and a
87 * <a href="tables.html#67">sequence</a>.
89 * Sets support random access iterators.
91 * @tparam Tp Type of element. Required to be a complete type.
92 * @tparam Nm Number of elements.
94 template<typename _Tp, std::size_t _Nm>
97 typedef _Tp value_type;
98 typedef value_type* pointer;
99 typedef const value_type* const_pointer;
100 typedef value_type& reference;
101 typedef const value_type& const_reference;
102 typedef value_type* iterator;
103 typedef const value_type* const_iterator;
104 typedef std::size_t size_type;
105 typedef std::ptrdiff_t difference_type;
106 typedef std::reverse_iterator<iterator> reverse_iterator;
107 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
109 // Support for zero-sized arrays mandatory.
110 typedef __array_traits<_Tp, _Nm> _AT_Type;
111 typename _AT_Type::_Type _M_elems;
113 // No explicit construct/copy/destroy for aggregate type.
116 _GLIBCXX20_CONSTEXPR void
117 fill(const value_type& __u)
118 { std::fill_n(begin(), size(), __u); }
120 _GLIBCXX20_CONSTEXPR void
122 noexcept(_AT_Type::_Is_nothrow_swappable::value)
123 { std::swap_ranges(begin(), end(), __other.begin()); }
126 _GLIBCXX17_CONSTEXPR iterator
128 { return iterator(data()); }
130 _GLIBCXX17_CONSTEXPR const_iterator
131 begin() const noexcept
132 { return const_iterator(data()); }
134 _GLIBCXX17_CONSTEXPR iterator
136 { return iterator(data() + _Nm); }
138 _GLIBCXX17_CONSTEXPR const_iterator
140 { return const_iterator(data() + _Nm); }
142 _GLIBCXX17_CONSTEXPR reverse_iterator
144 { return reverse_iterator(end()); }
146 _GLIBCXX17_CONSTEXPR const_reverse_iterator
147 rbegin() const noexcept
148 { return const_reverse_iterator(end()); }
150 _GLIBCXX17_CONSTEXPR reverse_iterator
152 { return reverse_iterator(begin()); }
154 _GLIBCXX17_CONSTEXPR const_reverse_iterator
155 rend() const noexcept
156 { return const_reverse_iterator(begin()); }
158 _GLIBCXX17_CONSTEXPR const_iterator
159 cbegin() const noexcept
160 { return const_iterator(data()); }
162 _GLIBCXX17_CONSTEXPR const_iterator
163 cend() const noexcept
164 { return const_iterator(data() + _Nm); }
166 _GLIBCXX17_CONSTEXPR const_reverse_iterator
167 crbegin() const noexcept
168 { return const_reverse_iterator(end()); }
170 _GLIBCXX17_CONSTEXPR const_reverse_iterator
171 crend() const noexcept
172 { return const_reverse_iterator(begin()); }
176 size() const noexcept { return _Nm; }
179 max_size() const noexcept { return _Nm; }
181 _GLIBCXX_NODISCARD constexpr bool
182 empty() const noexcept { return size() == 0; }
185 _GLIBCXX17_CONSTEXPR reference
186 operator[](size_type __n) noexcept
188 __glibcxx_requires_subscript(__n);
189 return _AT_Type::_S_ref(_M_elems, __n);
192 constexpr const_reference
193 operator[](size_type __n) const noexcept
195#if __cplusplus >= 201402L
196 __glibcxx_requires_subscript(__n);
198 return _AT_Type::_S_ref(_M_elems, __n);
201 _GLIBCXX17_CONSTEXPR reference
205 std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
206 ">= _Nm (which is %zu)"),
208 return _AT_Type::_S_ref(_M_elems, __n);
211 constexpr const_reference
212 at(size_type __n) const
214 // Result of conditional expression must be an lvalue so use
215 // boolean ? lvalue : (throw-expr, lvalue)
216 return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
217 : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
218 ">= _Nm (which is %zu)"),
220 _AT_Type::_S_ref(_M_elems, 0));
223 _GLIBCXX17_CONSTEXPR reference
226 __glibcxx_requires_nonempty();
230 constexpr const_reference
231 front() const noexcept
233#if __cplusplus >= 201402L
234 __glibcxx_requires_nonempty();
236 return _AT_Type::_S_ref(_M_elems, 0);
239 _GLIBCXX17_CONSTEXPR reference
242 __glibcxx_requires_nonempty();
243 return _Nm ? *(end() - 1) : *end();
246 constexpr const_reference
247 back() const noexcept
249#if __cplusplus >= 201402L
250 __glibcxx_requires_nonempty();
252 return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
253 : _AT_Type::_S_ref(_M_elems, 0);
256 _GLIBCXX17_CONSTEXPR pointer
258 { return _AT_Type::_S_ptr(_M_elems); }
260 _GLIBCXX17_CONSTEXPR const_pointer
261 data() const noexcept
262 { return _AT_Type::_S_ptr(_M_elems); }
265#if __cpp_deduction_guides >= 201606
266 template<typename _Tp, typename... _Up>
268 -> array<enable_if_t<(is_same_v<_Tp, _Up> && ...), _Tp>,
272 // Array comparisons.
273 template<typename _Tp, std::size_t _Nm>
276 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
277 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
279#if __cpp_lib_three_way_comparison && __cpp_lib_concepts
280 template<typename _Tp, size_t _Nm>
281 constexpr __detail::__synth3way_t<_Tp>
282 operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
284#ifdef __cpp_lib_is_constant_evaluated
285 if constexpr (_Nm && __is_memcmp_ordered<_Tp>::__value)
286 if (!std::is_constant_evaluated())
288 constexpr size_t __n = _Nm * sizeof(_Tp);
289 return __builtin_memcmp(__a.data(), __b.data(), __n) <=> 0;
293 for (size_t __i = 0; __i < _Nm; ++__i)
295 auto __c = __detail::__synth3way(__a[__i], __b[__i]);
299 return strong_ordering::equal;
302 template<typename _Tp, std::size_t _Nm>
305 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
306 { return !(__one == __two); }
308 template<typename _Tp, std::size_t _Nm>
311 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
313 return std::lexicographical_compare(__a.begin(), __a.end(),
314 __b.begin(), __b.end());
317 template<typename _Tp, std::size_t _Nm>
320 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
321 { return __two < __one; }
323 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>
332 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
333 { return !(__one < __two); }
334#endif // three_way_comparison && concepts
336 // Specialized algorithms.
337 template<typename _Tp, std::size_t _Nm>
340#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
341 // Constrained free swap overload, see p0185r1
343 __array_traits<_Tp, _Nm>::_Is_swappable::value
348 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
349 noexcept(noexcept(__one.swap(__two)))
350 { __one.swap(__two); }
352#if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
353 template<typename _Tp, std::size_t _Nm>
355 !__array_traits<_Tp, _Nm>::_Is_swappable::value>::type
356 swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete;
359 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
361 get(array<_Tp, _Nm>& __arr) noexcept
363 static_assert(_Int < _Nm, "array index is within bounds");
364 return __array_traits<_Tp, _Nm>::_S_ref(__arr._M_elems, _Int);
367 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
369 get(array<_Tp, _Nm>&& __arr) noexcept
371 static_assert(_Int < _Nm, "array index is within bounds");
372 return std::move(std::get<_Int>(__arr));
375 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
377 get(const array<_Tp, _Nm>& __arr) noexcept
379 static_assert(_Int < _Nm, "array index is within bounds");
380 return __array_traits<_Tp, _Nm>::_S_ref(__arr._M_elems, _Int);
383 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
384 constexpr const _Tp&&
385 get(const array<_Tp, _Nm>&& __arr) noexcept
387 static_assert(_Int < _Nm, "array index is within bounds");
388 return std::move(std::get<_Int>(__arr));
391#if __cplusplus > 201703L
392#define __cpp_lib_to_array 201907L
394 template<bool _Move = false, typename _Tp, size_t... _Idx>
395 constexpr array<remove_cv_t<_Tp>, sizeof...(_Idx)>
396 __to_array(_Tp (&__a)[sizeof...(_Idx)], index_sequence<_Idx...>)
399 return {{std::move(__a[_Idx])...}};
401 return {{__a[_Idx]...}};
404 template<typename _Tp, size_t _Nm>
405 constexpr array<remove_cv_t<_Tp>, _Nm>
406 to_array(_Tp (&__a)[_Nm])
407 noexcept(is_nothrow_constructible_v<_Tp, _Tp&>)
409 static_assert(!is_array_v<_Tp>);
410 static_assert(is_constructible_v<_Tp, _Tp&>);
411 if constexpr (is_constructible_v<_Tp, _Tp&>)
412 return __to_array(__a, make_index_sequence<_Nm>{});
413 __builtin_unreachable(); // FIXME: see PR c++/91388
416 template<typename _Tp, size_t _Nm>
417 constexpr array<remove_cv_t<_Tp>, _Nm>
418 to_array(_Tp (&&__a)[_Nm])
419 noexcept(is_nothrow_move_constructible_v<_Tp>)
421 static_assert(!is_array_v<_Tp>);
422 static_assert(is_move_constructible_v<_Tp>);
423 if constexpr (is_move_constructible_v<_Tp>)
424 return __to_array<1>(__a, make_index_sequence<_Nm>{});
425 __builtin_unreachable(); // FIXME: see PR c++/91388
429 // Tuple interface to class template array.
432 template<typename _Tp>
435 /// Partial specialization for std::array
436 template<typename _Tp, std::size_t _Nm>
437 struct tuple_size<array<_Tp, _Nm>>
438 : public integral_constant<std::size_t, _Nm> { };
441 template<std::size_t _Int, typename _Tp>
442 struct tuple_element;
444 /// Partial specialization for std::array
445 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
446 struct tuple_element<_Int, array<_Tp, _Nm>>
448 static_assert(_Int < _Nm, "index is out of bounds");
452 template<typename _Tp, std::size_t _Nm>
453 struct __is_tuple_like_impl<array<_Tp, _Nm>> : true_type
456_GLIBCXX_END_NAMESPACE_VERSION
461#endif // _GLIBCXX_ARRAY