optional_references.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Optional references</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="../../optional/tutorial.html" title="Tutorial">
  9. <link rel="prev" href="io_operators.html" title="IO operators">
  10. <link rel="next" href="optional_references/rebinding_semantics_for_assignment_of_optional_references.html" title="Rebinding semantics for assignment of optional references">
  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="io_operators.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../optional/tutorial.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="optional_references/rebinding_semantics_for_assignment_of_optional_references.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.tutorial.optional_references"></a><a class="link" href="optional_references.html" title="Optional references">Optional
  28. references</a>
  29. </h3></div></div></div>
  30. <div class="section">
  31. <div class="titlepage"><div><div><h4 class="title">
  32. <a name="boost_optional.tutorial.optional_references.overview"></a><a class="link" href="optional_references.html#boost_optional.tutorial.optional_references.overview" title="Overview">Overview</a>
  33. </h4></div></div></div>
  34. <p>
  35. This library allows the template parameter <code class="computeroutput"><span class="identifier">T</span></code>
  36. to be of reference type: <code class="computeroutput"><span class="identifier">T</span><span class="special">&amp;</span></code>, and to some extent, <code class="computeroutput"><span class="identifier">T</span> <span class="keyword">const</span><span class="special">&amp;</span></code>.
  37. </p>
  38. <p>
  39. However, since references are not real objects some restrictions apply
  40. and some operations are not available in this case:
  41. </p>
  42. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  43. <li class="listitem">
  44. Converting constructors
  45. </li>
  46. <li class="listitem">
  47. Converting assignment
  48. </li>
  49. <li class="listitem">
  50. InPlace construction
  51. </li>
  52. <li class="listitem">
  53. InPlace assignment
  54. </li>
  55. <li class="listitem">
  56. Value-access via pointer
  57. </li>
  58. </ul></div>
  59. <p>
  60. Also, even though <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&amp;&gt;</span></code> treats it wrapped pseudo-object
  61. much as a real value, a true real reference is stored so aliasing will
  62. ocurr:
  63. </p>
  64. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  65. <li class="listitem">
  66. Copies of <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&amp;&gt;</span></code> will copy the references but
  67. all these references will nonetheless refer to the same object.
  68. </li>
  69. <li class="listitem">
  70. Value-access will actually provide access to the referenced object
  71. rather than the reference itself.
  72. </li>
  73. </ul></div>
  74. <div class="caution"><table border="0" summary="Caution">
  75. <tr>
  76. <td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../doc/src/images/caution.png"></td>
  77. <th align="left">Caution</th>
  78. </tr>
  79. <tr><td align="left" valign="top"><p>
  80. On compilers that do not conform to Standard C++ rules of reference binding,
  81. some operations on optional references are disabled in order to prevent
  82. subtle bugs. For more details see <a class="link" href="../dependencies_and_portability/optional_reference_binding.html" title="Optional Reference Binding">Dependencies
  83. and Portability section</a>.
  84. </p></td></tr>
  85. </table></div>
  86. <h6>
  87. <a name="boost_optional.tutorial.optional_references.overview.h0"></a>
  88. <span class="phrase"><a name="boost_optional.tutorial.optional_references.overview.rvalue_references"></a></span><a class="link" href="optional_references.html#boost_optional.tutorial.optional_references.overview.rvalue_references">Rvalue
  89. references</a>
  90. </h6>
  91. <p>
  92. Rvalue references and lvalue references to const have the ability in C++
  93. to extend the life time of a temporary they bind to. Optional references
  94. do not have this capability, therefore to avoid surprising effects it is
  95. not possible to initialize an optional references from a temporary. Optional
  96. rvalue references are disabled altogether. Also, the initialization and
  97. assignment of an optional reference to const from rvalue reference is disabled.
  98. </p>
  99. <pre class="programlisting"><span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> <span class="comment">// legal</span>
  100. <span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="keyword">int</span><span class="special">&amp;&gt;</span> <span class="identifier">oi</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span> <span class="comment">// illegal</span>
  101. </pre>
  102. </div>
  103. </div>
  104. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  105. <td align="left"></td>
  106. <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>
  107. Distributed under the Boost Software License, Version 1.0. (See accompanying
  108. 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>)
  109. </p>
  110. </div></td>
  111. </tr></table>
  112. <hr>
  113. <div class="spirit-nav">
  114. <a accesskey="p" href="io_operators.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../../optional/tutorial.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="optional_references/rebinding_semantics_for_assignment_of_optional_references.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
  115. </div>
  116. </body>
  117. </html>