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">
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>
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>
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>
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>.
35 However, since references are not real objects some restrictions apply and
36 some operations are not available in this case:
38 <div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; ">
40 Converting constructors
52 Value-access via pointer
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:
60 <div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; ">
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
67 Value-access will actually provide access to the referenced object rather
68 than the reference itself.
71 <div class=
"warning"><table border=
"0" summary=
"Warning">
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>
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>.
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
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.
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>
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>)
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>