1 // class template array -*- C++ -*-
3 // Copyright (C) 2004-2017 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/>.
26 * This is a TR1 C++ Library header.
29 #ifndef _GLIBCXX_TR1_ARRAY
30 #define _GLIBCXX_TR1_ARRAY 1
32 #pragma GCC system_header
34 #include <bits/stl_algobase.h>
36 namespace std _GLIBCXX_VISIBILITY(default)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 * @brief A standard container for storing a fixed size sequence of elements.
47 * Meets the requirements of a <a href="tables.html#65">container</a>, a
48 * <a href="tables.html#66">reversible container</a>, and a
49 * <a href="tables.html#67">sequence</a>.
51 * Sets support random access iterators.
53 * @param Tp Type of element. Required to be a complete type.
54 * @param N Number of elements.
56 template<typename _Tp, std::size_t _Nm>
59 typedef _Tp value_type;
60 typedef value_type& reference;
61 typedef const value_type& const_reference;
62 typedef value_type* iterator;
63 typedef const value_type* const_iterator;
64 typedef std::size_t size_type;
65 typedef std::ptrdiff_t difference_type;
66 typedef std::reverse_iterator<iterator> reverse_iterator;
67 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
69 // Support for zero-sized arrays mandatory.
70 value_type _M_instance[_Nm ? _Nm : 1];
72 // No explicit construct/copy/destroy for aggregate type.
75 assign(const value_type& __u)
76 { std::fill_n(begin(), size(), __u); }
80 { std::swap_ranges(begin(), end(), __other.begin()); }
85 { return iterator(std::__addressof(_M_instance[0])); }
89 { return const_iterator(std::__addressof(_M_instance[0])); }
93 { return iterator(std::__addressof(_M_instance[_Nm])); }
97 { return const_iterator(std::__addressof(_M_instance[_Nm])); }
101 { return reverse_iterator(end()); }
103 const_reverse_iterator
105 { return const_reverse_iterator(end()); }
109 { return reverse_iterator(begin()); }
111 const_reverse_iterator
113 { return const_reverse_iterator(begin()); }
117 size() const { return _Nm; }
120 max_size() const { return _Nm; }
123 empty() const { return size() == 0; }
127 operator[](size_type __n)
128 { return _M_instance[__n]; }
131 operator[](size_type __n) const
132 { return _M_instance[__n]; }
138 std::__throw_out_of_range(__N("array::at"));
139 return _M_instance[__n];
143 at(size_type __n) const
146 std::__throw_out_of_range(__N("array::at"));
147 return _M_instance[__n];
160 { return _Nm ? *(end() - 1) : *end(); }
164 { return _Nm ? *(end() - 1) : *end(); }
168 { return std::__addressof(_M_instance[0]); }
172 { return std::__addressof(_M_instance[0]); }
175 // Array comparisons.
176 template<typename _Tp, std::size_t _Nm>
178 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
179 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
181 template<typename _Tp, std::size_t _Nm>
183 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
184 { return !(__one == __two); }
186 template<typename _Tp, std::size_t _Nm>
188 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
190 return std::lexicographical_compare(__a.begin(), __a.end(),
191 __b.begin(), __b.end());
194 template<typename _Tp, std::size_t _Nm>
196 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
197 { return __two < __one; }
199 template<typename _Tp, std::size_t _Nm>
201 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
202 { return !(__one > __two); }
204 template<typename _Tp, std::size_t _Nm>
206 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
207 { return !(__one < __two); }
209 // Specialized algorithms [6.2.2.2].
210 template<typename _Tp, std::size_t _Nm>
212 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
213 { __one.swap(__two); }
215 // Tuple interface to class template array [6.2.2.5].
218 template<typename _Tp>
222 template<int _Int, typename _Tp>
225 template<typename _Tp, std::size_t _Nm>
226 struct tuple_size<array<_Tp, _Nm> >
227 { static const int value = _Nm; };
229 template<typename _Tp, std::size_t _Nm>
231 tuple_size<array<_Tp, _Nm> >::value;
233 template<int _Int, typename _Tp, std::size_t _Nm>
234 struct tuple_element<_Int, array<_Tp, _Nm> >
235 { typedef _Tp type; };
237 template<int _Int, typename _Tp, std::size_t _Nm>
239 get(array<_Tp, _Nm>& __arr)
240 { return __arr[_Int]; }
242 template<int _Int, typename _Tp, std::size_t _Nm>
244 get(const array<_Tp, _Nm>& __arr)
245 { return __arr[_Int]; }
248 _GLIBCXX_END_NAMESPACE_VERSION
251 #endif // _GLIBCXX_TR1_ARRAY