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$"> 5 <libraryinfo> 6 <author> 7 <firstname>Nicolai</firstname> 8 <surname>Josuttis</surname> 9 </author> 10 11 <copyright> 12 <year>2001</year> 13 <year>2002</year> 14 <year>2003</year> 15 <year>2004</year> 16 <holder>Nicolai M. Josuttis</holder> 17 </copyright> 18 19 <legalnotice> 20 <para>Distributed under the Boost Software License, Version 1.0. 21 (See accompanying file <filename>LICENSE_1_0.txt</filename> or copy at 22 <ulink 23 url="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</ulink>) 24 </para> 25 </legalnotice> 26 27 <librarypurpose>STL compliant container wrapper for arrays of constant size</librarypurpose> 28 <librarycategory name="category:containers"/> 29 </libraryinfo> 30 31 <title>Boost.Array</title> 32 33 <section id="array.intro"> 34 <title>Introduction</title> 35 36 <using-namespace name="boost"/> 37 <using-class name="array"/> 38 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> 44 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> 51 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> 65 66 <para>After considering different names, we decided to name this 67 class simply <code><classname>array</classname></code>.</para> 68 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> 72 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>. 76 </para> 77 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 82 because: 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> 89 </itemizedlist> 90 </para> 91 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> 96 </itemizedlist> 97 </para> 98 </section> 99 100 <library-reference> 101 <header name="boost/array.hpp"> 102 <namespace name="boost"> 103 <class name="array"> 104 <template> 105 <template-type-parameter name="T"/> 106 <template-nontype-parameter name="N"> 107 <type>std::size_t</type> 108 </template-nontype-parameter> 109 </template> 110 111 <purpose><para>STL compliant container wrapper for arrays of constant size</para></purpose> 112 <typedef name="value_type"> 113 <type>T</type> 114 </typedef> 115 <typedef name="iterator"> 116 <type>T*</type> 117 </typedef> 118 <typedef name="const_iterator"> 119 <type>const T*</type> 120 </typedef> 121 <typedef name="reverse_iterator"> 122 <type><classname>std::reverse_iterator</classname><iterator></type> 123 </typedef> 124 <typedef name="const_reverse_iterator"> 125 <type><classname>std::reverse_iterator</classname><const_iterator></type> 126 </typedef> 127 <typedef name="reference"> 128 <type>T&</type> 129 </typedef> 130 <typedef name="const_reference"> 131 <type>const T&</type> 132 </typedef> 133 <typedef name="size_type"> 134 <type>std::size_t</type> 135 </typedef> 136 <typedef name="difference_type"> 137 <type>std::ptrdiff_t</type> 138 </typedef> 139 140 <static-constant name="static_size"> 141 <type>size_type</type> 142 <default>N</default> 143 </static-constant> 144 145 <copy-assignment> 146 <template> 147 <template-type-parameter name="U"/> 148 </template> 149 <parameter name="other"> 150 <paramtype>const <classname>array</classname><U, N>&</paramtype> 151 </parameter> 152 <effects><simpara><code>std::copy(rhs.<methodname>begin</methodname>(),rhs.<methodname>end</methodname>(), <methodname>begin</methodname>())</code></simpara></effects> 153 </copy-assignment> 154 155 <method-group name="iterator support"> 156 <overloaded-method name="begin"> 157 <signature> 158 <type>iterator</type> 159 </signature> 160 <signature cv="const"> 161 <type>const_iterator</type> 162 </signature> 163 164 <returns><simpara>iterator for the first element</simpara></returns> 165 <throws><simpara>will not throw</simpara></throws> 166 </overloaded-method> 167 168 <overloaded-method name="end"> 169 <signature> 170 <type>iterator</type> 171 </signature> 172 <signature cv="const"> 173 <type>const_iterator</type> 174 </signature> 175 176 <returns><simpara>iterator for position after the last element</simpara></returns> 177 <throws><simpara>will not throw</simpara></throws> 178 </overloaded-method> 179 </method-group> 180 181 <method-group name="reverse iterator support"> 182 <overloaded-method name="rbegin"> 183 <signature> 184 <type>reverse_iterator</type> 185 </signature> 186 <signature cv="const"> 187 <type>const_reverse_iterator</type> 188 </signature> 189 190 <returns><simpara>reverse iterator for the first element of reverse iteration</simpara></returns> 191 </overloaded-method> 192 193 <overloaded-method name="rend"> 194 <signature> 195 <type>reverse_iterator</type> 196 </signature> 197 <signature cv="const"> 198 <type>const_reverse_iterator</type> 199 </signature> 200 201 <returns><simpara>reverse iterator for position after the last element in reverse iteration</simpara></returns> 202 </overloaded-method> 203 </method-group> 204 205 <method-group name="capacity"> 206 <method name="size"> 207 <type>size_type</type> 208 <returns><simpara><code>N</code></simpara></returns> 209 </method> 210 <method name="empty"> 211 <type>bool</type> 212 <returns><simpara><code>N==0</code></simpara></returns> 213 <throws><simpara>will not throw</simpara></throws> 214 </method> 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> 219 </method> 220 </method-group> 221 222 <method-group name="element access"> 223 <overloaded-method name="operator[]"> 224 <signature> 225 <type>reference</type> 226 <parameter name="i"> 227 <paramtype>size_type</paramtype> 228 </parameter> 229 </signature> 230 231 <signature cv="const"> 232 <type>const_reference</type> 233 <parameter name="i"> 234 <paramtype>size_type</paramtype> 235 </parameter> 236 </signature> 237 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> 241 </overloaded-method> 242 243 <overloaded-method name="at"> 244 <signature> 245 <type>reference</type> 246 <parameter name="i"> 247 <paramtype>size_type</paramtype> 248 </parameter> 249 </signature> 250 251 <signature cv="const"> 252 <type>const_reference</type> 253 <parameter name="i"> 254 <paramtype>size_type</paramtype> 255 </parameter> 256 </signature> 257 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> 260 </overloaded-method> 261 262 <overloaded-method name="front"> 263 <signature> 264 <type>reference</type> 265 </signature> 266 <signature cv="const"> 267 <type>const_reference</type> 268 </signature> 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> 272 </overloaded-method> 273 274 <overloaded-method name="back"> 275 <signature> 276 <type>reference</type> 277 </signature> 278 <signature cv="const"> 279 <type>const_reference</type> 280 </signature> 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> 284 </overloaded-method> 285 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> 290 </method> 291 292 <method name="c_array"> 293 <type>T*</type> 294 <returns><simpara><code>elems</code></simpara></returns> 295 <throws><simpara>will not throw</simpara></throws> 296 </method> 297 </method-group> 298 299 <method-group name="modifiers"> 300 <method name="swap"> 301 <type>void</type> 302 <parameter name="other"> 303 <paramtype><classname>array</classname><T, N>&</paramtype> 304 </parameter> 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> 307 </method> 308 <method name="assign"> 309 <type>void</type> 310 <parameter name="value"> 311 <paramtype>const T&</paramtype> 312 </parameter> 313 <effects><simpara><code>std::fill_n(<methodname>begin</methodname>(), N, value)</code></simpara></effects> 314 </method> 315 </method-group> 316 317 <data-member name="elems[N]"> <!-- HACK --> 318 <type>T</type> 319 </data-member> 320 321 <free-function-group name="specialized algorithms"> 322 <function name="swap"> 323 <template> 324 <template-type-parameter name="T"/> 325 <template-nontype-parameter name="N"> 326 <type>std::size_t</type> 327 </template-nontype-parameter> 328 </template> 329 330 <type>void</type> 331 332 <parameter name="x"> 333 <paramtype><classname>array</classname><T, N>&</paramtype> 334 </parameter> 335 <parameter name="y"> 336 <paramtype><classname>array</classname><T, N>&</paramtype> 337 </parameter> 338 339 <effects><simpara><code>x.<methodname>swap</methodname>(y)</code></simpara></effects> 340 <throws><simpara>will not throw.</simpara></throws> 341 </function> 342 </free-function-group> 343 344 <free-function-group name="comparisons"> 345 <function name="operator=="> 346 <template> 347 <template-type-parameter name="T"/> 348 <template-nontype-parameter name="N"> 349 <type>std::size_t</type> 350 </template-nontype-parameter> 351 </template> 352 353 <type>bool</type> 354 355 <parameter name="x"> 356 <paramtype>const <classname>array</classname><T, N>&</paramtype> 357 </parameter> 358 <parameter name="y"> 359 <paramtype>const <classname>array</classname><T, N>&</paramtype> 360 </parameter> 361 362 <returns><simpara><code>std::equal(x.<methodname>begin</methodname>(), x.<methodname>end</methodname>(), y.<methodname>begin</methodname>())</code></simpara> 363 </returns> 364 </function> 365 366 <function name="operator!="> 367 <template> 368 <template-type-parameter name="T"/> 369 <template-nontype-parameter name="N"> 370 <type>std::size_t</type> 371 </template-nontype-parameter> 372 </template> 373 374 <type>bool</type> 375 376 <parameter name="x"> 377 <paramtype>const <classname>array</classname><T, N>&</paramtype> 378 </parameter> 379 <parameter name="y"> 380 <paramtype>const <classname>array</classname><T, N>&</paramtype> 381 </parameter> 382 383 <returns><simpara><code>!(x == y)</code></simpara> 384 </returns> 385 </function> 386 387 <function name="operator<"> 388 <template> 389 <template-type-parameter name="T"/> 390 <template-nontype-parameter name="N"> 391 <type>std::size_t</type> 392 </template-nontype-parameter> 393 </template> 394 395 <type>bool</type> 396 397 <parameter name="x"> 398 <paramtype>const <classname>array</classname><T, N>&</paramtype> 399 </parameter> 400 <parameter name="y"> 401 <paramtype>const <classname>array</classname><T, N>&</paramtype> 402 </parameter> 403 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> 405 </returns> 406 </function> 407 408 <function name="operator>"> 409 <template> 410 <template-type-parameter name="T"/> 411 <template-nontype-parameter name="N"> 412 <type>std::size_t</type> 413 </template-nontype-parameter> 414 </template> 415 416 <type>bool</type> 417 418 <parameter name="x"> 419 <paramtype>const <classname>array</classname><T, N>&</paramtype> 420 </parameter> 421 <parameter name="y"> 422 <paramtype>const <classname>array</classname><T, N>&</paramtype> 423 </parameter> 424 425 <returns><simpara><code>y < x</code></simpara></returns> 426 </function> 427 428 <function name="operator<="> 429 <template> 430 <template-type-parameter name="T"/> 431 <template-nontype-parameter name="N"> 432 <type>std::size_t</type> 433 </template-nontype-parameter> 434 </template> 435 436 <type>bool</type> 437 438 <parameter name="x"> 439 <paramtype>const <classname>array</classname><T, N>&</paramtype> 440 </parameter> 441 <parameter name="y"> 442 <paramtype>const <classname>array</classname><T, N>&</paramtype> 443 </parameter> 444 445 <returns><simpara><code>!(y < x)</code></simpara></returns> 446 </function> 447 448 <function name="operator>="> 449 <template> 450 <template-type-parameter name="T"/> 451 <template-nontype-parameter name="N"> 452 <type>std::size_t</type> 453 </template-nontype-parameter> 454 </template> 455 456 <type>bool</type> 457 458 <parameter name="x"> 459 <paramtype>const <classname>array</classname><T, N>&</paramtype> 460 </parameter> 461 <parameter name="y"> 462 <paramtype>const <classname>array</classname><T, N>&</paramtype> 463 </parameter> 464 465 <returns><simpara><code>!(x < y)</code></simpara></returns> 466 </function> 467 </free-function-group> 468 </class> 469 </namespace> 470 </header> 471 </library-reference> 472 473<section id="array.rationale"> 474 <title>Design Rationale</title> 475 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 479 mean: 480 <itemizedlist> 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 484 order:</simpara> 485 486 <programlisting><classname>boost::array</classname><int,4> a = { { 1, 2, 3 } };</programlisting> 487 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> 492 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: 497 <itemizedlist> 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> 502 </itemizedlist> 503 </para> 504 505 <para>Nevertheless, The current implementation uses this approach.</para> 506 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> 510 511<programlisting> 512<classname>boost::array</classname><int,4> a = { 1, 2, 3 }; 513</programlisting> 514 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> 519 520 <para>The code is provided "as is" without expressed or implied 521 warranty.</para> 522 523</section> 524 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. 529 530 <literallayout>The C++ Standard Library - A Tutorial and Reference 531by Nicolai M. Josuttis 532Addison Wesley Longman, 1999 533ISBN 0-201-37926-0</literallayout> 534 </para> 535 536 <para><ulink url="http://www.josuttis.com/">Home Page of Nicolai 537 Josuttis</ulink></para> 538</section> 539 540<section id="array.ack"> 541 <title>Acknowledgements</title> 542 543 <para>Doug Gregor ported the documentation to the BoostBook format.</para> 544</section> 545 546<!-- Notes: 547 empty() should return N != 0 548 size(), empty(), max_size() should be const 549 --> 550 551</library> 552