1 <?xml version=
"1.0" encoding=
"utf-8"?>
3 Copyright 2010 Daniel James.
5 Distributed under the Boost Software License, Version 1.0.
6 (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
9 <!DOCTYPE library PUBLIC
"-//Boost//DTD BoostBook XML V1.0//EN"
10 "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
11 <library name=
"Array" dirname=
"array" id=
"array" last-revision=
"$Date$">
14 <firstname>Nicolai
</firstname>
15 <surname>Josuttis
</surname>
23 <holder>Nicolai M. Josuttis
</holder>
27 <para>Distributed under the Boost Software License, Version
1.0.
28 (See accompanying file
<filename>LICENSE_1_0.txt
</filename> or copy at
30 url=
"http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt
</ulink>)
34 <librarypurpose>STL compliant container wrapper for arrays of constant size
</librarypurpose>
35 <librarycategory name=
"category:containers"/>
38 <title>Boost.Array
</title>
40 <section id=
"array.intro">
41 <title>Introduction
</title>
43 <using-namespace name=
"boost"/>
44 <using-class name=
"array"/>
46 <para>The C++ Standard Template Library STL as part of the C++
47 Standard Library provides a framework for processing algorithms on
48 different kind of containers. However, ordinary arrays don't
49 provide the interface of STL containers (although, they provide
50 the iterator interface of STL containers).
</para>
52 <para>As replacement for ordinary arrays, the STL provides class
53 <code><classname>std::vector
</classname></code>. However,
54 <code><classname>std::vector
<></classname></code> provides
55 the semantics of dynamic arrays. Thus, it manages data to be able
56 to change the number of elements. This results in some overhead in
57 case only arrays with static size are needed.
</para>
59 <para>In his book,
<emphasis>Generic Programming and the
60 STL
</emphasis>, Matthew H. Austern introduces a useful wrapper
61 class for ordinary arrays with static size, called
62 <code>block
</code>. It is safer and has no worse performance than
63 ordinary arrays. In
<emphasis>The C++ Programming
64 Language
</emphasis>,
3rd edition, Bjarne Stroustrup introduces a
65 similar class, called
<code>c_array
</code>, which I (
<ulink
66 url=
"http://www.josuttis.com">Nicolai Josuttis
</ulink>) present
67 slightly modified in my book
<emphasis>The C++ Standard Library -
68 A Tutorial and Reference
</emphasis>, called
69 <code>carray
</code>. This is the essence of these approaches
70 spiced with many feedback from
<ulink
71 url=
"http://www.boost.org">boost
</ulink>.
</para>
73 <para>After considering different names, we decided to name this
74 class simply
<code><classname>array
</classname></code>.
</para>
76 <para>Note that this class is suggested to be part of the next
77 Technical Report, which will extend the C++ Standard (see
78 <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>
80 <para>Class
<code><classname>array
</classname></code> fulfills most
81 but not all of the requirements of
"reversible containers" (see
82 Section
23.1, [lib.container.requirements] of the C++
83 Standard). The reasons array is not an reversible STL container is
85 <itemizedlist spacing=
"compact">
86 <listitem><simpara>No constructors are provided.
</simpara></listitem>
87 <listitem><simpara>Elements may have an undetermined initial value (see
<xref linkend=
"array.rationale"/>).
</simpara></listitem>
88 <listitem><simpara><functionname>swap
</functionname>() has no constant complexity.
</simpara></listitem>
89 <listitem><simpara><methodname>size
</methodname>() is always constant, based on the second template argument of the type.
</simpara></listitem>
90 <listitem><simpara>The container provides no allocator support.
</simpara></listitem>
94 <para>It doesn't fulfill the requirements of a
"sequence" (see Section
23.1.1, [lib.sequence.reqmts] of the C++ Standard), except that:
95 <itemizedlist spacing=
"compact">
96 <listitem><simpara><methodname>front
</methodname>() and
<methodname>back
</methodname>() are provided.
</simpara></listitem>
97 <listitem><simpara><methodname>operator[]
</methodname> and
<methodname>at
</methodname>() are provided.
</simpara></listitem>
103 <header name=
"boost/array.hpp">
104 <namespace name=
"boost">
107 <template-type-parameter name=
"T"/>
108 <template-nontype-parameter name=
"N">
109 <type>std::size_t
</type>
110 </template-nontype-parameter>
113 <purpose><para>STL compliant container wrapper for arrays of constant size
</para></purpose>
114 <typedef name=
"value_type">
117 <typedef name=
"iterator">
120 <typedef name=
"const_iterator">
121 <type>const T*
</type>
123 <typedef name=
"reverse_iterator">
124 <type><classname>std::reverse_iterator
</classname><iterator
></type>
126 <typedef name=
"const_reverse_iterator">
127 <type><classname>std::reverse_iterator
</classname><const_iterator
></type>
129 <typedef name=
"reference">
132 <typedef name=
"const_reference">
133 <type>const T
&</type>
135 <typedef name=
"size_type">
136 <type>std::size_t
</type>
138 <typedef name=
"difference_type">
139 <type>std::ptrdiff_t
</type>
142 <static-constant name=
"static_size">
143 <type>size_type
</type>
149 <template-type-parameter name=
"U"/>
151 <parameter name=
"other">
152 <paramtype>const
<classname>array
</classname><U, N
>&</paramtype>
154 <effects><simpara><code>std::copy(rhs.
<methodname>begin
</methodname>(),rhs.
<methodname>end
</methodname>(),
<methodname>begin
</methodname>())
</code></simpara></effects>
157 <method-group name=
"iterator support">
158 <overloaded-method name=
"begin">
160 <type>iterator
</type>
162 <signature cv=
"const">
163 <type>const_iterator
</type>
166 <returns><simpara>iterator for the first element
</simpara></returns>
167 <throws><simpara>will not throw
</simpara></throws>
170 <overloaded-method name=
"end">
172 <type>iterator
</type>
174 <signature cv=
"const">
175 <type>const_iterator
</type>
178 <returns><simpara>iterator for position after the last element
</simpara></returns>
179 <throws><simpara>will not throw
</simpara></throws>
183 <method-group name=
"reverse iterator support">
184 <overloaded-method name=
"rbegin">
186 <type>reverse_iterator
</type>
188 <signature cv=
"const">
189 <type>const_reverse_iterator
</type>
192 <returns><simpara>reverse iterator for the first element of reverse iteration
</simpara></returns>
195 <overloaded-method name=
"rend">
197 <type>reverse_iterator
</type>
199 <signature cv=
"const">
200 <type>const_reverse_iterator
</type>
203 <returns><simpara>reverse iterator for position after the last element in reverse iteration
</simpara></returns>
207 <method-group name=
"capacity">
209 <type>size_type
</type>
210 <returns><simpara><code>N
</code></simpara></returns>
212 <method name=
"empty">
214 <returns><simpara><code>N==
0</code></simpara></returns>
215 <throws><simpara>will not throw
</simpara></throws>
217 <method name=
"max_size">
218 <type>size_type
</type>
219 <returns><simpara><code>N
</code></simpara></returns>
220 <throws><simpara>will not throw
</simpara></throws>
224 <method-group name=
"element access">
225 <overloaded-method name=
"operator[]">
227 <type>reference
</type>
229 <paramtype>size_type
</paramtype>
233 <signature cv=
"const">
234 <type>const_reference
</type>
236 <paramtype>size_type
</paramtype>
240 <requires><simpara><code>i
< N
</code></simpara></requires>
241 <returns><simpara>element with index
<code>i
</code></simpara></returns>
242 <throws><simpara>will not throw.
</simpara></throws>
245 <overloaded-method name=
"at">
247 <type>reference
</type>
249 <paramtype>size_type
</paramtype>
253 <signature cv=
"const">
254 <type>const_reference
</type>
256 <paramtype>size_type
</paramtype>
260 <returns><simpara>element with index
<code>i
</code></simpara></returns>
261 <throws><simpara><code><classname>std::range_error
</classname></code> if
<code>i
>= N
</code></simpara></throws>
264 <overloaded-method name=
"front">
266 <type>reference
</type>
268 <signature cv=
"const">
269 <type>const_reference
</type>
271 <requires><simpara><code>N
> 0</code></simpara></requires>
272 <returns><simpara>the first element
</simpara></returns>
273 <throws><simpara>will not throw
</simpara></throws>
276 <overloaded-method name=
"back">
278 <type>reference
</type>
280 <signature cv=
"const">
281 <type>const_reference
</type>
283 <requires><simpara><code>N
> 0</code></simpara></requires>
284 <returns><simpara>the last element
</simpara></returns>
285 <throws><simpara>will not throw
</simpara></throws>
288 <method name=
"data" cv=
"const">
289 <type>const T*
</type>
290 <returns><simpara><code>elems
</code></simpara></returns>
291 <throws><simpara>will not throw
</simpara></throws>
294 <method name=
"c_array">
296 <returns><simpara><code>elems
</code></simpara></returns>
297 <throws><simpara>will not throw
</simpara></throws>
301 <method-group name=
"modifiers">
304 <parameter name=
"other">
305 <paramtype><classname>array
</classname><T, N
>&</paramtype>
307 <effects><simpara><code>std::swap_ranges(
<methodname>begin
</methodname>(),
<methodname>end
</methodname>(), other.
<methodname>begin
</methodname>())
</code></simpara></effects>
308 <complexity><simpara>linear in
<code>N
</code></simpara></complexity>
310 <method name=
"assign">
312 <parameter name=
"value">
313 <paramtype>const T
&</paramtype>
315 <effects><simpara><code>std::fill_n(
<methodname>begin
</methodname>(), N, value)
</code></simpara></effects>
319 <data-member name=
"elems[N]"> <!-- HACK -->
323 <free-function-group name=
"specialized algorithms">
324 <function name=
"swap">
326 <template-type-parameter name=
"T"/>
327 <template-nontype-parameter name=
"N">
328 <type>std::size_t
</type>
329 </template-nontype-parameter>
335 <paramtype><classname>array
</classname><T, N
>&</paramtype>
338 <paramtype><classname>array
</classname><T, N
>&</paramtype>
341 <effects><simpara><code>x.
<methodname>swap
</methodname>(y)
</code></simpara></effects>
342 <throws><simpara>will not throw.
</simpara></throws>
344 </free-function-group>
346 <free-function-group name=
"comparisons">
347 <function name=
"operator==">
349 <template-type-parameter name=
"T"/>
350 <template-nontype-parameter name=
"N">
351 <type>std::size_t
</type>
352 </template-nontype-parameter>
358 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
361 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
364 <returns><simpara><code>std::equal(x.
<methodname>begin
</methodname>(), x.
<methodname>end
</methodname>(), y.
<methodname>begin
</methodname>())
</code></simpara>
368 <function name=
"operator!=">
370 <template-type-parameter name=
"T"/>
371 <template-nontype-parameter name=
"N">
372 <type>std::size_t
</type>
373 </template-nontype-parameter>
379 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
382 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
385 <returns><simpara><code>!(x == y)
</code></simpara>
389 <function name=
"operator<">
391 <template-type-parameter name=
"T"/>
392 <template-nontype-parameter name=
"N">
393 <type>std::size_t
</type>
394 </template-nontype-parameter>
400 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
403 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
406 <returns><simpara><code>std::lexicographical_compare(x.
<methodname>begin
</methodname>(), x.
<methodname>end
</methodname>(), y.
<methodname>begin
</methodname>(), y.
<methodname>end
</methodname>())
</code></simpara>
410 <function name=
"operator>">
412 <template-type-parameter name=
"T"/>
413 <template-nontype-parameter name=
"N">
414 <type>std::size_t
</type>
415 </template-nontype-parameter>
421 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
424 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
427 <returns><simpara><code>y
< x
</code></simpara></returns>
430 <function name=
"operator<=">
432 <template-type-parameter name=
"T"/>
433 <template-nontype-parameter name=
"N">
434 <type>std::size_t
</type>
435 </template-nontype-parameter>
441 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
444 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
447 <returns><simpara><code>!(y
< x)
</code></simpara></returns>
450 <function name=
"operator>=">
452 <template-type-parameter name=
"T"/>
453 <template-nontype-parameter name=
"N">
454 <type>std::size_t
</type>
455 </template-nontype-parameter>
461 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
464 <paramtype>const
<classname>array
</classname><T, N
>&</paramtype>
467 <returns><simpara><code>!(x
< y)
</code></simpara></returns>
469 </free-function-group>
475 <section id=
"array.rationale">
476 <title>Design Rationale
</title>
478 <para>There was an important design tradeoff regarding the
479 constructors: We could implement array as an
"aggregate" (see
480 Section
8.5.1, [dcl.init.aggr], of the C++ Standard). This would
483 <listitem><simpara>An array can be initialized with a
484 brace-enclosing, comma-separated list of initializers for the
485 elements of the container, written in increasing subscript
488 <programlisting><classname>boost::array
</classname><int,
4> a = { {
1,
2,
3 } };
</programlisting>
490 <simpara>Note that if there are fewer elements in the
491 initializer list, then each remaining element gets
492 default-initialized (thus, it has a defined value).
</simpara>
493 </listitem></itemizedlist></para>
495 <para>However, this approach has its drawbacks:
<emphasis
496 role=
"bold"> passing no initializer list means that the elements
497 have an indetermined initial value
</emphasis>, because the rule says
498 that aggregates may have:
500 <listitem><simpara>No user-declared constructors.
</simpara></listitem>
501 <listitem><simpara>No private or protected non-static data members.
</simpara></listitem>
502 <listitem><simpara>No base classes.
</simpara></listitem>
503 <listitem><simpara>No virtual functions.
</simpara></listitem>
507 <para>Nevertheless, The current implementation uses this approach.
</para>
509 <para>Note that for standard conforming compilers it is possible to
510 use fewer braces (according to
8.5.1 (
11) of the Standard). That is,
511 you can initialize an array as follows:
</para>
514 <classname>boost::array
</classname><int,
4> a = {
1,
2,
3 };
517 <para>I'd appreciate any constructive feedback.
<emphasis
518 role=
"bold">Please note: I don't have time to read all boost
519 mails. Thus, to make sure that feedback arrives to me, please send
520 me a copy of each mail regarding this class.
</emphasis></para>
522 <para>The code is provided
"as is" without expressed or implied
527 <section id=
"array.more.info">
528 <title>For more information...
</title>
529 <para>To find more details about using ordinary arrays in C++ and
530 the framework of the STL, see e.g.
532 <literallayout>The C++ Standard Library - A Tutorial and Reference
533 by Nicolai M. Josuttis
534 Addison Wesley Longman,
1999
535 ISBN
0-
201-
37926-
0</literallayout>
538 <para><ulink url=
"http://www.josuttis.com/">Home Page of Nicolai
539 Josuttis
</ulink></para>
542 <section id=
"array.ack">
543 <title>Acknowledgements
</title>
545 <para>Doug Gregor ported the documentation to the BoostBook format.
</para>
549 empty() should return N != 0
550 size(), empty(), max_size() should be const