summaryrefslogtreecommitdiff
path: root/libs/array/doc/array.xml
blob: 8a151415796894deee6ffb75598ecc2087f3a8c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN"
  "http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
<library name="Array" dirname="array" id="array" last-revision="$Date: 2012-06-14 09:01:03 -0700 (Thu, 14 Jun 2012) $">
  <libraryinfo>
    <author>
      <firstname>Nicolai</firstname>
      <surname>Josuttis</surname>
    </author>

    <copyright>
      <year>2001</year>
      <year>2002</year>
      <year>2003</year>
      <year>2004</year>
      <holder>Nicolai M. Josuttis</holder>
    </copyright>
   
    <legalnotice>
      <para>Distributed under the Boost Software License, Version 1.0.
      (See accompanying file <filename>LICENSE_1_0.txt</filename> or copy at 
      <ulink
      url="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</ulink>)
      </para>
    </legalnotice>

    <librarypurpose>STL compliant container wrapper for arrays of constant size</librarypurpose>
    <librarycategory name="category:containers"/>
  </libraryinfo>

  <title>Boost.Array</title>
 
  <section id="array.intro">
    <title>Introduction</title> 

    <using-namespace name="boost"/>
    <using-class name="array"/>

    <para>The C++ Standard Template Library STL as part of the C++
    Standard Library provides a framework for processing algorithms on
    different kind of containers. However, ordinary arrays don't
    provide the interface of STL containers (although, they provide
    the iterator interface of STL containers).</para>

    <para>As replacement for ordinary arrays, the STL provides class
    <code><classname>std::vector</classname></code>.  However,
    <code><classname>std::vector&lt;&gt;</classname></code> provides
    the semantics of dynamic arrays. Thus, it manages data to be able
    to change the number of elements. This results in some overhead in
    case only arrays with static size are needed.</para>

    <para>In his book, <emphasis>Generic Programming and the
    STL</emphasis>, Matthew H. Austern introduces a useful wrapper
    class for ordinary arrays with static size, called
    <code>block</code>.  It is safer and has no worse performance than
    ordinary arrays. In <emphasis>The C++ Programming
    Language</emphasis>, 3rd edition, Bjarne Stroustrup introduces a
    similar class, called <code>c_array</code>, which I (<ulink
    url="http://www.josuttis.com">Nicolai Josuttis</ulink>) present
    slightly modified in my book <emphasis>The C++ Standard Library -
    A Tutorial and Reference</emphasis>, called
    <code>carray</code>. This is the essence of these approaches
    spiced with many feedback from <ulink
    url="http://www.boost.org">boost</ulink>.</para>

    <para>After considering different names, we decided to name this
    class simply <code><classname>array</classname></code>.</para>

    <para>Note that this class is suggested to be part of the next
    Technical Report, which will extend the C++ Standard (see
    <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>Update: <code>std::array</code> is (as of C++11) part of the C++ standard.
    The differences between <code>boost::array</code> and <code>std::array</code> are minimal.
    If you are using C++11, you should consider using <code>std::array</code> instead of <code>boost::array</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++
    Standard). The reasons array is not an reversible STL container is
    because:
      <itemizedlist spacing="compact">
        <listitem><simpara>No constructors are provided.</simpara></listitem>
        <listitem><simpara>Elements may have an undetermined initial value (see <xref linkend="array.rationale"/>).</simpara></listitem>
        <listitem><simpara><functionname>swap</functionname>() has no constant complexity.</simpara></listitem>
        <listitem><simpara><methodname>size</methodname>() is always constant, based on the second template argument of the type.</simpara></listitem>
        <listitem><simpara>The container provides no allocator support.</simpara></listitem>
      </itemizedlist>
    </para>

    <para>It doesn't fulfill the requirements of a "sequence" (see Section 23.1.1, [lib.sequence.reqmts] of the C++ Standard), except that:
      <itemizedlist spacing="compact">
        <listitem><simpara><methodname>front</methodname>() and <methodname>back</methodname>() are provided.</simpara></listitem>
        <listitem><simpara><methodname>operator[]</methodname> and <methodname>at</methodname>() are provided.</simpara></listitem>
      </itemizedlist>
    </para>
  </section>
  
  <library-reference>
    <header name="boost/array.hpp">
      <namespace name="boost">
        <class name="array">
          <template>
            <template-type-parameter name="T"/>
            <template-nontype-parameter name="N">
              <type>std::size_t</type>
            </template-nontype-parameter>
          </template>

          <purpose><para>STL compliant container wrapper for arrays of constant size</para></purpose>
          <typedef name="value_type">
            <type>T</type>
          </typedef>
          <typedef name="iterator">
             <type>T*</type>
          </typedef>
          <typedef name="const_iterator">
             <type>const T*</type>
          </typedef>
          <typedef name="reverse_iterator">
             <type><classname>std::reverse_iterator</classname>&lt;iterator&gt;</type>
          </typedef>
          <typedef name="const_reverse_iterator">
             <type><classname>std::reverse_iterator</classname>&lt;const_iterator&gt;</type>
          </typedef>
          <typedef name="reference">
             <type>T&amp;</type>
          </typedef>
          <typedef name="const_reference">
             <type>const T&amp;</type>
          </typedef>
          <typedef name="size_type">
             <type>std::size_t</type>
          </typedef>
          <typedef name="difference_type">
             <type>std::ptrdiff_t</type>
          </typedef>

          <static-constant name="static_size">
            <type>size_type</type>
            <default>N</default>
          </static-constant>

          <copy-assignment>
            <template>
              <template-type-parameter name="U"/>
            </template>
            <parameter name="other">
              <paramtype>const <classname>array</classname>&lt;U, N&gt;&amp;</paramtype>
            </parameter>
            <effects><simpara><code>std::copy(rhs.<methodname>begin</methodname>(),rhs.<methodname>end</methodname>(), <methodname>begin</methodname>())</code></simpara></effects>
          </copy-assignment>

          <method-group name="iterator support">
            <overloaded-method name="begin">
              <signature>
                <type>iterator</type>
              </signature>
              <signature cv="const">
                <type>const_iterator</type>
              </signature>

              <returns><simpara>iterator for the first element</simpara></returns>
              <throws><simpara>will not throw</simpara></throws>
            </overloaded-method>

            <overloaded-method name="end">
              <signature>
                <type>iterator</type>
              </signature>
              <signature cv="const">
                <type>const_iterator</type>
              </signature>

              <returns><simpara>iterator for position after the last element</simpara></returns>
              <throws><simpara>will not throw</simpara></throws>
            </overloaded-method>
          </method-group>

          <method-group name="reverse iterator support">
            <overloaded-method name="rbegin">
              <signature>
                <type>reverse_iterator</type>
              </signature>
              <signature cv="const">
                <type>const_reverse_iterator</type>
              </signature>

              <returns><simpara>reverse iterator for the first element of reverse iteration</simpara></returns>
            </overloaded-method>

            <overloaded-method name="rend">
              <signature>
                <type>reverse_iterator</type>
              </signature>
              <signature cv="const">
                <type>const_reverse_iterator</type>
              </signature>

              <returns><simpara>reverse iterator for position after the last element in reverse iteration</simpara></returns>
            </overloaded-method>
          </method-group>

          <method-group name="capacity">
            <method name="size">
              <type>size_type</type>
              <returns><simpara><code>N</code></simpara></returns>
            </method>
            <method name="empty">
              <type>bool</type>
              <returns><simpara><code>N==0</code></simpara></returns>
              <throws><simpara>will not throw</simpara></throws>
            </method>
            <method name="max_size">
              <type>size_type</type>
              <returns><simpara><code>N</code></simpara></returns>
              <throws><simpara>will not throw</simpara></throws>
            </method>
          </method-group>

          <method-group name="element access">
            <overloaded-method name="operator[]">
              <signature>
                <type>reference</type>
                <parameter name="i">
                  <paramtype>size_type</paramtype>
                </parameter>
              </signature>

              <signature cv="const">
                <type>const_reference</type>
                <parameter name="i">
                  <paramtype>size_type</paramtype>
                </parameter>
              </signature>

              <requires><simpara><code>i &lt; N</code></simpara></requires>
              <returns><simpara>element with index <code>i</code></simpara></returns>
              <throws><simpara>will not throw.</simpara></throws>
            </overloaded-method>

            <overloaded-method name="at">
              <signature>
                <type>reference</type>
                <parameter name="i">
                  <paramtype>size_type</paramtype>
                </parameter>
              </signature>

              <signature cv="const">
                <type>const_reference</type>
                <parameter name="i">
                  <paramtype>size_type</paramtype>
                </parameter>
              </signature>

              <returns><simpara>element with index <code>i</code></simpara></returns>
              <throws><simpara><code><classname>std::range_error</classname></code> if <code>i &gt;= N</code></simpara></throws>
            </overloaded-method>

            <overloaded-method name="front">
              <signature>
                <type>reference</type>
              </signature>
              <signature cv="const">
                <type>const_reference</type>
              </signature>
              <requires><simpara><code>N &gt; 0</code></simpara></requires>
              <returns><simpara>the first element</simpara></returns>
              <throws><simpara>will not throw</simpara></throws>
            </overloaded-method>

            <overloaded-method name="back">
              <signature>
                <type>reference</type>
              </signature>
              <signature cv="const">
                <type>const_reference</type>
              </signature>
              <requires><simpara><code>N &gt; 0</code></simpara></requires>
              <returns><simpara>the last element</simpara></returns>
              <throws><simpara>will not throw</simpara></throws>
            </overloaded-method>

            <method name="data" cv="const">
              <type>const T*</type>
              <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">
            <method name="swap">
              <type>void</type>
              <parameter name="other">
                <paramtype><classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>
              <effects><simpara><code>std::swap_ranges(<methodname>begin</methodname>(), <methodname>end</methodname>(), other.<methodname>begin</methodname>())</code></simpara></effects>
              <complexity><simpara>linear in <code>N</code></simpara></complexity>
            </method>
            <method name="assign">
              <type>void</type>
              <parameter name="value">
                <paramtype>const T&amp;</paramtype>
              </parameter>
              <effects><simpara><code>std::fill_n(<methodname>begin</methodname>(), N, value)</code></simpara></effects>
            </method>
          </method-group>

          <data-member name="elems[N]"> <!-- HACK -->
            <type>T</type>
          </data-member>

          <free-function-group name="specialized algorithms">
            <function name="swap"> 
              <template>
                <template-type-parameter name="T"/>
                <template-nontype-parameter name="N">
                  <type>std::size_t</type>
                </template-nontype-parameter>
              </template>

              <type>void</type>
              
              <parameter name="x">
                <paramtype><classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>
              <parameter name="y">
                <paramtype><classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>

              <effects><simpara><code>x.<methodname>swap</methodname>(y)</code></simpara></effects>
              <throws><simpara>will not throw.</simpara></throws>
            </function>
          </free-function-group>

          <free-function-group name="comparisons">
            <function name="operator==">
              <template>
                <template-type-parameter name="T"/>
                <template-nontype-parameter name="N">
                  <type>std::size_t</type>
                </template-nontype-parameter>
              </template>

              <type>bool</type>
              
              <parameter name="x">
                <paramtype>const <classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>
              <parameter name="y">
                <paramtype>const <classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>

              <returns><simpara><code>std::equal(x.<methodname>begin</methodname>(), x.<methodname>end</methodname>(), y.<methodname>begin</methodname>())</code></simpara>
              </returns>
            </function>
 
            <function name="operator!=">
              <template>
                <template-type-parameter name="T"/>
                <template-nontype-parameter name="N">
                  <type>std::size_t</type>
                </template-nontype-parameter>
              </template>

              <type>bool</type>
              
              <parameter name="x">
                <paramtype>const <classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>
              <parameter name="y">
                <paramtype>const <classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>

              <returns><simpara><code>!(x == y)</code></simpara>
              </returns>
            </function>

            <function name="operator&lt;">
              <template>
                <template-type-parameter name="T"/>
                <template-nontype-parameter name="N">
                  <type>std::size_t</type>
                </template-nontype-parameter>
              </template>

              <type>bool</type>
              
              <parameter name="x">
                <paramtype>const <classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>
              <parameter name="y">
                <paramtype>const <classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>

              <returns><simpara><code>std::lexicographical_compare(x.<methodname>begin</methodname>(), x.<methodname>end</methodname>(), y.<methodname>begin</methodname>(), y.<methodname>end</methodname>())</code></simpara>
              </returns>
            </function>

            <function name="operator&gt;">
              <template>
                <template-type-parameter name="T"/>
                <template-nontype-parameter name="N">
                  <type>std::size_t</type>
                </template-nontype-parameter>
              </template>

              <type>bool</type>
              
              <parameter name="x">
                <paramtype>const <classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>
              <parameter name="y">
                <paramtype>const <classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>

              <returns><simpara><code>y &lt; x</code></simpara></returns>
            </function>

            <function name="operator&lt;=">
              <template>
                <template-type-parameter name="T"/>
                <template-nontype-parameter name="N">
                  <type>std::size_t</type>
                </template-nontype-parameter>
              </template>

              <type>bool</type>
              
              <parameter name="x">
                <paramtype>const <classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>
              <parameter name="y">
                <paramtype>const <classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>

              <returns><simpara><code>!(y &lt; x)</code></simpara></returns>
            </function>

            <function name="operator&gt;=">
              <template>
                <template-type-parameter name="T"/>
                <template-nontype-parameter name="N">
                  <type>std::size_t</type>
                </template-nontype-parameter>
              </template>

              <type>bool</type>
              
              <parameter name="x">
                <paramtype>const <classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>
              <parameter name="y">
                <paramtype>const <classname>array</classname>&lt;T, N&gt;&amp;</paramtype>
              </parameter>

              <returns><simpara><code>!(x &lt; y)</code></simpara></returns>
            </function>
          </free-function-group>
        </class>
      </namespace>
    </header>
  </library-reference>

<section id="array.rationale">
  <title>Design Rationale</title>

  <para>There was an important design tradeoff regarding the
  constructors: We could implement array as an "aggregate" (see
  Section 8.5.1, [dcl.init.aggr], of the C++ Standard). This would
  mean:
    <itemizedlist>
      <listitem><simpara>An array can be initialized with a
      brace-enclosing, comma-separated list of initializers for the
      elements of the container, written in increasing subscript
      order:</simpara>

      <programlisting><classname>boost::array</classname>&lt;int,4&gt; a = { { 1, 2, 3 } };</programlisting>

      <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).</simpara>
  </listitem></itemizedlist></para>

  <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>Nevertheless, The current implementation uses this approach.</para>

  <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>

<programlisting>
<classname>boost::array</classname>&lt;int,4&gt; a = { 1, 2, 3 };
</programlisting>

  <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>

  <para>The code is provided "as is" without expressed or implied
  warranty.</para>

</section>

<section id="array.more.info">
  <title>For more information...</title>
  <para>To find more details about using ordinary arrays in C++ and
  the framework of the STL, see e.g.

    <literallayout>The C++ Standard Library - A Tutorial and Reference
by Nicolai M. Josuttis
Addison Wesley Longman, 1999
ISBN 0-201-37926-0</literallayout>
   </para>

  <para><ulink url="http://www.josuttis.com/">Home Page of Nicolai
  Josuttis</ulink></para>
</section>

<section id="array.ack">
  <title>Acknowledgements</title>
  
  <para>Doug Gregor ported the documentation to the BoostBook format.</para>
</section>

<!-- Notes:
   empty() should return N != 0
   size(), empty(), max_size() should be const
   -->

</library>