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=
"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">
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=
"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>
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
29 </h3></div></div></div>
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>
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">&</span></code>, and to some extent,
<code class=
"computeroutput"><span class=
"identifier">T
</span> <span class=
"keyword">const
</span><span class=
"special">&</span></code>.
39 However, since references are not real objects some restrictions apply
40 and some operations are not available in this case:
42 <div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; ">
44 Converting constructors
56 Value-access via pointer
60 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> treats it wrapped pseudo-object
61 much as a real value, a true real reference is stored so aliasing will
64 <div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; ">
66 Copies of
<code class=
"computeroutput"><span class=
"identifier">optional
</span><span class=
"special"><</span><span class=
"identifier">T
</span><span class=
"special">&></span></code> will copy the references but
67 all these references will nonetheless refer to the same object.
70 Value-access will actually provide access to the referenced object
71 rather than the reference itself.
74 <div class=
"caution"><table border=
"0" summary=
"Caution">
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>
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>.
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
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.
99 <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>
100 <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>
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
© 2003-
2007 Fernando Luis Cacciola Carballal
<br>Copyright
© 2014-
2016 Andrzej Krzemie
ń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>)
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>