1 /* The following code declares class array,
2 * an STL container (as wrapper) for arrays of constant size.
5 * http://www.boost.org/libs/array/
8 * The original author site is at: http://www.josuttis.com/
10 * (C) Copyright Nicolai M. Josuttis 2001.
12 * Distributed under the Boost Software License, Version 1.0. (See
13 * accompanying file LICENSE_1_0.txt or copy at
14 * http://www.boost.org/LICENSE_1_0.txt)
16 * 9 Jan 2013 - (mtc) Added constexpr
17 * 14 Apr 2012 - (mtc) Added support for boost::hash
18 * 28 Dec 2010 - (mtc) Added cbegin and cend (and crbegin and crend) for C++Ox compatibility.
19 * 10 Mar 2010 - (mtc) fill method added, matching resolution of the standard library working group.
20 * See <http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#776> or Trac issue #3168
21 * Eventually, we should remove "assign" which is now a synonym for "fill" (Marshall Clow)
22 * 10 Mar 2010 - added workaround for SUNCC and !STLPort [trac #3893] (Marshall Clow)
23 * 29 Jan 2004 - c_array() added, BOOST_NO_PRIVATE_IN_AGGREGATE removed (Nico Josuttis)
24 * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
25 * 05 Aug 2001 - minor update (Nico Josuttis)
26 * 20 Jan 2001 - STLport fix (Beman Dawes)
27 * 29 Sep 2000 - Initial Revision (Nico Josuttis)
31 #ifndef BOOST_ARRAY_HPP
32 #define BOOST_ARRAY_HPP
34 #include <boost/detail/workaround.hpp>
36 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
37 # pragma warning(push)
38 # pragma warning(disable:4996) // 'std::equal': Function call with parameters that may be unsafe
39 # pragma warning(disable:4510) // boost::array<T,N>' : default constructor could not be generated
40 # pragma warning(disable:4610) // warning C4610: class 'boost::array<T,N>' can never be instantiated - user defined constructor required
45 #include <boost/assert.hpp>
46 #include <boost/static_assert.hpp>
47 #include <boost/swap.hpp>
49 // Handles broken standard libraries better than <iterator>
50 #include <boost/detail/iterator.hpp>
51 #include <boost/throw_exception.hpp>
54 // FIXES for broken compilers
55 #include <boost/config.hpp>
60 template<class T, std::size_t N>
63 T elems[N]; // fixed-size array of elements of type T
69 typedef const T* const_iterator;
71 typedef const T& const_reference;
72 typedef std::size_t size_type;
73 typedef std::ptrdiff_t difference_type;
76 iterator begin() { return elems; }
77 const_iterator begin() const { return elems; }
78 const_iterator cbegin() const { return elems; }
80 iterator end() { return elems+N; }
81 const_iterator end() const { return elems+N; }
82 const_iterator cend() const { return elems+N; }
84 // reverse iterator support
85 #if !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
86 typedef std::reverse_iterator<iterator> reverse_iterator;
87 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
88 #elif defined(_RWSTD_NO_CLASS_PARTIAL_SPEC)
89 typedef std::reverse_iterator<iterator, std::random_access_iterator_tag,
90 value_type, reference, iterator, difference_type> reverse_iterator;
91 typedef std::reverse_iterator<const_iterator, std::random_access_iterator_tag,
92 value_type, const_reference, const_iterator, difference_type> const_reverse_iterator;
94 // workaround for broken reverse_iterator implementations
95 typedef std::reverse_iterator<iterator,T> reverse_iterator;
96 typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
99 reverse_iterator rbegin() { return reverse_iterator(end()); }
100 const_reverse_iterator rbegin() const {
101 return const_reverse_iterator(end());
103 const_reverse_iterator crbegin() const {
104 return const_reverse_iterator(end());
107 reverse_iterator rend() { return reverse_iterator(begin()); }
108 const_reverse_iterator rend() const {
109 return const_reverse_iterator(begin());
111 const_reverse_iterator crend() const {
112 return const_reverse_iterator(begin());
116 reference operator[](size_type i)
118 return BOOST_ASSERT_MSG( i < N, "out of range" ), elems[i];
121 /*BOOST_CONSTEXPR*/ const_reference operator[](size_type i) const
123 return BOOST_ASSERT_MSG( i < N, "out of range" ), elems[i];
126 // at() with range check
127 reference at(size_type i) { return rangecheck(i), elems[i]; }
128 /*BOOST_CONSTEXPR*/ const_reference at(size_type i) const { return rangecheck(i), elems[i]; }
130 // front() and back()
136 BOOST_CONSTEXPR const_reference front() const
146 BOOST_CONSTEXPR const_reference back() const
152 static BOOST_CONSTEXPR size_type size() { return N; }
153 static BOOST_CONSTEXPR bool empty() { return false; }
154 static BOOST_CONSTEXPR size_type max_size() { return N; }
155 enum { static_size = N };
157 // swap (note: linear complexity)
158 void swap (array<T,N>& y) {
159 for (size_type i = 0; i < N; ++i)
160 boost::swap(elems[i],y.elems[i]);
163 // direct access to data (read-only)
164 const T* data() const { return elems; }
165 T* data() { return elems; }
167 // use array as C array (direct read/write access to data)
168 T* c_array() { return elems; }
170 // assignment with type conversion
171 template <typename T2>
172 array<T,N>& operator= (const array<T2,N>& rhs) {
173 std::copy(rhs.begin(),rhs.end(), begin());
177 // assign one value to all elements
178 void assign (const T& value) { fill ( value ); } // A synonym for fill
179 void fill (const T& value)
181 std::fill_n(begin(),size(),value);
184 // check range (may be private because it is static)
185 static BOOST_CONSTEXPR bool rangecheck (size_type i) {
186 return i > size() ? boost::throw_exception(std::out_of_range ("array<>: index out of range")), true : true;
192 class array< T, 0 > {
196 typedef T value_type;
198 typedef const T* const_iterator;
199 typedef T& reference;
200 typedef const T& const_reference;
201 typedef std::size_t size_type;
202 typedef std::ptrdiff_t difference_type;
205 iterator begin() { return iterator( reinterpret_cast< T * >( this ) ); }
206 const_iterator begin() const { return const_iterator( reinterpret_cast< const T * >( this ) ); }
207 const_iterator cbegin() const { return const_iterator( reinterpret_cast< const T * >( this ) ); }
209 iterator end() { return begin(); }
210 const_iterator end() const { return begin(); }
211 const_iterator cend() const { return cbegin(); }
213 // reverse iterator support
214 #if !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
215 typedef std::reverse_iterator<iterator> reverse_iterator;
216 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
217 #elif defined(_RWSTD_NO_CLASS_PARTIAL_SPEC)
218 typedef std::reverse_iterator<iterator, std::random_access_iterator_tag,
219 value_type, reference, iterator, difference_type> reverse_iterator;
220 typedef std::reverse_iterator<const_iterator, std::random_access_iterator_tag,
221 value_type, const_reference, const_iterator, difference_type> const_reverse_iterator;
223 // workaround for broken reverse_iterator implementations
224 typedef std::reverse_iterator<iterator,T> reverse_iterator;
225 typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
228 reverse_iterator rbegin() { return reverse_iterator(end()); }
229 const_reverse_iterator rbegin() const {
230 return const_reverse_iterator(end());
232 const_reverse_iterator crbegin() const {
233 return const_reverse_iterator(end());
236 reverse_iterator rend() { return reverse_iterator(begin()); }
237 const_reverse_iterator rend() const {
238 return const_reverse_iterator(begin());
240 const_reverse_iterator crend() const {
241 return const_reverse_iterator(begin());
245 reference operator[](size_type /*i*/)
247 return failed_rangecheck();
250 /*BOOST_CONSTEXPR*/ const_reference operator[](size_type /*i*/) const
252 return failed_rangecheck();
255 // at() with range check
256 reference at(size_type /*i*/) { return failed_rangecheck(); }
257 /*BOOST_CONSTEXPR*/ const_reference at(size_type /*i*/) const { return failed_rangecheck(); }
259 // front() and back()
262 return failed_rangecheck();
265 BOOST_CONSTEXPR const_reference front() const
267 return failed_rangecheck();
272 return failed_rangecheck();
275 BOOST_CONSTEXPR const_reference back() const
277 return failed_rangecheck();
281 static BOOST_CONSTEXPR size_type size() { return 0; }
282 static BOOST_CONSTEXPR bool empty() { return true; }
283 static BOOST_CONSTEXPR size_type max_size() { return 0; }
284 enum { static_size = 0 };
286 void swap (array<T,0>& /*y*/) {
289 // direct access to data (read-only)
290 const T* data() const { return 0; }
291 T* data() { return 0; }
293 // use array as C array (direct read/write access to data)
294 T* c_array() { return 0; }
296 // assignment with type conversion
297 template <typename T2>
298 array<T,0>& operator= (const array<T2,0>& ) {
302 // assign one value to all elements
303 void assign (const T& value) { fill ( value ); }
304 void fill (const T& ) {}
306 // check range (may be private because it is static)
307 static reference failed_rangecheck () {
308 std::out_of_range e("attempt to access element of an empty array");
309 boost::throw_exception(e);
310 #if defined(BOOST_NO_EXCEPTIONS) || (!defined(BOOST_MSVC) && !defined(__PATHSCALE__))
312 // We need to return something here to keep
313 // some compilers happy: however we will never
314 // actually get here....
316 static T placeholder;
323 template<class T, std::size_t N>
324 bool operator== (const array<T,N>& x, const array<T,N>& y) {
325 return std::equal(x.begin(), x.end(), y.begin());
327 template<class T, std::size_t N>
328 bool operator< (const array<T,N>& x, const array<T,N>& y) {
329 return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
331 template<class T, std::size_t N>
332 bool operator!= (const array<T,N>& x, const array<T,N>& y) {
335 template<class T, std::size_t N>
336 bool operator> (const array<T,N>& x, const array<T,N>& y) {
339 template<class T, std::size_t N>
340 bool operator<= (const array<T,N>& x, const array<T,N>& y) {
343 template<class T, std::size_t N>
344 bool operator>= (const array<T,N>& x, const array<T,N>& y) {
349 template<class T, std::size_t N>
350 inline void swap (array<T,N>& x, array<T,N>& y) {
354 #if defined(__SUNPRO_CC)
355 // Trac ticket #4757; the Sun Solaris compiler can't handle
356 // syntax like 'T(&get_c_array(boost::array<T,N>& arg))[N]'
358 // We can't just use this for all compilers, because the
359 // borland compilers can't handle this form.
361 template <typename T, std::size_t N> struct c_array
367 // Specific for boost::array: simply returns its elems data member.
368 template <typename T, std::size_t N>
369 typename detail::c_array<T,N>::type& get_c_array(boost::array<T,N>& arg)
374 // Specific for boost::array: simply returns its elems data member.
375 template <typename T, std::size_t N>
376 typename detail::c_array<T,N>::type const& get_c_array(const boost::array<T,N>& arg)
381 // Specific for boost::array: simply returns its elems data member.
382 template <typename T, std::size_t N>
383 T(&get_c_array(boost::array<T,N>& arg))[N]
389 template <typename T, std::size_t N>
390 const T(&get_c_array(const boost::array<T,N>& arg))[N]
397 // Overload for std::array, assuming that std::array will have
398 // explicit conversion functions as discussed at the WG21 meeting
399 // in Summit, March 2009.
400 template <typename T, std::size_t N>
401 T(&get_c_array(std::array<T,N>& arg))[N]
403 return static_cast<T(&)[N]>(arg);
407 template <typename T, std::size_t N>
408 const T(&get_c_array(const std::array<T,N>& arg))[N]
410 return static_cast<T(&)[N]>(arg);
414 template <class It> std::size_t hash_range(It, It);
416 template<class T, std::size_t N>
417 std::size_t hash_value(const array<T,N>& arr)
419 return boost::hash_range(arr.begin(), arr.end());
422 template <size_t Idx, typename T, size_t N>
423 T &get(boost::array<T,N> &arr) BOOST_NOEXCEPT {
424 BOOST_STATIC_ASSERT_MSG ( Idx < N, "boost::get<>(boost::array &) index out of range" );
428 template <size_t Idx, typename T, size_t N>
429 const T &get(const boost::array<T,N> &arr) BOOST_NOEXCEPT {
430 BOOST_STATIC_ASSERT_MSG ( Idx < N, "boost::get<>(const boost::array &) index out of range" );
434 } /* namespace boost */
436 #ifndef BOOST_NO_CXX11_HDR_ARRAY
437 // If we don't have std::array, I'm assuming that we don't have std::get
439 template <size_t Idx, typename T, size_t N>
440 T &get(boost::array<T,N> &arr) BOOST_NOEXCEPT {
441 BOOST_STATIC_ASSERT_MSG ( Idx < N, "std::get<>(boost::array &) index out of range" );
445 template <size_t Idx, typename T, size_t N>
446 const T &get(const boost::array<T,N> &arr) BOOST_NOEXCEPT {
447 BOOST_STATIC_ASSERT_MSG ( Idx < N, "std::get<>(const boost::array &) index out of range" );
453 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
454 # pragma warning(pop)
457 #endif /*BOOST_ARRAY_HPP*/