3 // Copyright (C) 2007-2020 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.
29 #ifndef _GLIBCXX_ARRAY
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>
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
47 template<typename _Tp, std::size_t _Nm>
50 typedef _Tp _Type[_Nm];
51 typedef __is_swappable<_Tp> _Is_swappable;
52 typedef __is_nothrow_swappable<_Tp> _Is_nothrow_swappable;
55 _S_ref(const _Type& __t, std::size_t __n) noexcept
56 { return const_cast<_Tp&>(__t[__n]); }
59 _S_ptr(const _Type& __t) noexcept
60 { return const_cast<_Tp*>(__t); }
63 template<typename _Tp>
64 struct __array_traits<_Tp, 0>
67 typedef true_type _Is_swappable;
68 typedef true_type _Is_nothrow_swappable;
71 _S_ref(const _Type&, std::size_t) noexcept
72 { return *static_cast<_Tp*>(nullptr); }
75 _S_ptr(const _Type&) noexcept
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 typedef _GLIBCXX_STD_C::__array_traits<_Tp, _Nm> _AT_Type;
110 typename _AT_Type::_Type _M_elems;
112 // No explicit construct/copy/destroy for aggregate type.
115 _GLIBCXX20_CONSTEXPR void
116 fill(const value_type& __u)
117 { std::fill_n(begin(), size(), __u); }
119 _GLIBCXX20_CONSTEXPR void
121 noexcept(_AT_Type::_Is_nothrow_swappable::value)
122 { std::swap_ranges(begin(), end(), __other.begin()); }
125 _GLIBCXX17_CONSTEXPR iterator
127 { return iterator(data()); }
129 _GLIBCXX17_CONSTEXPR const_iterator
130 begin() const noexcept
131 { return const_iterator(data()); }
133 _GLIBCXX17_CONSTEXPR iterator
135 { return iterator(data() + _Nm); }
137 _GLIBCXX17_CONSTEXPR const_iterator
139 { return const_iterator(data() + _Nm); }
141 _GLIBCXX17_CONSTEXPR reverse_iterator
143 { return reverse_iterator(end()); }
145 _GLIBCXX17_CONSTEXPR const_reverse_iterator
146 rbegin() const noexcept
147 { return const_reverse_iterator(end()); }
149 _GLIBCXX17_CONSTEXPR reverse_iterator
151 { return reverse_iterator(begin()); }
153 _GLIBCXX17_CONSTEXPR const_reverse_iterator
154 rend() const noexcept
155 { return const_reverse_iterator(begin()); }
157 _GLIBCXX17_CONSTEXPR const_iterator
158 cbegin() const noexcept
159 { return const_iterator(data()); }
161 _GLIBCXX17_CONSTEXPR const_iterator
162 cend() const noexcept
163 { return const_iterator(data() + _Nm); }
165 _GLIBCXX17_CONSTEXPR const_reverse_iterator
166 crbegin() const noexcept
167 { return const_reverse_iterator(end()); }
169 _GLIBCXX17_CONSTEXPR const_reverse_iterator
170 crend() const noexcept
171 { return const_reverse_iterator(begin()); }
175 size() const noexcept { return _Nm; }
178 max_size() const noexcept { return _Nm; }
180 _GLIBCXX_NODISCARD constexpr bool
181 empty() const noexcept { return size() == 0; }
184 _GLIBCXX17_CONSTEXPR reference
185 operator[](size_type __n) noexcept
186 { return _AT_Type::_S_ref(_M_elems, __n); }
188 constexpr const_reference
189 operator[](size_type __n) const noexcept
190 { return _AT_Type::_S_ref(_M_elems, __n); }
192 _GLIBCXX17_CONSTEXPR reference
196 std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
197 ">= _Nm (which is %zu)"),
199 return _AT_Type::_S_ref(_M_elems, __n);
202 constexpr const_reference
203 at(size_type __n) const
205 // Result of conditional expression must be an lvalue so use
206 // boolean ? lvalue : (throw-expr, lvalue)
207 return __n < _Nm ? _AT_Type::_S_ref(_M_elems, __n)
208 : (std::__throw_out_of_range_fmt(__N("array::at: __n (which is %zu) "
209 ">= _Nm (which is %zu)"),
211 _AT_Type::_S_ref(_M_elems, 0));
214 _GLIBCXX17_CONSTEXPR reference
218 constexpr const_reference
219 front() const noexcept
220 { return _AT_Type::_S_ref(_M_elems, 0); }
222 _GLIBCXX17_CONSTEXPR reference
224 { return _Nm ? *(end() - 1) : *end(); }
226 constexpr const_reference
227 back() const noexcept
229 return _Nm ? _AT_Type::_S_ref(_M_elems, _Nm - 1)
230 : _AT_Type::_S_ref(_M_elems, 0);
233 _GLIBCXX17_CONSTEXPR pointer
235 { return _AT_Type::_S_ptr(_M_elems); }
237 _GLIBCXX17_CONSTEXPR const_pointer
238 data() const noexcept
239 { return _AT_Type::_S_ptr(_M_elems); }
242 #if __cpp_deduction_guides >= 201606
243 template<typename _Tp, typename... _Up>
245 -> array<enable_if_t<(is_same_v<_Tp, _Up> && ...), _Tp>,
249 // Array comparisons.
250 template<typename _Tp, std::size_t _Nm>
253 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
254 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
256 #if __cpp_lib_three_way_comparison && __cpp_lib_concepts
257 template<typename _Tp, size_t _Nm>
258 constexpr __detail::__synth3way_t<_Tp>
259 operator<=>(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
261 if constexpr (_Nm && __is_byte<_Tp>::__value)
262 return __builtin_memcmp(__a.data(), __b.data(), _Nm) <=> 0;
265 for (size_t __i = 0; __i < _Nm; ++__i)
267 auto __c = __detail::__synth3way(__a[__i], __b[__i]);
272 return strong_ordering::equal;
275 template<typename _Tp, std::size_t _Nm>
278 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
279 { return !(__one == __two); }
281 template<typename _Tp, std::size_t _Nm>
284 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
286 return std::lexicographical_compare(__a.begin(), __a.end(),
287 __b.begin(), __b.end());
290 template<typename _Tp, std::size_t _Nm>
293 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
294 { return __two < __one; }
296 template<typename _Tp, std::size_t _Nm>
299 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
300 { return !(__one > __two); }
302 template<typename _Tp, std::size_t _Nm>
305 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
306 { return !(__one < __two); }
307 #endif // three_way_comparison && concepts
309 // Specialized algorithms.
310 template<typename _Tp, std::size_t _Nm>
313 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
314 // Constrained free swap overload, see p0185r1
316 _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::_Is_swappable::value
321 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
322 noexcept(noexcept(__one.swap(__two)))
323 { __one.swap(__two); }
325 #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11
326 template<typename _Tp, std::size_t _Nm>
328 !_GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::_Is_swappable::value>::type
329 swap(array<_Tp, _Nm>&, array<_Tp, _Nm>&) = delete;
332 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
334 get(array<_Tp, _Nm>& __arr) noexcept
336 static_assert(_Int < _Nm, "array index is within bounds");
337 return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
338 _S_ref(__arr._M_elems, _Int);
341 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
343 get(array<_Tp, _Nm>&& __arr) noexcept
345 static_assert(_Int < _Nm, "array index is within bounds");
346 return std::move(_GLIBCXX_STD_C::get<_Int>(__arr));
349 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
351 get(const array<_Tp, _Nm>& __arr) noexcept
353 static_assert(_Int < _Nm, "array index is within bounds");
354 return _GLIBCXX_STD_C::__array_traits<_Tp, _Nm>::
355 _S_ref(__arr._M_elems, _Int);
358 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
359 constexpr const _Tp&&
360 get(const array<_Tp, _Nm>&& __arr) noexcept
362 static_assert(_Int < _Nm, "array index is within bounds");
363 return std::move(_GLIBCXX_STD_C::get<_Int>(__arr));
366 #if __cplusplus > 201703L
367 #define __cpp_lib_to_array 201907L
369 template<bool _Move = false, typename _Tp, size_t... _Idx>
370 constexpr array<remove_cv_t<_Tp>, sizeof...(_Idx)>
371 __to_array(_Tp (&__a)[sizeof...(_Idx)], index_sequence<_Idx...>)
374 return {{std::move(__a[_Idx])...}};
376 return {{__a[_Idx]...}};
379 template<typename _Tp, size_t _Nm>
380 constexpr array<remove_cv_t<_Tp>, _Nm>
381 to_array(_Tp (&__a)[_Nm])
382 noexcept(is_nothrow_constructible_v<_Tp, _Tp&>)
384 static_assert(!is_array_v<_Tp>);
385 static_assert(is_constructible_v<_Tp, _Tp&>);
386 if constexpr (is_constructible_v<_Tp, _Tp&>)
387 return _GLIBCXX_STD_C::__to_array(__a, make_index_sequence<_Nm>{});
388 __builtin_unreachable(); // FIXME: see PR c++/91388
391 template<typename _Tp, size_t _Nm>
392 constexpr array<remove_cv_t<_Tp>, _Nm>
393 to_array(_Tp (&&__a)[_Nm])
394 noexcept(is_nothrow_move_constructible_v<_Tp>)
396 static_assert(!is_array_v<_Tp>);
397 static_assert(is_move_constructible_v<_Tp>);
398 if constexpr (is_move_constructible_v<_Tp>)
399 return _GLIBCXX_STD_C::__to_array<1>(__a, make_index_sequence<_Nm>{});
400 __builtin_unreachable(); // FIXME: see PR c++/91388
404 _GLIBCXX_END_NAMESPACE_CONTAINER
407 namespace std _GLIBCXX_VISIBILITY(default)
409 _GLIBCXX_BEGIN_NAMESPACE_VERSION
411 // Tuple interface to class template array.
414 template<typename _Tp>
417 /// Partial specialization for std::array
418 template<typename _Tp, std::size_t _Nm>
419 struct tuple_size<_GLIBCXX_STD_C::array<_Tp, _Nm>>
420 : public integral_constant<std::size_t, _Nm> { };
423 template<std::size_t _Int, typename _Tp>
424 struct tuple_element;
426 /// Partial specialization for std::array
427 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
428 struct tuple_element<_Int, _GLIBCXX_STD_C::array<_Tp, _Nm>>
430 static_assert(_Int < _Nm, "index is out of bounds");
434 template<typename _Tp, std::size_t _Nm>
435 struct __is_tuple_like_impl<_GLIBCXX_STD_C::array<_Tp, _Nm>> : true_type
438 _GLIBCXX_END_NAMESPACE_VERSION
441 #ifdef _GLIBCXX_DEBUG
442 # include <debug/array>
447 #endif // _GLIBCXX_ARRAY