1 // class template array -*- C++ -*-
3 // Copyright (C) 2004 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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.
40 #include <bits/functexcept.h>
47 /// @brief struct array [6.2.2].
48 /// NB: Requires complete type _Tp.
49 template<typename _Tp, size_t _Nm = 1>
52 typedef _Tp value_type;
53 typedef value_type& reference;
54 typedef const value_type& const_reference;
55 typedef value_type* iterator;
56 typedef const value_type* const_iterator;
57 typedef size_t size_type;
58 typedef ptrdiff_t difference_type;
59 typedef std::reverse_iterator<iterator> reverse_iterator;
60 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
62 // Compile time constant without other dependencies.
63 enum { _S_index = _Nm };
65 // Support for zero-sized arrays mandatory.
66 value_type _M_instance[_Nm ? _Nm : 1];
68 // No explicit construct/copy/destroy for aggregate type.
71 assign(const value_type& u);
79 { return reinterpret_cast<iterator>(&_M_instance[0]); }
83 { return reinterpret_cast<const_iterator>(&_M_instance[0]); }
87 { return reinterpret_cast<iterator>(&_M_instance[_Nm]); }
91 { return reinterpret_cast<const_iterator>(&_M_instance[_Nm]); }
95 { return reverse_iterator(this->end()); }
97 const_reverse_iterator
99 { return const_reverse_iterator(this->end()); }
103 { return reverse_iterator(this->begin()); }
105 const_reverse_iterator
107 { return const_reverse_iterator(this->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 reinterpret_cast<reference>(_M_instance[__n]); }
125 operator[](size_type __n) const
126 { return reinterpret_cast<const_reference>(_M_instance[__n]); }
129 at(size_type __n) const
131 if (__builtin_expect(__n > _Nm, false))
132 std::__throw_out_of_range("array::at");
133 return reinterpret_cast<const_reference>(_M_instance[__n]);
139 if (__builtin_expect(__n > _Nm, false))
140 std::__throw_out_of_range("array::at");
141 return reinterpret_cast<reference>(_M_instance[__n]);
163 // Array comparisons.
164 template<typename _Tp, size_t _Nm>
166 operator==(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
167 { return std::equal(__one.begin(), __one.end(), __two.begin()); }
169 template<typename _Tp, size_t _Nm>
171 operator!=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
172 { return !(__one == __two); }
174 template<typename _Tp, size_t _Nm>
176 operator<(const array<_Tp, _Nm>& a, const array<_Tp, _Nm>& b)
178 return std::lexicographical_compare(a.begin(), a.end(),
182 template<typename _Tp, size_t _Nm>
184 operator>(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
185 { return __two < __one; }
187 template<typename _Tp, size_t _Nm>
189 operator<=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
190 { return !(__one > __two); }
192 template<typename _Tp, size_t _Nm>
194 operator>=(const array<_Tp, _Nm>& __one, const array<_Tp, _Nm>& __two)
195 { return !(__one < __two); }
197 // Specialized algorithms [6.2.2.2].
198 template<typename _Tp, size_t _Nm>
200 swap(array<_Tp, _Nm>& __one, array<_Tp, _Nm>& __two)
201 { swap_ranges(__one.begin(), __one.end(), __two.begin()); }
202 } // namespace std::tr1