optional_data_members.html 7.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Optional data members</title>
  5. <link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../../index.html" title="Boost.Optional">
  8. <link rel="up" href="../quick_start.html" title="Quick Start">
  9. <link rel="prev" href="optional_automatic_variables.html" title="Optional automatic variables">
  10. <link rel="next" href="bypassing_unnecessary_default_construction.html" title="Bypassing unnecessary default construction">
  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="optional_automatic_variables.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../quick_start.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="bypassing_unnecessary_default_construction.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h3 class="title">
  27. <a name="boost_optional.quick_start.optional_data_members"></a><a class="link" href="optional_data_members.html" title="Optional data members">Optional
  28. data members</a>
  29. </h3></div></div></div>
  30. <p>
  31. Suppose we want to implement a <span class="emphasis"><em>lazy load</em></span> optimization.
  32. This is because we do not want to perform an expensive initialization of
  33. our <code class="computeroutput"><span class="identifier">Resource</span></code> until (if at
  34. all) it is really used. We can do it this way:
  35. </p>
  36. <pre class="programlisting"><span class="keyword">class</span> <span class="identifier">Widget</span>
  37. <span class="special">{</span>
  38. <span class="keyword">mutable</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="identifier">Resource</span><span class="special">&gt;</span> <span class="identifier">resource_</span><span class="special">;</span>
  39. <span class="keyword">public</span><span class="special">:</span>
  40. <span class="identifier">Widget</span><span class="special">()</span> <span class="special">{}</span>
  41. <span class="keyword">const</span> <span class="identifier">Resource</span><span class="special">&amp;</span> <span class="identifier">getResource</span><span class="special">()</span> <span class="keyword">const</span> <span class="comment">// not thread-safe</span>
  42. <span class="special">{</span>
  43. <span class="keyword">if</span> <span class="special">(</span><span class="identifier">resource_</span> <span class="special">==</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">none</span><span class="special">)</span>
  44. <span class="identifier">resource_</span><span class="special">.</span><span class="identifier">emplace</span><span class="special">(</span><span class="string">"resource"</span><span class="special">,</span> <span class="string">"arguments"</span><span class="special">);</span>
  45. <span class="keyword">return</span> <span class="special">*</span><span class="identifier">resource_</span><span class="special">;</span>
  46. <span class="special">}</span>
  47. <span class="special">};</span>
  48. </pre>
  49. <p>
  50. <code class="computeroutput"><span class="identifier">optional</span></code>'s default constructor
  51. creates an uninitialized optional. No call to <code class="computeroutput"><span class="identifier">Resource</span></code>'s
  52. default constructor is attempted. <code class="computeroutput"><span class="identifier">Resource</span></code>
  53. doesn't have to be <a href="http://www.sgi.com/tech/stl/DefaultConstructible.html" target="_top"><code class="computeroutput"><span class="identifier">DefaultConstructible</span></code></a>. In function
  54. <code class="computeroutput"><span class="identifier">getResource</span></code> we first check
  55. if <code class="computeroutput"><span class="identifier">resource_</span></code> is initialized.
  56. This time we do not use the contextual conversion to <code class="computeroutput"><span class="keyword">bool</span></code>,
  57. but a comparison with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">none</span></code>.
  58. These two ways are equivalent. Function <code class="computeroutput"><span class="identifier">emplace</span></code>
  59. initializes the optional in-place by perfect-forwarding the arguments to
  60. the constructor of <code class="computeroutput"><span class="identifier">Resource</span></code>.
  61. No copy- or move-construction is involved here. <code class="computeroutput"><span class="identifier">Resource</span></code>
  62. doesn't even have to be <code class="computeroutput"><span class="identifier">MoveConstructible</span></code>.
  63. </p>
  64. <div class="note"><table border="0" summary="Note">
  65. <tr>
  66. <td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../doc/src/images/note.png"></td>
  67. <th align="left">Note</th>
  68. </tr>
  69. <tr><td align="left" valign="top"><p>
  70. Function <code class="computeroutput"><span class="identifier">emplace</span></code> is only
  71. available on compilers that support rvalue references and variadic templates.
  72. If your compiler does not support these features and you still need to
  73. avoid any move-constructions, use <a class="link" href="../tutorial/in_place_factories.html" title="In-Place Factories">In-Place
  74. Factories</a>.
  75. </p></td></tr>
  76. </table></div>
  77. </div>
  78. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  79. <td align="left"></td>
  80. <td align="right"><div class="copyright-footer">Copyright &#169; 2003-2007 Fernando Luis Cacciola Carballal<br>Copyright &#169; 2014-2018 Andrzej Krzemie&#324;ski<p>
  81. Distributed under the Boost Software License, Version 1.0. (See accompanying
  82. 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>)
  83. </p>
  84. </div></td>
  85. </tr></table>
  86. <hr>
  87. <div class="spirit-nav">
  88. <a accesskey="p" href="optional_automatic_variables.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../quick_start.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="bypassing_unnecessary_default_construction.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  89. </div>
  90. </body>
  91. </html>