1 // class template array -*- C++ -*-
3 // Copyright (C) 2004, 2005, 2006 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 2, 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 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
31 * This is a TR1 C++ Library header.
41 #include <bits/functexcept.h>
48 /// @brief struct array [6.2.2].
49 /// NB: Requires complete type _Tp.
50 template<typename _Tp, std::size_t _Nm>
53 typedef _Tp value_type;
54 typedef value_type& reference;
55 typedef const value_type& const_reference;
56 typedef value_type* iterator;
57 typedef const value_type* const_iterator;
58 typedef std::size_t size_type;
59 typedef std::ptrdiff_t difference_type;
60 typedef std::reverse_iterator<iterator> reverse_iterator;
61 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
63 // Support for zero-sized arrays mandatory.
64 value_type _M_instance[_Nm ? _Nm : 1] __attribute__((__aligned__));
66 // No explicit construct/copy/destroy for aggregate type.
69 assign(const value_type& __u)
70 { std::fill_n(begin(), size(), __u); }
74 { std::swap_ranges(begin(), end(), __other.begin()); }
79 { return iterator(&_M_instance[0]); }
83 { return const_iterator(&_M_instance[0]); }
87 { return iterator(&_M_instance[_Nm]); }
91 { return const_iterator(&_M_instance[_Nm]); }
95 { return reverse_iterator(end()); }
97 const_reverse_iterator
99 { return const_reverse_iterator(end()); }
103 { return reverse_iterator(begin()); }
105 const_reverse_iterator
107 { return const_reverse_iterator(begin()); }
111 size() const { return _Nm; }
114 max_size() const { return _Nm; }
117 empty() const { return size() == 0; }
121 operator[](size_type __n)
122 { return _M_instance[__n]; }
125 operator[](size_type __n) const
126 { return _M_instance[__n]; }
130 { return _M_at<_Nm>(__n); }
133 at(size_type __n) const
134 { return _M_at<_Nm>(__n); }
146 { return _Nm ? *(end() - 1) : *end(); }
150 { return _Nm ? *(end() - 1) : *end(); }
154 { return &_M_instance[0]; }
158 { return &_M_instance[0]; }
161 template<std::size_t _Mm>
162 typename std::__enable_if<reference, _Mm>::__type
165 if (__builtin_expect(__n >= _Mm, false))
166 std::__throw_out_of_range("array::_M_at");
167 return _M_instance[__n];
170 // Avoid "unsigned comparison with zero" warnings.
171 template<std::size_t _Mm>
172 typename std::__enable_if<reference, !_Mm>::__type
175 std::__throw_out_of_range("array::_M_at");
176 return _M_instance[0];
179 template<std::size_t _Mm>
180 typename std::__enable_if<const_reference, _Mm>::__type
181 _M_at(size_type __n) const
183 if (__builtin_expect(__n >= _Mm, false))
184 std::__throw_out_of_range("array::_M_at");
185 return _M_instance[__n];
188 template<std::size_t _Mm>
189 typename std::__enable_if<const_reference, !_Mm>::__type
190 _M_at(size_type) const
192 std::__throw_out_of_range("array::_M_at");
193 return _M_instance[0];
197 // Array comparisons.
198 template<typename _Tp, std::size_t _Nm>
200 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
201 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
203 template<typename _Tp, std::size_t _Nm>
205 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
206 { return !(__one == __two); }
208 template<typename _Tp, std::size_t _Nm>
210 operator<(const array<_Tp, _Nm>& __a, const array<_Tp, _Nm>& __b)
212 return std::lexicographical_compare(__a.begin(), __a.end(),
213 __b.begin(), __b.end());
216 template<typename _Tp, std::size_t _Nm>
218 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
219 { return __two < __one; }
221 template<typename _Tp, std::size_t _Nm>
223 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
224 { return !(__one > __two); }
226 template<typename _Tp, std::size_t _Nm>
228 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
229 { return !(__one < __two); }
231 // Specialized algorithms [6.2.2.2].
232 template<typename _Tp, std::size_t _Nm>
234 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
235 { std::swap_ranges(__one.begin(), __one.end(), __two.begin()); }
237 // Tuple interface to class template array [6.2.2.5].
238 template<typename _Tp> class tuple_size;
239 template<int _Int, typename _Tp> class tuple_element;
241 template<typename _Tp, std::size_t _Nm>
242 struct tuple_size<array<_Tp, _Nm> >
243 { static const int value = _Nm; };
245 template<int _Int, typename _Tp, std::size_t _Nm>
246 struct tuple_element<_Int, array<_Tp, _Nm> >
247 { typedef _Tp type; };
249 template<int _Int, typename _Tp, std::size_t _Nm>
251 get(array<_Tp, _Nm>& __arr)
252 { return __arr[_Int]; }
254 template<int _Int, typename _Tp, std::size_t _Nm>
256 get(const array<_Tp, _Nm>& __arr)
257 { return __arr[_Int]; }
258 } // namespace std::tr1