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