<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> <title>Array Example Boostbook XML Documentation</title> <link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css"> <meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> <link rel="home" href="../index.html" title="Document To Test Formatting"> <link rel="up" href="../index.html" title="Document To Test Formatting"> <link rel="prev" href="remez.html" title="Sample Article (The Remez Method)"> <link rel="next" href="../boost/array.html" title="Class template array"> </head> <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> <table cellpadding="2" width="100%"><tr> <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td> <td align="center"><a href="../../../../index.html">Home</a></td> <td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td> <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> <td align="center"><a href="../../../../more/index.htm">More</a></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="remez.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../boost/array.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> </div> <div class="section"> <div class="titlepage"><div><div><h2 class="title" style="clear: both"> <a name="document_to_test_formatting.array"></a><a class="link" href="array.html" title="Array Example Boostbook XML Documentation"> Array Example Boostbook XML Documentation</a> </h2></div></div></div> <div class="toc"><dl> <dt><span class="section"><a href="array.html#array.intro">Introduction</a></span></dt> <dt><span class="section"><a href="array.html#id772287">Reference</a></span></dt> <dt><span class="section"><a href="array.html#array.rationale">Design Rationale</a></span></dt> <dt><span class="section"><a href="array.html#array.more.info">For more information...</a></span></dt> <dt><span class="section"><a href="array.html#array.ack">Acknowledgements</a></span></dt> </dl></div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="array.intro"></a>Introduction</h3></div></div></div> <p>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).</p> <p>As replacement for ordinary arrays, the STL provides class <code class="computeroutput">std::vector</code>. However, <code class="computeroutput">std::vector<></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.</p> <p>In his book, <span class="emphasis"><em>Generic Programming and the STL</em></span>, Matthew H. Austern introduces a useful wrapper class for ordinary arrays with static size, called <code class="computeroutput">block</code>. It is safer and has no worse performance than ordinary arrays. In <span class="emphasis"><em>The C++ Programming Language</em></span>, 3rd edition, Bjarne Stroustrup introduces a similar class, called <code class="computeroutput">c_array</code>, which I (<a href="http://www.josuttis.com" target="_top">Nicolai Josuttis</a>) present slightly modified in my book <span class="emphasis"><em>The C++ Standard Library - A Tutorial and Reference</em></span>, called <code class="computeroutput">carray</code>. This is the essence of these approaches spiced with many feedback from <a href="http://www.boost.org" target="_top">boost</a>.</p> <p>After considering different names, we decided to name this class simply <code class="computeroutput"><a class="link" href="../boost/array.html" title="Class template array">array</a></code>.</p> <p>Note that this class is suggested to be part of the next Technical Report, which will extend the C++ Standard (see <a href="http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1548.htm" target="_top">http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1548.htm</a>).</p> <p>Class <code class="computeroutput"><a class="link" href="../boost/array.html" title="Class template array">array</a></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: </p> <div class="itemizedlist"><ul class="itemizedlist" type="disc" compact> <li class="listitem">No constructors are provided.</li> <li class="listitem">Elements may have an undetermined initial value (see <a class="xref" href="array.html#array.rationale" title="Design Rationale">the section called “Design Rationale”</a>).</li> <li class="listitem"> <code class="computeroutput"><a class="link" href="../boost/array.html#boost.array.swap">swap</a></code>() has no constant complexity.</li> <li class="listitem"> <code class="computeroutput"><a class="link" href="../boost/array.html#id330848-bb">size</a></code>() is always constant, based on the second template argument of the type.</li> <li class="listitem">The container provides no allocator support.</li> </ul></div> <p> </p> <p>It doesn't fulfill the requirements of a "sequence" (see Section 23.1.1, [lib.sequence.reqmts] of the C++ Standard), except that: </p> <div class="itemizedlist"><ul class="itemizedlist" type="disc" compact> <li class="listitem"> <code class="computeroutput"><a class="link" href="../boost/array.html#id331056-bb">front</a></code>() and <code class="computeroutput"><a class="link" href="../boost/array.html#id331110-bb">back</a></code>() are provided.</li> <li class="listitem"> <code class="computeroutput"><a class="link" href="../boost/array.html#id330919-bb">operator[]</a></code> and <code class="computeroutput"><a class="link" href="../boost/array.html#id330982-bb">at</a></code>() are provided.</li> </ul></div> <p> </p> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="id772287"></a>Reference</h3></div></div></div> <div class="toc"><dl><dt><span class="section"><a href="array.html#header.boost.array_hpp">Header <boost/array.hpp></a></span></dt></dl></div> <div class="section"> <div class="titlepage"><div><div><h4 class="title"> <a name="header.boost.array_hpp"></a>Header <<a href="../../../../boost/array.hpp" target="_top">boost/array.hpp</a>></h4></div></div></div> <pre class="synopsis"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> T<span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> N<span class="special">></span> <span class="keyword">class</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special">;</span> <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> T<span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> N<span class="special">></span> <span class="keyword">void</span> <a class="link" href="../boost/array.html#boost.array.swap"><span class="identifier">swap</span></a><span class="special">(</span><a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">,</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">)</span><span class="special">;</span> <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> T<span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> N<span class="special">></span> <span class="keyword">bool</span> <a class="link" href="../boost/array.html#boost.array.operator=="><span class="keyword">operator</span><span class="special">==</span></a><span class="special">(</span><span class="keyword">const</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">)</span><span class="special">;</span> <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> T<span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> N<span class="special">></span> <span class="keyword">bool</span> <a class="link" href="../boost/array.html#boost.array.operator!="><span class="keyword">operator</span><span class="special">!=</span></a><span class="special">(</span><span class="keyword">const</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">)</span><span class="special">;</span> <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> T<span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> N<span class="special">></span> <span class="keyword">bool</span> <a class="link" href="../boost/array.html#boost.array.operator_id331592"><span class="keyword">operator</span><span class="special"><</span></a><span class="special">(</span><span class="keyword">const</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">)</span><span class="special">;</span> <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> T<span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> N<span class="special">></span> <span class="keyword">bool</span> <a class="link" href="../boost/array.html#boost.array.operator_id331679"><span class="keyword">operator</span><span class="special">></span></a><span class="special">(</span><span class="keyword">const</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">)</span><span class="special">;</span> <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> T<span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> N<span class="special">></span> <span class="keyword">bool</span> <a class="link" href="../boost/array.html#boost.array.operator_=_id331750"><span class="keyword">operator</span><span class="special"><=</span></a><span class="special">(</span><span class="keyword">const</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">)</span><span class="special">;</span> <span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> T<span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> N<span class="special">></span> <span class="keyword">bool</span> <a class="link" href="../boost/array.html#boost.array.operator_=_id332091"><span class="keyword">operator</span><span class="special">>=</span></a><span class="special">(</span><span class="keyword">const</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">,</span> <span class="keyword">const</span> <a class="link" href="../boost/array.html" title="Class template array">array</a><span class="special"><</span><span class="identifier">T</span><span class="special">,</span> <span class="identifier">N</span><span class="special">></span><span class="special">&</span><span class="special">)</span><span class="special">;</span> <span class="special">}</span></pre> </div> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="array.rationale"></a>Design Rationale</h3></div></div></div> <p> 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: </p> <div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"> <p class="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: </p> <pre class="programlisting"> <code class="computeroutput"><a class="link" href="../boost/array.html" title="Class template array">boost::array</a></code><int,4> a = { { 1, 2, 3 } }; </pre> <p class="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). </p> </li></ul></div> <p> </p> <p> However, this approach has its drawbacks: <span class="bold"><strong> passing no initializer list means that the elements have an indetermined initial value </strong></span>, because the rule says that aggregates may have: </p> <div class="itemizedlist"><ul class="itemizedlist" type="disc"> <li class="listitem">No user-declared constructors.</li> <li class="listitem">No private or protected non-static data members.</li> <li class="listitem">No base classes.</li> <li class="listitem">No virtual functions.</li> </ul></div> <p> </p> <p>Nevertheless, The current implementation uses this approach.</p> <p> 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: </p> <pre class="programlisting"> <code class="computeroutput"><a class="link" href="../boost/array.html" title="Class template array">boost::array</a></code><int,4> a = { 1, 2, 3 }; </pre> <p> I'd appreciate any constructive feedback. <span class="bold"><strong> 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. </strong></span> </p> <p> The code is provided "as is" without expressed or implied warranty. </p> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="array.more.info"></a>For more information...</h3></div></div></div> <p> To find more details about using ordinary arrays in C++ and the framework of the STL, see e.g. </p> <div class="literallayout"><p><br>          The C++ Standard Library - A Tutorial and Reference<br>          by Nicolai M. Josuttis<br>          Addison Wesley Longman, 1999<br>          ISBN 0-201-37926-0<br>       </p></div> <p> </p> <p> <a href="http://www.josuttis.com/" target="_top"> Home Page of Nicolai Josuttis </a> </p> </div> <div class="section"> <div class="titlepage"><div><div><h3 class="title"> <a name="array.ack"></a>Acknowledgements</h3></div></div></div> <p>Doug Gregor ported the documentation to the BoostBook format.</p> </div> </div> <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> <td align="left"></td> <td align="right"><div class="copyright-footer">Copyright © 2007 John Maddock, Joel de Guzman, Eric Niebler and Matias Capeletto<p> Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>) </p> </div></td> </tr></table> <hr> <div class="spirit-nav"> <a accesskey="p" href="remez.html"><img src="../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../boost/array.html"><img src="../../../../doc/src/images/next.png" alt="Next"></a> </div> </body> </html>