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