Compare commits

...

16 Commits

Author SHA1 Message Date
bcbf9de9ed Created a branch from trunk
[SVN r38959]
2007-08-26 05:34:35 +00:00
069b5e2ca1 Throw out_of_range rather than range_error. See 21.1.1 paragraph 13.
[SVN r36304]
2006-12-08 16:54:30 +00:00
526953fc5e Test for out_of_range rather than range_error. See 21.1.1 paragraph 13.
[SVN r36303]
2006-12-08 16:53:49 +00:00
b06f12b0b7 Fixed license & copyright issues.
[SVN r36224]
2006-12-01 10:29:49 +00:00
f0bbb8b211 Add copyright, license
[SVN r35905]
2006-11-07 19:11:57 +00:00
d7a5408143 http://www.josuttis.com/ hasn't the latest version any more
[SVN r34855]
2006-08-08 18:49:30 +00:00
564e4029d0 Simplified code.
[SVN r34384]
2006-06-24 11:31:19 +00:00
0dc11c2f23 Fix compiler errors resulting from missing return values.
[SVN r34259]
2006-06-09 11:40:07 +00:00
7da1c4b310 Remove size zero support for old compilers that do not support partial template specialization
[SVN r34161]
2006-06-04 11:01:59 +00:00
8f10fdf27e Support for zero length arrays
[SVN r34154]
2006-06-03 12:51:13 +00:00
b41f7fdebe Merged from Version_1_33_1
[SVN r31949]
2005-12-08 03:23:02 +00:00
2fde5e334b License update
[SVN r24990]
2004-09-09 13:59:01 +00:00
4ee6e051fe *** empty log message ***
[SVN r24128]
2004-07-27 20:00:34 +00:00
bac6446111 added debug bounds checking; added new empty() definition
[SVN r24127]
2004-07-27 19:58:09 +00:00
cfb6d6eae6 array.xml: documentation updates from Nico
[SVN r22066]
2004-01-30 03:51:06 +00:00
0543286830 Installed some updates from Nicolai Josuttis
[SVN r22065]
2004-01-30 03:32:19 +00:00
10 changed files with 350 additions and 98 deletions

107
array0.cpp Normal file
View File

@ -0,0 +1,107 @@
/* tests for using class array<> specialization for size 0
* (C) Copyright Alisdair Meredith 2006.
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
#include <string>
#include <iostream>
#include <boost/array.hpp>
namespace {
unsigned int failed_tests = 0;
void fail_test( const char * reason ) {
++failed_tests;
std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl;
}
template< class T >
void BadValue( const T & )
{
fail_test( "Unexpected value" );
}
template< class T >
void RunTests()
{
typedef boost::array< T, 0 > test_type;
// Test value and aggegrate initialization
test_type test_case = {};
const boost::array< T, 0 > const_test_case = test_type();
test_case.assign( T() );
// front/back and operator[] must compile, but calling them is undefined
// Likewise, all tests below should evaluate to false, avoiding undefined behaviour
if( !test_case.empty() ) {
BadValue( test_case.front() );
}
if( !const_test_case.empty() ) {
BadValue( const_test_case.back() );
}
if( test_case.size() > 0 ) {
BadValue( test_case[ 0 ] );
}
if( const_test_case.max_size() > 0 ) {
BadValue( const_test_case[ 0 ] );
}
// Assert requirements of TR1 6.2.2.4
if( test_case.begin() != test_case.end() ) {
fail_test( "Not an empty range" );
}
if( const_test_case.begin() != const_test_case.end() ) {
fail_test( "Not an empty range" );
}
if( test_case.begin() == const_test_case.begin() ) {
fail_test( "iterators for different containers are not distinct" );
}
if( test_case.data() == const_test_case.data() ) {
// Value of data is unspecified in TR1, so no requirement this test pass or fail
// However, it must compile!
}
// Check can safely use all iterator types with std algorithms
std::for_each( test_case.begin(), test_case.end(), BadValue< T > );
std::for_each( test_case.rbegin(), test_case.rend(), BadValue< T > );
std::for_each( const_test_case.begin(), const_test_case.end(), BadValue< T > );
std::for_each( const_test_case.rbegin(), const_test_case.rend(), BadValue< T > );
// Check swap is well formed
std::swap( test_case, test_case );
// Check assigment operator and overloads are well formed
test_case = const_test_case;
// Confirm at() throws the std lib defined exception
try {
BadValue( test_case.at( 0 ) );
} catch ( const std::out_of_range & ) {
}
try {
BadValue( const_test_case.at( 0 ) );
} catch ( const std::out_of_range & ) {
}
}
}
int main()
{
RunTests< bool >();
RunTests< void * >();
RunTests< long double >();
RunTests< std::string >();
return failed_tests;
}

View File

@ -1,10 +1,9 @@
/* simple example for using class array<> /* simple example for using class array<>
* *
* (C) Copyright Nicolai M. Josuttis 2001. * (C) Copyright Nicolai M. Josuttis 2001.
* Permission to copy, use, modify, sell and distribute this software * Distributed under the Boost Software License, Version 1.0. (See
* is granted provided this copyright notice appears in all copies. * accompanying file LICENSE_1_0.txt or copy at
* This software is provided "as is" without express or implied * http://www.boost.org/LICENSE_1_0.txt)
* warranty, and with no claim as to its suitability for any purpose.
* *
* Changelog: * Changelog:
* 20 Jan 2001 - Removed boolalpha use since stock GCC doesn't support it * 20 Jan 2001 - Removed boolalpha use since stock GCC doesn't support it

View File

@ -1,9 +1,8 @@
/* example for using class array<> /* example for using class array<>
* (C) Copyright Nicolai M. Josuttis 2001. * (C) Copyright Nicolai M. Josuttis 2001.
* Permission to copy, use, modify, sell and distribute this software * Distributed under the Boost Software License, Version 1.0. (See
* is granted provided this copyright notice appears in all copies. * accompanying file LICENSE_1_0.txt or copy at
* This software is provided "as is" without express or implied * http://www.boost.org/LICENSE_1_0.txt)
* warranty, and with no claim as to its suitability for any purpose.
*/ */
#include <algorithm> #include <algorithm>

View File

@ -1,9 +1,8 @@
/* example for using class array<> /* example for using class array<>
* (C) Copyright Nicolai M. Josuttis 2001. * (C) Copyright Nicolai M. Josuttis 2001.
* Permission to copy, use, modify, sell and distribute this software * Distributed under the Boost Software License, Version 1.0. (See
* is granted provided this copyright notice appears in all copies. * accompanying file LICENSE_1_0.txt or copy at
* This software is provided "as is" without express or implied * http://www.boost.org/LICENSE_1_0.txt)
* warranty, and with no claim as to its suitability for any purpose.
*/ */
#include <string> #include <string>

View File

@ -1,9 +1,8 @@
/* example for using class array<> /* example for using class array<>
* (C) Copyright Nicolai M. Josuttis 2001. * (C) Copyright Nicolai M. Josuttis 2001.
* Permission to copy, use, modify, sell and distribute this software * Distributed under the Boost Software License, Version 1.0. (See
* is granted provided this copyright notice appears in all copies. * accompanying file LICENSE_1_0.txt or copy at
* This software is provided "as is" without express or implied * http://www.boost.org/LICENSE_1_0.txt)
* warranty, and with no claim as to its suitability for any purpose.
*/ */
#include <algorithm> #include <algorithm>

View File

@ -1,9 +1,8 @@
/* simple example for using class array<> /* simple example for using class array<>
* (C) Copyright Nicolai M. Josuttis 2001. * (C) Copyright Nicolai M. Josuttis 2001.
* Permission to copy, use, modify, sell and distribute this software * Distributed under the Boost Software License, Version 1.0. (See
* is granted provided this copyright notice appears in all copies. * accompanying file LICENSE_1_0.txt or copy at
* This software is provided "as is" without express or implied * http://www.boost.org/LICENSE_1_0.txt)
* warranty, and with no claim as to its suitability for any purpose.
*/ */
#include <iostream> #include <iostream>

View File

@ -10,15 +10,18 @@
<copyright> <copyright>
<year>2001</year> <year>2001</year>
<year>2002</year>
<year>2003</year>
<year>2004</year>
<holder>Nicolai M. Josuttis</holder> <holder>Nicolai M. Josuttis</holder>
</copyright> </copyright>
<legalnotice> <legalnotice>
<para>Permission to copy, use, modify, sell and distribute this <para>Distributed under the Boost Software License, Version 1.0.
software is granted provided this copyright notice appears in (See accompanying file <filename>LICENSE_1_0.txt</filename> or copy at
all copies. This software is provided "as is" without express or <ulink
implied warranty, and with no claim as to its suitability for url="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</ulink>)
any purpose.</para> </para>
</legalnotice> </legalnotice>
<librarypurpose>STL compliant container wrapper for arrays of constant size</librarypurpose> <librarypurpose>STL compliant container wrapper for arrays of constant size</librarypurpose>
@ -63,9 +66,13 @@
<para>After considering different names, we decided to name this <para>After considering different names, we decided to name this
class simply <code><classname>array</classname></code>.</para> class simply <code><classname>array</classname></code>.</para>
<para>Class <code><classname>array</classname></code> fulfills <para>Note that this class is suggested to be part of the next
most but not all of the requirements of "reversible containers" Technical Report, which will extend the C++ Standard (see
(see Section 23.1, [lib.container.requirements] of the C++ <ulink url="http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1548.htm">http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1548.htm</ulink>).</para>
<para>Class <code><classname>array</classname></code> fulfills most
but not all of the requirements of "reversible containers" (see
Section 23.1, [lib.container.requirements] of the C++
Standard). The reasons array is not an reversible STL container is Standard). The reasons array is not an reversible STL container is
because: because:
<itemizedlist spacing="compact"> <itemizedlist spacing="compact">
@ -276,6 +283,12 @@
<returns><simpara><code>elems</code></simpara></returns> <returns><simpara><code>elems</code></simpara></returns>
<throws><simpara>will not throw</simpara></throws> <throws><simpara>will not throw</simpara></throws>
</method> </method>
<method name="c_array">
<type>T*</type>
<returns><simpara><code>elems</code></simpara></returns>
<throws><simpara>will not throw</simpara></throws>
</method>
</method-group> </method-group>
<method-group name="modifiers"> <method-group name="modifiers">
@ -469,51 +482,39 @@
<simpara>Note that if there are fewer elements in the <simpara>Note that if there are fewer elements in the
initializer list, then each remaining element gets initializer list, then each remaining element gets
default-initialized (thus, it has a defined value). However, default-initialized (thus, it has a defined value).</simpara>
passing no initializer list means that the elements have an </listitem></itemizedlist></para>
indetermined initial value.</simpara></listitem>
<listitem><simpara>It has no user-declared constructors.</simpara></listitem> <para>However, this approach has its drawbacks: <emphasis
<listitem><simpara>It has no private or protected non-static data members.</simpara></listitem> role="bold"> passing no initializer list means that the elements
<listitem><simpara>It has no base classes.</simpara></listitem> have an indetermined initial value</emphasis>, because the rule says
<listitem><simpara>It has no virtual functions.</simpara></listitem> that aggregates may have:
<itemizedlist>
<listitem><simpara>No user-declared constructors.</simpara></listitem>
<listitem><simpara>No private or protected non-static data members.</simpara></listitem>
<listitem><simpara>No base classes.</simpara></listitem>
<listitem><simpara>No virtual functions.</simpara></listitem>
</itemizedlist> </itemizedlist>
</para> </para>
<para>The current implementation uses this approach. However, being
able to have indeterminate initial values is a big drawback. So,
please give me some feedback, how useful you consider this feature
to be.</para>
</section>
<section id="array.issues"> <para>Nevertheless, The current implementation uses this approach.</para>
<title>Open Issues</title>
<itemizedlist> <para>Note that for standard conforming compilers it is possible to
use fewer braces (according to 8.5.1 (11) of the Standard). That is,
you can initialize an array as follows:</para>
<listitem> <programlisting>
<simpara> Do we want initializer list support or would the <classname>boost::array</classname>&lt;int,4&gt; a = { 1, 2, 3 };
following be OK?:</simpara> </programlisting>
<programlisting>int data[] = { 1, 2, 3, 4 } <para>I'd appreciate any constructive feedback. <emphasis
<classname>boost::array</classname>&lt;int,5&gt; x(data); // or <classname>boost::array</classname>&lt;int,data&gt; x;</programlisting> role="bold">Please note: I don't have time to read all boost
</listitem> mails. Thus, to make sure that feedback arrives to me, please send
me a copy of each mail regarding this class.</emphasis></para>
<listitem><simpara>Could "{ ... }" be used portably instead of "{ <para>The code is provided "as is" without expressed or implied
{ ... } }" to initialize values? 8.5.1 (11) of the Standard seems warranty.</para>
to allow it; however, gcc 2.95.2 prints a warning
message.</simpara></listitem>
<listitem><simpara>Any way to have determinate initial values and
initializer list support?</simpara></listitem>
<listitem><simpara>Static_casts for reverse iterator stuff?</simpara></listitem>
</itemizedlist>
<para>I'd appreciate any constructive feedback. Please note: I don't
have time to read all boost mails. Thus, to make sure that <ulink
url="mailto:solutions@josuttis.com">feedback</ulink> arrives me,
please send me a copy of each mail regarding this class.</para>
</section> </section>
<section id="array.more.info"> <section id="array.more.info">

View File

@ -2,36 +2,41 @@
* an STL container (as wrapper) for arrays of constant size. * an STL container (as wrapper) for arrays of constant size.
* *
* See * See
* http://www.josuttis.com/cppcode * http://www.boost.org/libs/array/
* for details and the latest version. * for documentation.
*
* The original author site is at: http://www.josuttis.com/
* *
* (C) Copyright Nicolai M. Josuttis 2001. * (C) Copyright Nicolai M. Josuttis 2001.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
* *
* Distributed under the Boost Software License, Version 1.0. (See
* accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*
* 29 Jan 2004 - c_array() added, BOOST_NO_PRIVATE_IN_AGGREGATE removed (Nico Josuttis)
* 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries. * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
* 05 Aug 2001 - minor update (Nico Josuttis) * 05 Aug 2001 - minor update (Nico Josuttis)
* 20 Jan 2001 - STLport fix (Beman Dawes) * 20 Jan 2001 - STLport fix (Beman Dawes)
* 29 Sep 2000 - Initial Revision (Nico Josuttis) * 29 Sep 2000 - Initial Revision (Nico Josuttis)
*
* Jan 29, 2004
*/ */
// See http://www.boost.org/libs/array for Documentation.
#ifndef BOOST_ARRAY_HPP #ifndef BOOST_ARRAY_HPP
#define BOOST_ARRAY_HPP #define BOOST_ARRAY_HPP
#include <cstddef> #include <cstddef>
#include <stdexcept> #include <stdexcept>
#include <boost/assert.hpp>
// Handles broken standard libraries better than <iterator> // Handles broken standard libraries better than <iterator>
#include <boost/detail/iterator.hpp> #include <boost/detail/iterator.hpp>
#include <boost/throw_exception.hpp>
#include <algorithm> #include <algorithm>
// FIXES for broken compilers // FIXES for broken compilers
#include <boost/config.hpp> #include <boost/config.hpp>
namespace boost { namespace boost {
template<class T, std::size_t N> template<class T, std::size_t N>
@ -48,7 +53,7 @@ namespace boost {
typedef const T& const_reference; typedef const T& const_reference;
typedef std::size_t size_type; typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type; typedef std::ptrdiff_t difference_type;
// iterator support // iterator support
iterator begin() { return elems; } iterator begin() { return elems; }
const_iterator begin() const { return elems; } const_iterator begin() const { return elems; }
@ -81,18 +86,42 @@ namespace boost {
} }
// operator[] // operator[]
reference operator[](size_type i) { return elems[i]; } reference operator[](size_type i)
const_reference operator[](size_type i) const { return elems[i]; } {
BOOST_ASSERT( i < N && "out of range" );
return elems[i];
}
const_reference operator[](size_type i) const
{
BOOST_ASSERT( i < N && "out of range" );
return elems[i];
}
// at() with range check // at() with range check
reference at(size_type i) { rangecheck(i); return elems[i]; } reference at(size_type i) { rangecheck(i); return elems[i]; }
const_reference at(size_type i) const { rangecheck(i); return elems[i]; } const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
// front() and back() // front() and back()
reference front() { return elems[0]; } reference front()
const_reference front() const { return elems[0]; } {
reference back() { return elems[N-1]; } return elems[0];
const_reference back() const { return elems[N-1]; } }
const_reference front() const
{
return elems[0];
}
reference back()
{
return elems[N-1];
}
const_reference back() const
{
return elems[N-1];
}
// size is constant // size is constant
static size_type size() { return N; } static size_type size() { return N; }
@ -105,8 +134,12 @@ namespace boost {
std::swap_ranges(begin(),end(),y.begin()); std::swap_ranges(begin(),end(),y.begin());
} }
// direct access to data // direct access to data (read-only)
const T* data() const { return elems; } const T* data() const { return elems; }
T* data() { return elems; }
// use array as C array (direct read/write access to data)
T* c_array() { return elems; }
// assignment with type conversion // assignment with type conversion
template <typename T2> template <typename T2>
@ -121,16 +154,136 @@ namespace boost {
std::fill_n(begin(),size(),value); std::fill_n(begin(),size(),value);
} }
#ifndef BOOST_NO_PRIVATE_IN_AGGREGATE
private:
#endif
// check range (may be private because it is static) // check range (may be private because it is static)
static void rangecheck (size_type i) { static void rangecheck (size_type i) {
if (i >= size()) { throw std::range_error("array"); } if (i >= size()) {
throw std::out_of_range("array<>: index out of range");
}
} }
}; };
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
template< class T >
class array< T, 0 > {
public:
// type definitions
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef T& reference;
typedef const T& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
// iterator support
iterator begin() { return iterator( reinterpret_cast< T * >( this ) ); }
const_iterator begin() const { return const_iterator( reinterpret_cast< const T * >( this ) ); }
iterator end() { return begin(); }
const_iterator end() const { return begin(); }
// reverse iterator support
#if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
#elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
// workaround for broken reverse_iterator in VC7
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
reference, iterator, reference> > reverse_iterator;
typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
const_reference, iterator, reference> > const_reverse_iterator;
#else
// workaround for broken reverse_iterator implementations
typedef std::reverse_iterator<iterator,T> reverse_iterator;
typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
#endif
reverse_iterator rbegin() { return reverse_iterator(end()); }
const_reverse_iterator rbegin() const {
return const_reverse_iterator(end());
}
reverse_iterator rend() { return reverse_iterator(begin()); }
const_reverse_iterator rend() const {
return const_reverse_iterator(begin());
}
// operator[]
reference operator[](size_type i)
{
return failed_rangecheck();
}
const_reference operator[](size_type i) const
{
return failed_rangecheck();
}
// at() with range check
reference at(size_type i) { return failed_rangecheck(); }
const_reference at(size_type i) const { return failed_rangecheck(); }
// front() and back()
reference front()
{
return failed_rangecheck();
}
const_reference front() const
{
return failed_rangecheck();
}
reference back()
{
return failed_rangecheck();
}
const_reference back() const
{
return failed_rangecheck();
}
// size is constant
static size_type size() { return 0; }
static bool empty() { return true; }
static size_type max_size() { return 0; }
enum { static_size = 0 };
void swap (array<T,0>& y) {
}
// direct access to data (read-only)
const T* data() const { return 0; }
T* data() { return 0; }
// use array as C array (direct read/write access to data)
T* c_array() { return 0; }
// assignment with type conversion
template <typename T2>
array<T,0>& operator= (const array<T2,0>& ) {
return *this;
}
// assign one value to all elements
void assign (const T& ) { }
// check range (may be private because it is static)
static reference failed_rangecheck () {
std::out_of_range e("attempt to access element of an empty array");
boost::throw_exception(e);
//
// We need to return something here to keep
// some compilers happy: however we will never
// actually get here....
//
static T placeholder;
return placeholder;
}
};
#endif
// comparisons // comparisons
template<class T, std::size_t N> template<class T, std::size_t N>
bool operator== (const array<T,N>& x, const array<T,N>& y) { bool operator== (const array<T,N>& x, const array<T,N>& y) {
@ -166,10 +319,3 @@ namespace boost {
} /* namespace boost */ } /* namespace boost */
#endif /*BOOST_ARRAY_HPP*/ #endif /*BOOST_ARRAY_HPP*/

View File

@ -4,6 +4,10 @@
</head> </head>
<body> <body>
Automatic redirection failed, please go to Automatic redirection failed, please go to
<a href="../../doc/html/array.html">../../doc/html/array.html</a> <a href="../../doc/html/array.html">../../doc/html/array.html</a> &nbsp;<hr>
<p><EFBFBD> Copyright Beman Dawes, 2001</p>
<p>Distributed under the Boost Software License, Version 1.0. (See accompanying
file <a href="../../LICENSE_1_0.txt">LICENSE_1_0.txt</a> or copy
at <a href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</a>)</p>
</body> </body>
</html> </html>

View File

@ -3,10 +3,9 @@
* by Nicolai M. Josuttis, Addison-Wesley, 1999 * by Nicolai M. Josuttis, Addison-Wesley, 1999
* *
* (C) Copyright Nicolai M. Josuttis 1999. * (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software * Distributed under the Boost Software License, Version 1.0. (See
* is granted provided this copyright notice appears in all copies. * accompanying file LICENSE_1_0.txt or copy at
* This software is provided "as is" without express or implied * http://www.boost.org/LICENSE_1_0.txt)
* warranty, and with no claim as to its suitability for any purpose.
*/ */
#include <iostream> #include <iostream>