3 // Copyright (C) 2007, 2008, 2009, 2010, 2011 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 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
39 #include <bits/stl_algobase.h>
40 #include <bits/range_access.h>
42 namespace std _GLIBCXX_VISIBILITY(default)
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 * @brief A standard container for storing a fixed size sequence of elements.
51 * Meets the requirements of a <a href="tables.html#65">container</a>, a
52 * <a href="tables.html#66">reversible container</a>, and a
53 * <a href="tables.html#67">sequence</a>.
55 * Sets support random access iterators.
57 * @param Tp Type of element. Required to be a complete type.
58 * @param N Number of elements.
60 template<typename _Tp, std::size_t _Nm>
63 typedef _Tp value_type;
64 typedef value_type* pointer;
65 typedef const value_type* const_pointer;
66 typedef value_type& reference;
67 typedef const value_type& const_reference;
68 typedef value_type* iterator;
69 typedef const value_type* const_iterator;
70 typedef std::size_t size_type;
71 typedef std::ptrdiff_t difference_type;
72 typedef std::reverse_iterator<iterator> reverse_iterator;
73 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
75 // Support for zero-sized arrays mandatory.
76 value_type _M_instance[_Nm ? _Nm : 1];
78 // No explicit construct/copy/destroy for aggregate type.
82 fill(const value_type& __u)
83 { std::fill_n(begin(), size(), __u); }
87 noexcept(noexcept(swap(std::declval<_Tp&>(), std::declval<_Tp&>())))
88 { std::swap_ranges(begin(), end(), __other.begin()); }
93 { return iterator(data()); }
96 begin() const noexcept
97 { return const_iterator(data()); }
101 { return iterator(data() + _Nm); }
105 { return const_iterator(data() + _Nm); }
109 { return reverse_iterator(end()); }
111 const_reverse_iterator
112 rbegin() const noexcept
113 { return const_reverse_iterator(end()); }
117 { return reverse_iterator(begin()); }
119 const_reverse_iterator
120 rend() const noexcept
121 { return const_reverse_iterator(begin()); }
124 cbegin() const noexcept
125 { return const_iterator(std::__addressof(_M_instance[0])); }
128 cend() const noexcept
129 { return const_iterator(std::__addressof(_M_instance[_Nm])); }
131 const_reverse_iterator
132 crbegin() const noexcept
133 { return const_reverse_iterator(end()); }
135 const_reverse_iterator
136 crend() const noexcept
137 { return const_reverse_iterator(begin()); }
141 size() const noexcept { return _Nm; }
144 max_size() const noexcept { return _Nm; }
147 empty() const noexcept { return size() == 0; }
151 operator[](size_type __n)
152 { return _M_instance[__n]; }
154 constexpr const_reference
155 operator[](size_type __n) const noexcept
156 { return _M_instance[__n]; }
162 std::__throw_out_of_range(__N("array::at"));
163 return _M_instance[__n];
167 constexpr const_reference
168 at(size_type __n) const
171 _M_instance[__n] : throw out_of_range(__N("array::at"));
175 at(size_type __n) const
178 _M_instance[__n] : __throw_out_of_range(__N("array::at"));
192 { return _Nm ? *(end() - 1) : *end(); }
196 { return _Nm ? *(end() - 1) : *end(); }
200 { return std::__addressof(_M_instance[0]); }
203 data() const noexcept
204 { return std::__addressof(_M_instance[0]); }
207 // Array comparisons.
208 template<typename _Tp, std::size_t _Nm>
210 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
211 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
213 template<typename _Tp, std::size_t _Nm>
215 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
216 { return !(__one == __two); }
218 template<typename _Tp, std::size_t _Nm>
220 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
222 return std::lexicographical_compare(__a.begin(), __a.end(),
223 __b.begin(), __b.end());
226 template<typename _Tp, std::size_t _Nm>
228 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
229 { return __two < __one; }
231 template<typename _Tp, std::size_t _Nm>
233 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
234 { return !(__one > __two); }
236 template<typename _Tp, std::size_t _Nm>
238 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
239 { return !(__one < __two); }
241 // Specialized algorithms.
242 template<typename _Tp, std::size_t _Nm>
244 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
245 noexcept(noexcept(__one.swap(__two)))
246 { __one.swap(__two); }
248 // Tuple interface to class template array.
251 template<typename _Tp>
254 template<typename _Tp, std::size_t _Nm>
255 struct tuple_size<array<_Tp, _Nm>>
256 : public integral_constant<std::size_t, _Nm> { };
259 template<std::size_t _Int, typename _Tp>
262 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
263 struct tuple_element<_Int, array<_Tp, _Nm> >
264 { typedef _Tp type; };
266 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
268 get(array<_Tp, _Nm>& __arr) noexcept
269 { return __arr[_Int]; }
271 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
273 get(array<_Tp, _Nm>&& __arr) noexcept
274 { return std::move(get<_Int>(__arr)); }
276 template<std::size_t _Int, typename _Tp, std::size_t _Nm>
278 get(const array<_Tp, _Nm>& __arr) noexcept
279 { return __arr[_Int]; }
281 _GLIBCXX_END_NAMESPACE_VERSION
284 #endif // __GXX_EXPERIMENTAL_CXX0X__
286 #endif // _GLIBCXX_ARRAY