3 // Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file include/array
27 * This is a Standard C++ Library header.
30 #ifndef _GLIBCXX_ARRAY
31 #define _GLIBCXX_ARRAY 1
33 #pragma GCC system_header
35 #ifndef __GXX_EXPERIMENTAL_CXX0X__
36 # include <bits/c++0x_warning.h>
40 #include <bits/stl_algobase.h>
41 #include <bits/range_access.h>
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
48 * @brief A standard container for storing a fixed size sequence of elements.
52 * Meets the requirements of a <a href="tables.html#65">container</a>, a
53 * <a href="tables.html#66">reversible container</a>, and a
54 * <a href="tables.html#67">sequence</a>.
56 * Sets support random access iterators.
58 * @tparam Tp Type of element. Required to be a complete type.
59 * @tparam N Number of elements.
61 template<typename _Tp, std::size_t _Nm>
64 typedef _Tp value_type;
65 typedef value_type* pointer;
66 typedef const value_type* const_pointer;
67 typedef value_type& reference;
68 typedef const value_type& const_reference;
69 typedef value_type* iterator;
70 typedef const value_type* const_iterator;
71 typedef std::size_t size_type;
72 typedef std::ptrdiff_t difference_type;
73 typedef std::reverse_iterator<iterator> reverse_iterator;
74 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
76 // Support for zero-sized arrays mandatory.
77 value_type _M_instance[_Nm ? _Nm : 1];
79 // No explicit construct/copy/destroy for aggregate type.
83 fill(const value_type& __u)
84 { std::fill_n(begin(), size(), __u); }
88 noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
89 { std::swap_ranges(begin(), end(), __other.begin()); }
94 { return iterator(data()); }
97 begin() const noexcept
98 { return const_iterator(data()); }
102 { return iterator(data() + _Nm); }
106 { return const_iterator(data() + _Nm); }
110 { return reverse_iterator(end()); }
112 const_reverse_iterator
113 rbegin() const noexcept
114 { return const_reverse_iterator(end()); }
118 { return reverse_iterator(begin()); }
120 const_reverse_iterator
121 rend() const noexcept
122 { return const_reverse_iterator(begin()); }
125 cbegin() const noexcept
126 { return const_iterator(std::__addressof(_M_instance[0])); }
129 cend() const noexcept
130 { return const_iterator(std::__addressof(_M_instance[_Nm])); }
132 const_reverse_iterator
133 crbegin() const noexcept
134 { return const_reverse_iterator(end()); }
136 const_reverse_iterator
137 crend() const noexcept
138 { return const_reverse_iterator(begin()); }
142 size() const noexcept { return _Nm; }
145 max_size() const noexcept { return _Nm; }
148 empty() const noexcept { return size() == 0; }
152 operator[](size_type __n)
153 { return _M_instance[__n]; }
155 constexpr const_reference
156 operator[](size_type __n) const noexcept
157 { return _M_instance[__n]; }
163 std::__throw_out_of_range(__N("array::at"));
164 return _M_instance[__n];
167 constexpr const_reference
168 at(size_type __n) const
170 // Result of conditional expression must be an lvalue so use
171 // boolean ? lvalue : (throw-expr, lvalue)
172 return __n < _Nm ? _M_instance[__n]
173 : (std::__throw_out_of_range(__N("array::at")), _M_instance[0]);
186 { return _Nm ? *(end() - 1) : *end(); }
190 { return _Nm ? *(end() - 1) : *end(); }
194 { return std::__addressof(_M_instance[0]); }
197 data() const noexcept
198 { return std::__addressof(_M_instance[0]); }
201 // Array comparisons.
202 template<typename _Tp, std::size_t _Nm>
204 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
205 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
207 template<typename _Tp, std::size_t _Nm>
209 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
210 { return !(__one == __two); }
212 template<typename _Tp, std::size_t _Nm>
214 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
216 return std::lexicographical_compare(__a.begin(), __a.end(),
217 __b.begin(), __b.end());
220 template<typename _Tp, std::size_t _Nm>
222 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
223 { return __two < __one; }
225 template<typename _Tp, std::size_t _Nm>
227 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
228 { return !(__one > __two); }
230 template<typename _Tp, std::size_t _Nm>
232 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
233 { return !(__one < __two); }
235 // Specialized algorithms.
236 template<typename _Tp, std::size_t _Nm>
238 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
239 noexcept(noexcept(__one.swap(__two)))
240 { __one.swap(__two); }
242 // Tuple interface to class template array.
245 template<typename _Tp>
248 template<typename _Tp, std::size_t _Nm>
249 struct tuple_size<array<_Tp, _Nm>>
250 : public integral_constant<std::size_t, _Nm> { };
253 template<std::size_t _Int, typename _Tp>
256 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
257 struct tuple_element<_Int, array<_Tp, _Nm>>
259 static_assert(_Int < _Nm, "index is out of bounds");
263 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
265 get(array<_Tp, _Nm>& __arr) noexcept
267 static_assert(_Int < _Nm, "index is out of bounds");
268 return __arr._M_instance[_Int];
271 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
273 get(array<_Tp, _Nm>&& __arr) noexcept
275 static_assert(_Int < _Nm, "index is out of bounds");
276 return std::move(get<_Int>(__arr));
279 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
281 get(const array<_Tp, _Nm>& __arr) noexcept
283 static_assert(_Int < _Nm, "index is out of bounds");
284 return __arr._M_instance[_Int];
287 _GLIBCXX_END_NAMESPACE_VERSION
290 #endif // __GXX_EXPERIMENTAL_CXX0X__
292 #endif // _GLIBCXX_ARRAY