the_semantics.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>The semantics</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="../design_overview.html" title="Design Overview">
  9. <link rel="prev" href="../design_overview.html" title="Design Overview">
  10. <link rel="next" href="the_interface.html" title="The Interface">
  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="../design_overview.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../design_overview.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="the_interface.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h4 class="title">
  27. <a name="boost_optional.tutorial.design_overview.the_semantics"></a><a class="link" href="the_semantics.html" title="The semantics">The
  28. semantics</a>
  29. </h4></div></div></div>
  30. <p>
  31. Objects of type <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> are intended to be used in places where
  32. objects of type <code class="computeroutput"><span class="identifier">T</span></code> would
  33. but which might be uninitialized. Hence, <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>'s purpose is to formalize the additional
  34. possibly uninitialized state. From the perspective of this role, <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
  35. can have the same operational semantics of <code class="computeroutput"><span class="identifier">T</span></code>
  36. plus the additional semantics corresponding to this special state. As such,
  37. <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
  38. could be thought of as a <span class="emphasis"><em>supertype</em></span> of <code class="computeroutput"><span class="identifier">T</span></code>. Of course, we can't do that in C++,
  39. so we need to compose the desired semantics using a different mechanism.
  40. Doing it the other way around, that is, making <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> a <span class="emphasis"><em>subtype</em></span> of
  41. <code class="computeroutput"><span class="identifier">T</span></code> is not only conceptually
  42. wrong but also impractical: it is not allowed to derive from a non-class
  43. type, such as a built-in type.
  44. </p>
  45. <p>
  46. We can draw from the purpose of <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> the required basic semantics:
  47. </p>
  48. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  49. <li class="listitem">
  50. <span class="bold"><strong>Default Construction:</strong></span> To introduce
  51. a formally uninitialized wrapped object.
  52. </li>
  53. <li class="listitem">
  54. <span class="bold"><strong>Direct Value Construction via copy:</strong></span>
  55. To introduce a formally initialized wrapped object whose value is obtained
  56. as a copy of some object.
  57. </li>
  58. <li class="listitem">
  59. <span class="bold"><strong>Deep Copy Construction:</strong></span> To obtain
  60. a new yet equivalent wrapped object.
  61. </li>
  62. <li class="listitem">
  63. <span class="bold"><strong>Direct Value Assignment (upon initialized):</strong></span>
  64. To assign a value to the wrapped object.
  65. </li>
  66. <li class="listitem">
  67. <span class="bold"><strong>Direct Value Assignment (upon uninitialized):</strong></span>
  68. To initialize the wrapped object with a value obtained as a copy of
  69. some object.
  70. </li>
  71. <li class="listitem">
  72. <span class="bold"><strong>Assignment (upon initialized):</strong></span> To
  73. assign to the wrapped object the value of another wrapped object.
  74. </li>
  75. <li class="listitem">
  76. <span class="bold"><strong>Assignment (upon uninitialized):</strong></span> To
  77. initialize the wrapped object with value of another wrapped object.
  78. </li>
  79. <li class="listitem">
  80. <span class="bold"><strong>Deep Relational Operations (when supported by
  81. the type T):</strong></span> To compare wrapped object values taking into
  82. account the presence of uninitialized states.
  83. </li>
  84. <li class="listitem">
  85. <span class="bold"><strong>Value access:</strong></span> To unwrap the wrapped
  86. object.
  87. </li>
  88. <li class="listitem">
  89. <span class="bold"><strong>Initialization state query:</strong></span> To determine
  90. if the object is formally initialized or not.
  91. </li>
  92. <li class="listitem">
  93. <span class="bold"><strong>Swap:</strong></span> To exchange wrapped objects.
  94. (with whatever exception safety guarantees are provided by <code class="computeroutput"><span class="identifier">T</span></code>'s swap).
  95. </li>
  96. <li class="listitem">
  97. <span class="bold"><strong>De-initialization:</strong></span> To release the
  98. wrapped object (if any) and leave the wrapper in the uninitialized
  99. state.
  100. </li>
  101. </ul></div>
  102. <p>
  103. Additional operations are useful, such as converting constructors and converting
  104. assignments, in-place construction and assignment, and safe value access
  105. via a pointer to the wrapped object or null.
  106. </p>
  107. </div>
  108. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  109. <td align="left"></td>
  110. <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>
  111. Distributed under the Boost Software License, Version 1.0. (See accompanying
  112. 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>)
  113. </p>
  114. </div></td>
  115. </tr></table>
  116. <hr>
  117. <div class="spirit-nav">
  118. <a accesskey="p" href="../design_overview.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../design_overview.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="the_interface.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
  119. </div>
  120. </body>
  121. </html>