faq.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.3.10: http://docutils.sourceforge.net/" />
  7. <title>Boost Pointer Container Library</title>
  8. <style type="text/css">
  9. /*
  10. :Author: David Goodger
  11. :Contact: goodger@users.sourceforge.net
  12. :Date: $Date$
  13. :Revision: $Revision$
  14. :Copyright: This stylesheet has been placed in the public domain.
  15. Default cascading style sheet for the HTML output of Docutils.
  16. See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
  17. customize this style sheet.
  18. */
  19. /* "! important" is used here to override other ``margin-top`` and
  20. ``margin-bottom`` styles that are later in the stylesheet or
  21. more specific. See http://www.w3.org/TR/CSS1#the-cascade */
  22. .first {
  23. margin-top: 0 ! important }
  24. .last, .with-subtitle {
  25. margin-bottom: 0 ! important }
  26. .hidden {
  27. display: none }
  28. a.toc-backref {
  29. text-decoration: none ;
  30. color: black }
  31. blockquote.epigraph {
  32. margin: 2em 5em ; }
  33. dl.docutils dd {
  34. margin-bottom: 0.5em }
  35. /* Uncomment (and remove this text!) to get bold-faced definition list terms
  36. dl.docutils dt {
  37. font-weight: bold }
  38. */
  39. div.abstract {
  40. margin: 2em 5em }
  41. div.abstract p.topic-title {
  42. font-weight: bold ;
  43. text-align: center }
  44. div.admonition, div.attention, div.caution, div.danger, div.error,
  45. div.hint, div.important, div.note, div.tip, div.warning {
  46. margin: 2em ;
  47. border: medium outset ;
  48. padding: 1em }
  49. div.admonition p.admonition-title, div.hint p.admonition-title,
  50. div.important p.admonition-title, div.note p.admonition-title,
  51. div.tip p.admonition-title {
  52. font-weight: bold ;
  53. font-family: sans-serif }
  54. div.attention p.admonition-title, div.caution p.admonition-title,
  55. div.danger p.admonition-title, div.error p.admonition-title,
  56. div.warning p.admonition-title {
  57. color: red ;
  58. font-weight: bold ;
  59. font-family: sans-serif }
  60. /* Uncomment (and remove this text!) to get reduced vertical space in
  61. compound paragraphs.
  62. div.compound .compound-first, div.compound .compound-middle {
  63. margin-bottom: 0.5em }
  64. div.compound .compound-last, div.compound .compound-middle {
  65. margin-top: 0.5em }
  66. */
  67. div.dedication {
  68. margin: 2em 5em ;
  69. text-align: center ;
  70. font-style: italic }
  71. div.dedication p.topic-title {
  72. font-weight: bold ;
  73. font-style: normal }
  74. div.figure {
  75. margin-left: 2em }
  76. div.footer, div.header {
  77. clear: both;
  78. font-size: smaller }
  79. div.line-block {
  80. display: block ;
  81. margin-top: 1em ;
  82. margin-bottom: 1em }
  83. div.line-block div.line-block {
  84. margin-top: 0 ;
  85. margin-bottom: 0 ;
  86. margin-left: 1.5em }
  87. div.sidebar {
  88. margin-left: 1em ;
  89. border: medium outset ;
  90. padding: 1em ;
  91. background-color: #ffffee ;
  92. width: 40% ;
  93. float: right ;
  94. clear: right }
  95. div.sidebar p.rubric {
  96. font-family: sans-serif ;
  97. font-size: medium }
  98. div.system-messages {
  99. margin: 5em }
  100. div.system-messages h1 {
  101. color: red }
  102. div.system-message {
  103. border: medium outset ;
  104. padding: 1em }
  105. div.system-message p.system-message-title {
  106. color: red ;
  107. font-weight: bold }
  108. div.topic {
  109. margin: 2em }
  110. h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
  111. h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
  112. margin-top: 0.4em }
  113. h1.title {
  114. text-align: center }
  115. h2.subtitle {
  116. text-align: center }
  117. hr.docutils {
  118. width: 75% }
  119. img.align-left {
  120. clear: left }
  121. img.align-right {
  122. clear: right }
  123. img.borderless {
  124. border: 0 }
  125. ol.simple, ul.simple {
  126. margin-bottom: 1em }
  127. ol.arabic {
  128. list-style: decimal }
  129. ol.loweralpha {
  130. list-style: lower-alpha }
  131. ol.upperalpha {
  132. list-style: upper-alpha }
  133. ol.lowerroman {
  134. list-style: lower-roman }
  135. ol.upperroman {
  136. list-style: upper-roman }
  137. p.attribution {
  138. text-align: right ;
  139. margin-left: 50% }
  140. p.caption {
  141. font-style: italic }
  142. p.credits {
  143. font-style: italic ;
  144. font-size: smaller }
  145. p.label {
  146. white-space: nowrap }
  147. p.rubric {
  148. font-weight: bold ;
  149. font-size: larger ;
  150. color: maroon ;
  151. text-align: center }
  152. p.sidebar-title {
  153. font-family: sans-serif ;
  154. font-weight: bold ;
  155. font-size: larger }
  156. p.sidebar-subtitle {
  157. font-family: sans-serif ;
  158. font-weight: bold }
  159. p.topic-title {
  160. font-weight: bold }
  161. pre.address {
  162. margin-bottom: 0 ;
  163. margin-top: 0 ;
  164. font-family: serif ;
  165. font-size: 100% }
  166. pre.line-block {
  167. font-family: serif ;
  168. font-size: 100% }
  169. pre.literal-block, pre.doctest-block {
  170. margin-left: 2em ;
  171. margin-right: 2em ;
  172. background-color: #eeeeee }
  173. span.classifier {
  174. font-family: sans-serif ;
  175. font-style: oblique }
  176. span.classifier-delimiter {
  177. font-family: sans-serif ;
  178. font-weight: bold }
  179. span.interpreted {
  180. font-family: sans-serif }
  181. span.option {
  182. white-space: nowrap }
  183. span.pre {
  184. white-space: pre }
  185. span.problematic {
  186. color: red }
  187. span.section-subtitle {
  188. /* font-size relative to parent (h1..h6 element) */
  189. font-size: 80% }
  190. table.citation {
  191. border-left: solid thin gray }
  192. table.docinfo {
  193. margin: 2em 4em }
  194. table.docutils {
  195. margin-top: 0.5em ;
  196. margin-bottom: 0.5em }
  197. table.footnote {
  198. border-left: solid thin black }
  199. table.docutils td, table.docutils th,
  200. table.docinfo td, table.docinfo th {
  201. padding-left: 0.5em ;
  202. padding-right: 0.5em ;
  203. vertical-align: top }
  204. table.docutils th.field-name, table.docinfo th.docinfo-name {
  205. font-weight: bold ;
  206. text-align: left ;
  207. white-space: nowrap ;
  208. padding-left: 0 }
  209. h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
  210. h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
  211. font-size: 100% }
  212. tt.docutils {
  213. background-color: #eeeeee }
  214. ul.auto-toc {
  215. list-style-type: none }
  216. </style>
  217. </head>
  218. <body>
  219. <div class="document" id="boost-pointer-container-library">
  220. <h1 class="title"><img alt="Boost" src="boost.png" /> Pointer Container Library</h1>
  221. <h2 class="subtitle" id="faq">FAQ</h2>
  222. <div class="contents local topic">
  223. <ul class="simple">
  224. <li><a class="reference" href="#calling-assign-is-very-costly-and-i-do-not-really-need-to-store-cloned-objects-i-merely-need-to-overwrite-the-existing-ones-what-do-i-do" id="id5" name="id5">Calling <tt class="docutils literal"><span class="pre">assign()</span></tt> is very costly and I do not really need to store cloned objects; I merely need to overwrite the existing ones; what do I do?</a></li>
  225. <li><a class="reference" href="#which-mutating-algorithms-are-safe-to-use-with-pointers" id="id6" name="id6">Which mutating algorithms are safe to use with pointers?</a></li>
  226. <li><a class="reference" href="#why-does-ptr-map-t-insert-replace-take-two-arguments-the-key-and-the-pointer-instead-of-one-std-pair-and-why-is-the-key-passed-by-non-const-reference" id="id7" name="id7">Why does <tt class="docutils literal"><span class="pre">ptr_map&lt;T&gt;::insert()/replace()</span></tt> take two arguments (the key and the pointer) instead of one <tt class="docutils literal"><span class="pre">std::pair</span></tt>? And why is the key passed by non-const reference?</a></li>
  227. <li><a class="reference" href="#when-instantiating-a-pointer-container-with-a-type-t-is-t-then-allowed-to-be-incomplete-at-that-point" id="id8" name="id8">When instantiating a pointer container with a type <tt class="docutils literal"><span class="pre">T</span></tt>, is <tt class="docutils literal"><span class="pre">T</span></tt> then allowed to be incomplete at that point?</a></li>
  228. <li><a class="reference" href="#why-do-iterator-range-inserts-give-the-strong-exception-safety-guarantee" id="id9" name="id9">Why do iterator-range inserts give the strong exception-safety guarantee?</a></li>
  229. <li><a class="reference" href="#what-is-the-polymorphic-class-problem" id="id10" name="id10">What is the polymorphic class problem?</a></li>
  230. <li><a class="reference" href="#are-the-pointer-containers-faster-and-do-they-have-a-better-memory-footprint-than-a-container-of-smart-pointers" id="id11" name="id11">Are the pointer containers faster and do they have a better memory footprint than a container of smart pointers?</a></li>
  231. <li><a class="reference" href="#when-the-stored-pointers-cannot-be-0-how-do-i-allow-this-empty-behavior-anyway" id="id12" name="id12">When the stored pointers cannot be <tt class="docutils literal"><span class="pre">0</span></tt>, how do I allow this &quot;empty&quot; behavior anyway?</a></li>
  232. </ul>
  233. </div>
  234. <div class="section">
  235. <h1><a class="toc-backref" href="#id5" id="calling-assign-is-very-costly-and-i-do-not-really-need-to-store-cloned-objects-i-merely-need-to-overwrite-the-existing-ones-what-do-i-do" name="calling-assign-is-very-costly-and-i-do-not-really-need-to-store-cloned-objects-i-merely-need-to-overwrite-the-existing-ones-what-do-i-do">Calling <tt class="docutils literal docutils literal"><span class="pre">assign()</span></tt> is very costly and I do not really need to store cloned objects; I merely need to overwrite the existing ones; what do I do?</a></h1>
  236. <p>Call <tt class="docutils literal"><span class="pre">std::copy(</span> <span class="pre">first,</span> <span class="pre">last,</span> <span class="pre">c.begin()</span> <span class="pre">);</span></tt>.</p>
  237. </div>
  238. <div class="section">
  239. <h1><a class="toc-backref" href="#id6" id="which-mutating-algorithms-are-safe-to-use-with-pointers" name="which-mutating-algorithms-are-safe-to-use-with-pointers">Which mutating algorithms are safe to use with pointers?</a></h1>
  240. <p>Any mutating algorithm that moves elements around by swapping them. An
  241. important example is <tt class="docutils literal"><span class="pre">std::sort()</span></tt>; examples of unsafe algorithms are
  242. <tt class="docutils literal"><span class="pre">std::unique()</span></tt> and <tt class="docutils literal"><span class="pre">std::remove()</span></tt>.</p>
  243. <!-- That is why these algorithms are
  244. provided as member functions. -->
  245. </div>
  246. <div class="section">
  247. <h1><a class="toc-backref" href="#id7" id="why-does-ptr-map-t-insert-replace-take-two-arguments-the-key-and-the-pointer-instead-of-one-std-pair-and-why-is-the-key-passed-by-non-const-reference" name="why-does-ptr-map-t-insert-replace-take-two-arguments-the-key-and-the-pointer-instead-of-one-std-pair-and-why-is-the-key-passed-by-non-const-reference">Why does <tt class="docutils literal docutils literal"><span class="pre">ptr_map&lt;T&gt;::insert()/replace()</span></tt> take two arguments (the key and the pointer) instead of one <tt class="docutils literal docutils literal"><span class="pre">std::pair</span></tt>? And why is the key passed by non-const reference?</a></h1>
  248. <p>This is the only way the function can be implemented in an exception-safe
  249. manner; since the copy-constructor of the key might throw, and since
  250. function arguments are not guaranteed to be evaluated from left to right,
  251. we need to ensure that evaluating the first argument does not throw.
  252. Passing the key as a reference achieves just that.</p>
  253. </div>
  254. <div class="section">
  255. <h1><a class="toc-backref" href="#id8" id="when-instantiating-a-pointer-container-with-a-type-t-is-t-then-allowed-to-be-incomplete-at-that-point" name="when-instantiating-a-pointer-container-with-a-type-t-is-t-then-allowed-to-be-incomplete-at-that-point">When instantiating a pointer container with a type <tt class="docutils literal docutils literal"><span class="pre">T</span></tt>, is <tt class="docutils literal docutils literal"><span class="pre">T</span></tt> then allowed to be incomplete at that point?</a></h1>
  256. <p>No. This is a distinct property of <tt class="docutils literal"><span class="pre">shared_ptr</span></tt> which implies some overhead.</p>
  257. <p>However, one can leave <tt class="docutils literal"><span class="pre">T</span></tt> incomplete in the header file:</p>
  258. <pre class="literal-block">
  259. // foo.hpp
  260. class Foo { ... };
  261. new_clone( const Foo&amp; ) { ... }
  262. delete_clone( const Foo* ) { ... }
  263. // x.hpp
  264. class Foo; // Foo is incomplete here
  265. class X { ptr_deque&lt;Foo&gt; container; ... }
  266. // x.cpp
  267. #include &lt;x.hpp&gt;
  268. #include &lt;foo.hpp&gt; // now Foo is not incomplete anymore
  269. ...
  270. </pre>
  271. </div>
  272. <div class="section">
  273. <h1><a class="toc-backref" href="#id9" id="why-do-iterator-range-inserts-give-the-strong-exception-safety-guarantee" name="why-do-iterator-range-inserts-give-the-strong-exception-safety-guarantee">Why do iterator-range inserts give the strong exception-safety guarantee?</a></h1>
  274. <p>Is this not very inefficient? It is because it is actually affordable to
  275. do so; the overhead is one heap-allocation which is relatively small
  276. compared to cloning N objects.</p>
  277. </div>
  278. <div class="section">
  279. <h1><a class="toc-backref" href="#id10" id="what-is-the-polymorphic-class-problem" name="what-is-the-polymorphic-class-problem">What is the <span class="target" id="polymorphic-class-problem">polymorphic class problem</span>?</a></h1>
  280. <p>The problem refers to the relatively troublesome way C++ supports Object
  281. Oriented programming in connection with containers of pointers to
  282. polymorphic objects. In a language without garbage collection, you end up
  283. using either a container of smart pointers or a container that takes
  284. ownership of the pointers. The hard part is to find a safe, fast and
  285. elegant solution.</p>
  286. </div>
  287. <div class="section">
  288. <h1><a class="toc-backref" href="#id11" id="are-the-pointer-containers-faster-and-do-they-have-a-better-memory-footprint-than-a-container-of-smart-pointers" name="are-the-pointer-containers-faster-and-do-they-have-a-better-memory-footprint-than-a-container-of-smart-pointers">Are the pointer containers faster and do they have a better memory footprint than a container of smart pointers?</a></h1>
  289. <p>The short answer is yes: they are faster and they do use less memory; in
  290. fact, they are the only way to obtain the zero-overhead hallmark of C++.
  291. Smart pointers usually have one word or more of memory overhead per
  292. pointer because a reference count must be maintained. And since the
  293. reference count must be maintained, there is also a runtime-overhead. If
  294. your objects are big, then the memory overhead is often negligible, but if
  295. you have many small objects, it is not. Further reading can be found in
  296. these references: <a class="reference" href="ptr_container.html#references">[11]</a> and <a class="reference" href="ptr_container.html#references">[12]</a>.</p>
  297. </div>
  298. <div class="section">
  299. <h1><a class="toc-backref" href="#id12" id="when-the-stored-pointers-cannot-be-0-how-do-i-allow-this-empty-behavior-anyway" name="when-the-stored-pointers-cannot-be-0-how-do-i-allow-this-empty-behavior-anyway">When the stored pointers cannot be <tt class="docutils literal docutils literal"><span class="pre">0</span></tt>, how do I allow this &quot;empty&quot; behavior anyway?</a></h1>
  300. <p>Storing a null-pointer among a list of pointers does not fit well into the Object Oriented paradigm.
  301. The most elegant design is to use the Null-Object Pattern where one basically makes a concrete
  302. class with dummy implementations of the virtual functions. See <a class="reference" href="ptr_container.html#references">[13]</a> for details.</p>
  303. <hr><table class="docutils field-list" frame="void" rules="none">
  304. <col class="field-name" />
  305. <col class="field-body" />
  306. <tbody valign="top">
  307. <tr class="field"><th class="field-name">Copyright:</th><td class="field-body">Thorsten Ottosen 2004-2006. Use, modification and distribution is subject to the Boost Software License, Version 1.0 (see <a class="reference" href="http://www.boost.org/LICENSE_1_0.txt">LICENSE_1_0.txt</a>).</td>
  308. </tr>
  309. </tbody>
  310. </table>
  311. </div>
  312. </div>
  313. </body>
  314. </html>