1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"> 4<title>Array Example Boostbook XML Documentation</title> 5<link rel="stylesheet" href="../../../../doc/src/boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.75.2"> 7<link rel="home" href="../index.html" title="Document To Test Formatting"> 8<link rel="up" href="../index.html" title="Document To Test Formatting"> 9<link rel="prev" href="remez.html" title="Sample Article (The Remez Method)"> 10<link rel="next" href="../boost/array.html" title="Class template array"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../boost.png"></td> 15<td align="center"><a href="../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<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> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h2 class="title" style="clear: both"> 27<a name="document_to_test_formatting.array"></a><a class="link" href="array.html" title="Array Example Boostbook XML Documentation"> Array Example Boostbook 28 XML Documentation</a> 29</h2></div></div></div> 30<div class="toc"><dl> 31<dt><span class="section"><a href="array.html#array.intro">Introduction</a></span></dt> 32<dt><span class="section"><a href="array.html#id772287">Reference</a></span></dt> 33<dt><span class="section"><a href="array.html#array.rationale">Design Rationale</a></span></dt> 34<dt><span class="section"><a href="array.html#array.more.info">For more information...</a></span></dt> 35<dt><span class="section"><a href="array.html#array.ack">Acknowledgements</a></span></dt> 36</dl></div> 37<div class="section"> 38<div class="titlepage"><div><div><h3 class="title"> 39<a name="array.intro"></a>Introduction</h3></div></div></div> 40<p>The C++ Standard Template Library STL as part of the C++ 41 Standard Library provides a framework for processing algorithms on 42 different kind of containers. However, ordinary arrays don't 43 provide the interface of STL containers (although, they provide 44 the iterator interface of STL containers).</p> 45<p>As replacement for ordinary arrays, the STL provides class 46 <code class="computeroutput">std::vector</code>. However, 47 <code class="computeroutput">std::vector<></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.</p> 51<p>In his book, <span class="emphasis"><em>Generic Programming and the 52 STL</em></span>, Matthew H. Austern introduces a useful wrapper 53 class for ordinary arrays with static size, called 54 <code class="computeroutput">block</code>. It is safer and has no worse performance than 55 ordinary arrays. In <span class="emphasis"><em>The C++ Programming 56 Language</em></span>, 3rd edition, Bjarne Stroustrup introduces a 57 similar class, called <code class="computeroutput">c_array</code>, which I (<a href="http://www.josuttis.com" target="_top">Nicolai Josuttis</a>) present 58 slightly modified in my book <span class="emphasis"><em>The C++ Standard Library - 59 A Tutorial and Reference</em></span>, called 60 <code class="computeroutput">carray</code>. This is the essence of these approaches 61 spiced with many feedback from <a href="http://www.boost.org" target="_top">boost</a>.</p> 62<p>After considering different names, we decided to name this 63 class simply <code class="computeroutput"><a class="link" href="../boost/array.html" title="Class template array">array</a></code>.</p> 64<p>Note that this class is suggested to be part of the next 65 Technical Report, which will extend the C++ Standard (see 66 <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1548.htm" target="_top">http://std.dkuug.dk/jtc1/sc22/wg21/docs/papers/2003/n1548.htm</a>).</p> 67<p>Class <code class="computeroutput"><a class="link" href="../boost/array.html" title="Class template array">array</a></code> fulfills most 68 but not all of the requirements of "reversible containers" (see 69 Section 23.1, [lib.container.requirements] of the C++ 70 Standard). The reasons array is not an reversible STL container is 71 because: 72 </p> 73<div class="itemizedlist"><ul class="itemizedlist" type="disc" compact> 74<li class="listitem">No constructors are provided.</li> 75<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> 76<li class="listitem"> 77<code class="computeroutput"><a class="link" href="../boost/array.html#boost.array.swap">swap</a></code>() has no constant complexity.</li> 78<li class="listitem"> 79<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> 80<li class="listitem">The container provides no allocator support.</li> 81</ul></div> 82<p> 83 </p> 84<p>It doesn't fulfill the requirements of a "sequence" (see Section 23.1.1, [lib.sequence.reqmts] of the C++ Standard), except that: 85 </p> 86<div class="itemizedlist"><ul class="itemizedlist" type="disc" compact> 87<li class="listitem"> 88<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> 89<li class="listitem"> 90<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> 91</ul></div> 92<p> 93 </p> 94</div> 95<div class="section"> 96<div class="titlepage"><div><div><h3 class="title"> 97<a name="id772287"></a>Reference</h3></div></div></div> 98<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> 99<div class="section"> 100<div class="titlepage"><div><div><h4 class="title"> 101<a name="header.boost.array_hpp"></a>Header <<a href="../../../../boost/array.hpp" target="_top">boost/array.hpp</a>></h4></div></div></div> 102<pre class="synopsis"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> 103 <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> 104 <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> 105 <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> 106 <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> 107 <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> 108 <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> 109 <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> 110 <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> 111 <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> 112 <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> 113 <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> 114 <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> 115 <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> 116 <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> 117<span class="special">}</span></pre> 118</div> 119</div> 120<div class="section"> 121<div class="titlepage"><div><div><h3 class="title"> 122<a name="array.rationale"></a>Design Rationale</h3></div></div></div> 123<p> 124 There was an important design tradeoff regarding the 125 constructors: We could implement array as an "aggregate" (see 126 Section 8.5.1, [dcl.init.aggr], of the C++ Standard). This would 127 mean: 128 </p> 129<div class="itemizedlist"><ul class="itemizedlist" type="disc"><li class="listitem"> 130<p class="simpara"> 131 An array can be initialized with a 132 brace-enclosing, comma-separated list of initializers for the 133 elements of the container, written in increasing subscript 134 order: 135 </p> 136<pre class="programlisting"> 137 <code class="computeroutput"><a class="link" href="../boost/array.html" title="Class template array">boost::array</a></code><int,4> a = { { 1, 2, 3 } }; 138 </pre> 139<p class="simpara"> 140 Note that if there are fewer elements in the 141 initializer list, then each remaining element gets 142 default-initialized (thus, it has a defined value). 143 </p> 144</li></ul></div> 145<p> 146 </p> 147<p> 148 However, this approach has its drawbacks: <span class="bold"><strong> 149 passing no initializer list means that the elements 150 have an indetermined initial value 151 </strong></span>, because the rule says 152 that aggregates may have: 153 </p> 154<div class="itemizedlist"><ul class="itemizedlist" type="disc"> 155<li class="listitem">No user-declared constructors.</li> 156<li class="listitem">No private or protected non-static data members.</li> 157<li class="listitem">No base classes.</li> 158<li class="listitem">No virtual functions.</li> 159</ul></div> 160<p> 161 </p> 162<p>Nevertheless, The current implementation uses this approach.</p> 163<p> 164 Note that for standard conforming compilers it is possible to 165 use fewer braces (according to 8.5.1 (11) of the Standard). That is, 166 you can initialize an array as follows: 167 </p> 168<pre class="programlisting"> 169 <code class="computeroutput"><a class="link" href="../boost/array.html" title="Class template array">boost::array</a></code><int,4> a = { 1, 2, 3 }; 170 </pre> 171<p> 172 I'd appreciate any constructive feedback. <span class="bold"><strong> 173 Please note: I don't have time to read all boost 174 mails. Thus, to make sure that feedback arrives to me, please send 175 me a copy of each mail regarding this class. 176 </strong></span> 177 </p> 178<p> 179 The code is provided "as is" without expressed or implied 180 warranty. 181 </p> 182</div> 183<div class="section"> 184<div class="titlepage"><div><div><h3 class="title"> 185<a name="array.more.info"></a>For more information...</h3></div></div></div> 186<p> 187 To find more details about using ordinary arrays in C++ and 188 the framework of the STL, see e.g. 189 190 </p> 191<div class="literallayout"><p><br> 192         The C++ Standard Library - A Tutorial and Reference<br> 193         by Nicolai M. Josuttis<br> 194         Addison Wesley Longman, 1999<br> 195         ISBN 0-201-37926-0<br> 196      </p></div> 197<p> 198 </p> 199<p> 200 <a href="http://www.josuttis.com/" target="_top"> 201 Home Page of Nicolai 202 Josuttis 203 </a> 204 </p> 205</div> 206<div class="section"> 207<div class="titlepage"><div><div><h3 class="title"> 208<a name="array.ack"></a>Acknowledgements</h3></div></div></div> 209<p>Doug Gregor ported the documentation to the BoostBook format.</p> 210</div> 211</div> 212<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 213<td align="left"></td> 214<td align="right"><div class="copyright-footer">Copyright © 2007 John Maddock, Joel de Guzman, Eric Niebler and Matias 215 Capeletto<p> 216 Distributed under the Boost Software License, Version 1.0. (See accompanying 217 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>) 218 </p> 219</div></td> 220</tr></table> 221<hr> 222<div class="spirit-nav"> 223<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> 224</div> 225</body> 226</html> 227