1 <?xml version=
"1.0" encoding=
"utf-8"?>
2 <!DOCTYPE library PUBLIC
"-//Boost//DTD BoostBook XML V1.0//EN"
3 "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
4 <library name=
"Array" dirname=
"array" id=
"array" last-revision=
"$Date$">
7 <firstname>Nicolai
</firstname>
8 <surname>Josuttis
</surname>
16 <holder>Nicolai M. Josuttis
</holder>
20 <para>Distributed under the Boost Software License, Version
1.0.
21 (See accompanying file
<filename>LICENSE_1_0.txt
</filename> or copy at
23 url=
"http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt
</ulink>)
27 <librarypurpose>STL compliant container wrapper for arrays of constant size
</librarypurpose>
28 <librarycategory name=
"category:containers"/>
31 <title>Boost.Array
</title>
33 <section id=
"array.intro">
34 <title>Introduction
</title>
36 <using-namespace name=
"boost"/>
37 <using-class name=
"array"/>
39 <para>The C++ Standard Template Library STL as part of the C++
40 Standard Library provides a framework for processing algorithms on
41 different kind of containers. However, ordinary arrays don't
42 provide the interface of STL containers (although, they provide
43 the iterator interface of STL containers).
</para>
45 <para>As replacement for ordinary arrays, the STL provides class
46 <code><classname>std::vector
</classname></code>. However,
47 <code><classname>std::vector
<></classname></code> provides
48 the semantics of dynamic arrays. Thus, it manages data to be able
49 to change the number of elements. This results in some overhead in
50 case only arrays with static size are needed.
</para>
52 <para>In his book,
<emphasis>Generic Programming and the
53 STL
</emphasis>, Matthew H. Austern introduces a useful wrapper
54 class for ordinary arrays with static size, called
55 <code>block
</code>. It is safer and has no worse performance than
56 ordinary arrays. In
<emphasis>The C++ Programming
57 Language
</emphasis>,
3rd edition, Bjarne Stroustrup introduces a
58 similar class, called
<code>c_array
</code>, which I (
<ulink
59 url=
"http://www.josuttis.com">Nicolai Josuttis
</ulink>) present
60 slightly modified in my book
<emphasis>The C++ Standard Library -
61 A Tutorial and Reference
</emphasis>, called
62 <code>carray
</code>. This is the essence of these approaches
63 spiced with many feedback from
<ulink
64 url=
"http://www.boost.org">boost
</ulink>.
</para>
66 <para>After considering different names, we decided to name this
67 class simply
<code><classname>array
</classname></code>.
</para>
69 <para>Note that this class is suggested to be part of the next
70 Technical Report, which will extend the C++ Standard (see
71 <ulink url=
"http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1548.htm">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/
2003/n1548.htm
</ulink>).
</para>
73 <para>Update:
<code>std::array
</code> is (as of C++
11) part of the C++ standard.
74 The differences between
<code>boost::array
</code> and
<code>std::array
</code> are minimal.
75 If you are using C++
11, you should consider using
<code>std::array
</code> instead of
<code>boost::array
</code>.
78 <para>Class
<code><classname>array
</classname></code> fulfills most
79 but not all of the requirements of
"reversible containers" (see
80 Section
23.1, [lib.container.requirements] of the C++
81 Standard). The reasons array is not an reversible STL container is
83 <itemizedlist spacing=
"compact">
84 <listitem><simpara>No constructors are provided.
</simpara></listitem>
85 <listitem><simpara>Elements may have an undetermined initial value (see
<xref linkend=
"array.rationale"/>).
</simpara></listitem>
86 <listitem><simpara><functionname>swap
</functionname>() has no constant complexity.
</simpara></listitem>
87 <listitem><simpara><methodname>size
</methodname>() is always constant, based on the second template argument of the type.
</simpara></listitem>
88 <listitem><simpara>The container provides no allocator support.
</simpara></listitem>
92 <para>It doesn't fulfill the requirements of a
"sequence" (see Section
23.1.1, [lib.sequence.reqmts] of the C++ Standard), except that:
93 <itemizedlist spacing=
"compact">
94 <listitem><simpara><methodname>front
</methodname>() and
<methodname>back
</methodname>() are provided.
</simpara></listitem>
95 <listitem><simpara><methodname>operator[]
</methodname> and
<methodname>at
</methodname>() are provided.
</simpara></listitem>
101 <header name=
"boost/array.hpp">
102 <namespace name=
"boost">
105 <template-type-parameter name=
"T"/>
106 <template-nontype-parameter name=
"N">
107 <type>std::size_t
</type>
108 </template-nontype-parameter>
111 <purpose><para>STL compliant container wrapper for arrays of constant size
</para></purpose>
112 <typedef name=
"value_type">
115 <typedef name=
"iterator">
118 <typedef name=
"const_iterator">
119 <type>const T*
</type>
121 <typedef name=
"reverse_iterator">
122 <type><classname>std::reverse_iterator
</classname><iterator
></type>
124 <typedef name=
"const_reverse_iterator">
125 <type><classname>std::reverse_iterator
</classname><const_iterator
></type>
127 <typedef name=
"reference">
130 <typedef name=
"const_reference">
131 <type>const T
&</type>
133 <typedef name=
"size_type">
134 <type>std::size_t
</type>
136 <typedef name=
"difference_type">
137 <type>std::ptrdiff_t
</type>
140 <static-constant name=
"static_size">
141 <type>size_type
</type>
147 <template-type-parameter name=
"U"/>
149 <parameter name=
"other">
150 <paramtype>const
<classname>array
</classname><U, N
>&</paramtype>
152 <effects><simpara><code>std::copy(rhs.
<methodname>begin
</methodname>(),rhs.
<methodname>end
</methodname>(),
<methodname>begin
</methodname>())
</code></simpara></effects>
155 <method-group name=
"iterator support">
156 <overloaded-method name=
"begin">
158 <type>iterator
</type>
160 <signature cv=
"const">
161 <type>const_iterator
</type>
164 <returns><simpara>iterator for the first element
</simpara></returns>
165 <throws><simpara>will not throw
</simpara></throws>
168 <overloaded-method name=
"end">
170 <type>iterator
</type>
172 <signature cv=
"const">
173 <type>const_iterator
</type>
176 <returns><simpara>iterator for position after the last element
</simpara></returns>
177 <throws><simpara>will not throw
</simpara></throws>
181 <method-group name=
"reverse iterator support">
182 <overloaded-method name=
"rbegin">
184 <type>reverse_iterator
</type>
186 <signature cv=
"const">
187 <type>const_reverse_iterator
</type>
190 <returns><simpara>reverse iterator for the first element of reverse iteration
</simpara></returns>
193 <overloaded-method name=
"rend">
195 <type>reverse_iterator
</type>
197 <signature cv=
"const">
198 <type>const_reverse_iterator
</type>
201 <returns><simpara>reverse iterator for position after the last element in reverse iteration
</simpara></returns>
205 <method-group name=
"capacity">
207 <type>size_type
</type>
208 <returns><simpara><code>N
</code></simpara></returns>
210 <method name=
"empty">
212 <returns><simpara><code>N==
0</code></simpara></returns>
213 <throws><simpara>will not throw
</simpara></throws>
215 <method name=
"max_size">
216 <type>size_type
</type>
217 <returns><simpara><code>N
</code></simpara></returns>
218 <throws><simpara>will not throw
</simpara></throws>
222 <method-group name=
"element access">
223 <overloaded-method name=
"operator[]">
225 <type>reference
</type>
227 <paramtype>size_type
</paramtype>
231 <signature cv=
"const">
232 <type>const_reference
</type>
234 <paramtype>size_type
</paramtype>
238 <requires><simpara><code>i
< N
</code></simpara></requires>
239 <returns><simpara>element with index
<code>i
</code></simpara></returns>
240 <throws><simpara>will not throw.
</simpara></throws>
243 <overloaded-method name=
"at">
245 <type>reference
</type>
247 <paramtype>size_type
</paramtype>
251 <signature cv=
"const">
252 <type>const_reference
</type>
254 <paramtype>size_type
</paramtype>
258 <returns><simpara>element with index
<code>i
</code></simpara></returns>
259 <throws><simpara><code><classname>std::range_error
</classname></code> if
<code>i
>= N
</code></simpara></throws>
262 <overloaded-method name=
"front">
264 <type>reference
</type>
266 <signature cv=
"const">
267 <type>const_reference
</type>
269 <requires><simpara><code>N
> 0</code></simpara></requires>
270 <returns><simpara>the first element
</simpara></returns>
271 <throws><simpara>will not throw
</simpara></throws>
274 <overloaded-method name=
"back">
276 <type>reference
</type>
278 <signature cv=
"const">
279 <type>const_reference
</type>
281 <requires><simpara><code>N
> 0</code></simpara></requires>
282 <returns><simpara>the last element
</simpara></returns>
283 <throws><simpara>will not throw
</simpara></throws>
286 <method name=
"data" cv=
"const">
287 <type>const T*
</type>
288 <returns><simpara><code>elems
</code></simpara></returns>
289 <throws><simpara>will not throw
</simpara></throws>
292 <method name=
"c_array">
294 <returns><simpara><code>elems
</code></simpara></returns>
295 <throws><simpara>will not throw
</simpara></throws>
299 <method-group name=
"modifiers">
302 <parameter name=
"other">
303 <paramtype><classname>array
</classname><T, N
>&</paramtype>
305 <effects><simpara><code>std::swap_ranges(
<methodname>begin
</methodname>(),
<methodname>end
</methodname>(), other.
<methodname>begin
</methodname>())
</code></simpara></effects>
306 <complexity><simpara>linear in
<code>N
</code></simpara></complexity>
308 <method name=
"assign">
310 <parameter name=
"value">
311 <paramtype>const T
&</paramtype>
313 <effects><simpara><code>std::fill_n(
<methodname>begin
</methodname>(), N, value)
</code></simpara></effects>
317 <data-member name=
"elems[N]"> <!-- HACK -->
321 <free-function-group name=
"specialized algorithms">
322 <function name=
"swap">
324 <template-type-parameter name=
"T"/>
325 <template-nontype-parameter name=
"N">
326 <type>std::size_t
</type>
327 </template-nontype-parameter>
333 <paramtype><classname>array
</classname><T, N
>&</paramtype>
336 <paramtype><classname>array
</classname><T, N
>&</paramtype>
339 <effects><simpara><code>x.
<methodname>swap
</methodname>(y)
</code></simpara></effects>
340 <throws><simpara>will not throw.
</simpara></throws>
342 </free-function-group>
344 <free-function-group name=
"comparisons">
345 <function name=
"operator==">
347 <template-type-parameter name=
"T"/>
348 <template-nontype-parameter name=
"N">
349 <type>std::size_t
</type>
350 </template-nontype-parameter>
356 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
359 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
362 <returns><simpara><code>std::equal(x.
<methodname>begin
</methodname>(), x.
<methodname>end
</methodname>(), y.
<methodname>begin
</methodname>())
</code></simpara>
366 <function name=
"operator!=">
368 <template-type-parameter name=
"T"/>
369 <template-nontype-parameter name=
"N">
370 <type>std::size_t
</type>
371 </template-nontype-parameter>
377 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
380 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
383 <returns><simpara><code>!(x == y)
</code></simpara>
387 <function name=
"operator<">
389 <template-type-parameter name=
"T"/>
390 <template-nontype-parameter name=
"N">
391 <type>std::size_t
</type>
392 </template-nontype-parameter>
398 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
401 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
404 <returns><simpara><code>std::lexicographical_compare(x.
<methodname>begin
</methodname>(), x.
<methodname>end
</methodname>(), y.
<methodname>begin
</methodname>(), y.
<methodname>end
</methodname>())
</code></simpara>
408 <function name=
"operator>">
410 <template-type-parameter name=
"T"/>
411 <template-nontype-parameter name=
"N">
412 <type>std::size_t
</type>
413 </template-nontype-parameter>
419 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
422 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
425 <returns><simpara><code>y
< x
</code></simpara></returns>
428 <function name=
"operator<=">
430 <template-type-parameter name=
"T"/>
431 <template-nontype-parameter name=
"N">
432 <type>std::size_t
</type>
433 </template-nontype-parameter>
439 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
442 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
445 <returns><simpara><code>!(y
< x)
</code></simpara></returns>
448 <function name=
"operator>=">
450 <template-type-parameter name=
"T"/>
451 <template-nontype-parameter name=
"N">
452 <type>std::size_t
</type>
453 </template-nontype-parameter>
459 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
462 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
465 <returns><simpara><code>!(x
< y)
</code></simpara></returns>
467 </free-function-group>
473 <section id=
"array.rationale">
474 <title>Design Rationale
</title>
476 <para>There was an important design tradeoff regarding the
477 constructors: We could implement array as an
"aggregate" (see
478 Section
8.5.1, [dcl.init.aggr], of the C++ Standard). This would
481 <listitem><simpara>An array can be initialized with a
482 brace-enclosing, comma-separated list of initializers for the
483 elements of the container, written in increasing subscript
486 <programlisting><classname>boost::array
</classname><int,
4> a = { {
1,
2,
3 } };
</programlisting>
488 <simpara>Note that if there are fewer elements in the
489 initializer list, then each remaining element gets
490 default-initialized (thus, it has a defined value).
</simpara>
491 </listitem></itemizedlist></para>
493 <para>However, this approach has its drawbacks:
<emphasis
494 role=
"bold"> passing no initializer list means that the elements
495 have an indetermined initial value
</emphasis>, because the rule says
496 that aggregates may have:
498 <listitem><simpara>No user-declared constructors.
</simpara></listitem>
499 <listitem><simpara>No private or protected non-static data members.
</simpara></listitem>
500 <listitem><simpara>No base classes.
</simpara></listitem>
501 <listitem><simpara>No virtual functions.
</simpara></listitem>
505 <para>Nevertheless, The current implementation uses this approach.
</para>
507 <para>Note that for standard conforming compilers it is possible to
508 use fewer braces (according to
8.5.1 (
11) of the Standard). That is,
509 you can initialize an array as follows:
</para>
512 <classname>boost::array
</classname><int,
4> a = {
1,
2,
3 };
515 <para>I'd appreciate any constructive feedback.
<emphasis
516 role=
"bold">Please note: I don't have time to read all boost
517 mails. Thus, to make sure that feedback arrives to me, please send
518 me a copy of each mail regarding this class.
</emphasis></para>
520 <para>The code is provided
"as is" without expressed or implied
525 <section id=
"array.more.info">
526 <title>For more information...
</title>
527 <para>To find more details about using ordinary arrays in C++ and
528 the framework of the STL, see e.g.
530 <literallayout>The C++ Standard Library - A Tutorial and Reference
531 by Nicolai M. Josuttis
532 Addison Wesley Longman,
1999
533 ISBN
0-
201-
37926-
0</literallayout>
536 <para><ulink url=
"http://www.josuttis.com/">Home Page of Nicolai
537 Josuttis
</ulink></para>
540 <section id=
"array.ack">
541 <title>Acknowledgements
</title>
543 <para>Doug Gregor ported the documentation to the BoostBook format.
</para>
547 empty() should return N != 0
548 size(), empty(), max_size() should be const