mirror of
https://github.com/boostorg/array.git
synced 2025-07-01 15:01:05 +02:00
Compare commits
7 Commits
svn-branch
...
boost-1.33
Author | SHA1 | Date | |
---|---|---|---|
196c8da594 | |||
63bacedc41 | |||
2fde5e334b | |||
4ee6e051fe | |||
bac6446111 | |||
cfb6d6eae6 | |||
0543286830 |
@ -1,10 +1,9 @@
|
||||
/* simple example for using class array<>
|
||||
*
|
||||
* (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)
|
||||
*
|
||||
* Changelog:
|
||||
* 20 Jan 2001 - Removed boolalpha use since stock GCC doesn't support it
|
||||
|
@ -1,9 +1,8 @@
|
||||
/* example for using class array<>
|
||||
* (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)
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
|
@ -1,9 +1,8 @@
|
||||
/* example for using class array<>
|
||||
* (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)
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
|
@ -1,9 +1,8 @@
|
||||
/* example for using class array<>
|
||||
* (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)
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
|
@ -1,9 +1,8 @@
|
||||
/* simple example for using class array<>
|
||||
* (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)
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
|
@ -10,6 +10,9 @@
|
||||
|
||||
<copyright>
|
||||
<year>2001</year>
|
||||
<year>2002</year>
|
||||
<year>2003</year>
|
||||
<year>2004</year>
|
||||
<holder>Nicolai M. Josuttis</holder>
|
||||
</copyright>
|
||||
|
||||
@ -63,9 +66,13 @@
|
||||
<para>After considering different names, we decided to name this
|
||||
class simply <code><classname>array</classname></code>.</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++
|
||||
<para>Note that this class is suggested to be part of the next
|
||||
Technical Report, which will extend the C++ Standard (see
|
||||
<ulink>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
|
||||
because:
|
||||
<itemizedlist spacing="compact">
|
||||
@ -276,6 +283,12 @@
|
||||
<returns><simpara><code>elems</code></simpara></returns>
|
||||
<throws><simpara>will not throw</simpara></throws>
|
||||
</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 name="modifiers">
|
||||
@ -469,51 +482,39 @@
|
||||
|
||||
<simpara>Note that if there are fewer elements in the
|
||||
initializer list, then each remaining element gets
|
||||
default-initialized (thus, it has a defined value). However,
|
||||
passing no initializer list means that the elements have an
|
||||
indetermined initial value.</simpara></listitem>
|
||||
default-initialized (thus, it has a defined value).</simpara>
|
||||
</listitem></itemizedlist></para>
|
||||
|
||||
<listitem><simpara>It has no user-declared constructors.</simpara></listitem>
|
||||
<listitem><simpara>It has no private or protected non-static data members.</simpara></listitem>
|
||||
<listitem><simpara>It has no base classes.</simpara></listitem>
|
||||
<listitem><simpara>It has no virtual functions.</simpara></listitem>
|
||||
<para>However, this approach has its drawbacks: <emphasis
|
||||
role="bold"> passing no initializer list means that the elements
|
||||
have an indetermined initial value</emphasis>, because the rule says
|
||||
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>
|
||||
|
||||
</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">
|
||||
<title>Open Issues</title>
|
||||
<para>Nevertheless, The current implementation uses this approach.</para>
|
||||
|
||||
<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>
|
||||
<simpara> Do we want initializer list support or would the
|
||||
following be OK?:</simpara>
|
||||
<programlisting>
|
||||
<classname>boost::array</classname><int,4> a = { 1, 2, 3 };
|
||||
</programlisting>
|
||||
|
||||
<programlisting>int data[] = { 1, 2, 3, 4 }
|
||||
<classname>boost::array</classname><int,5> x(data); // or <classname>boost::array</classname><int,data> x;</programlisting>
|
||||
</listitem>
|
||||
<para>I'd appreciate any constructive feedback. <emphasis
|
||||
role="bold">Please note: I don't have time to read all boost
|
||||
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 "{
|
||||
{ ... } }" to initialize values? 8.5.1 (11) of the Standard seems
|
||||
to allow it; however, gcc 2.95.2 prints a warning
|
||||
message.</simpara></listitem>
|
||||
<para>The code is provided "as is" without expressed or implied
|
||||
warranty.</para>
|
||||
|
||||
<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 id="array.more.info">
|
||||
|
@ -4,26 +4,29 @@
|
||||
* See
|
||||
* http://www.josuttis.com/cppcode
|
||||
* for details and the latest version.
|
||||
* See
|
||||
* http://www.boost.org/libs/array for Documentation.
|
||||
* for documentation.
|
||||
*
|
||||
* (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.
|
||||
* 05 Aug 2001 - minor update (Nico Josuttis)
|
||||
* 20 Jan 2001 - STLport fix (Beman Dawes)
|
||||
* 29 Sep 2000 - Initial Revision (Nico Josuttis)
|
||||
*
|
||||
* Jan 29, 2004
|
||||
*/
|
||||
|
||||
// See http://www.boost.org/libs/array for Documentation.
|
||||
|
||||
#ifndef BOOST_ARRAY_HPP
|
||||
#define BOOST_ARRAY_HPP
|
||||
|
||||
#include <cstddef>
|
||||
#include <stdexcept>
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
// Handles broken standard libraries better than <iterator>
|
||||
#include <boost/detail/iterator.hpp>
|
||||
@ -32,6 +35,7 @@
|
||||
// FIXES for broken compilers
|
||||
#include <boost/config.hpp>
|
||||
|
||||
|
||||
namespace boost {
|
||||
|
||||
template<class T, std::size_t N>
|
||||
@ -81,18 +85,42 @@ namespace boost {
|
||||
}
|
||||
|
||||
// operator[]
|
||||
reference operator[](size_type i) { return elems[i]; }
|
||||
const_reference operator[](size_type i) const { return elems[i]; }
|
||||
reference operator[](size_type 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
|
||||
reference at(size_type i) { rangecheck(i); return elems[i]; }
|
||||
const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
|
||||
|
||||
// front() and back()
|
||||
reference front() { return elems[0]; }
|
||||
const_reference front() const { return elems[0]; }
|
||||
reference back() { return elems[N-1]; }
|
||||
const_reference back() const { return elems[N-1]; }
|
||||
reference front()
|
||||
{
|
||||
return elems[0];
|
||||
}
|
||||
|
||||
const_reference front() const
|
||||
{
|
||||
return elems[0];
|
||||
}
|
||||
|
||||
reference back()
|
||||
{
|
||||
return elems[N-1];
|
||||
}
|
||||
|
||||
const_reference back() const
|
||||
{
|
||||
return elems[N-1];
|
||||
}
|
||||
|
||||
// size is constant
|
||||
static size_type size() { return N; }
|
||||
@ -105,9 +133,12 @@ namespace boost {
|
||||
std::swap_ranges(begin(),end(),y.begin());
|
||||
}
|
||||
|
||||
// direct access to data
|
||||
// direct access to data (read-only)
|
||||
const T* data() const { return elems; }
|
||||
|
||||
// use array as C array (direct read/write access to data)
|
||||
T* c_array() { return elems; }
|
||||
|
||||
// assignment with type conversion
|
||||
template <typename T2>
|
||||
array<T,N>& operator= (const array<T2,N>& rhs) {
|
||||
@ -121,12 +152,11 @@ namespace boost {
|
||||
std::fill_n(begin(),size(),value);
|
||||
}
|
||||
|
||||
#ifndef BOOST_NO_PRIVATE_IN_AGGREGATE
|
||||
private:
|
||||
#endif
|
||||
// check range (may be private because it is static)
|
||||
static void rangecheck (size_type i) {
|
||||
if (i >= size()) { throw std::range_error("array"); }
|
||||
if (i >= size()) {
|
||||
throw std::range_error("array<>: index out of range");
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
@ -166,10 +196,3 @@ namespace boost {
|
||||
} /* namespace boost */
|
||||
|
||||
#endif /*BOOST_ARRAY_HPP*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -3,10 +3,9 @@
|
||||
* by Nicolai M. Josuttis, Addison-Wesley, 1999
|
||||
*
|
||||
* (C) Copyright Nicolai M. Josuttis 1999.
|
||||
* 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)
|
||||
*/
|
||||
#include <iostream>
|
||||
|
||||
|
Reference in New Issue
Block a user