2014-06-04 18:13:06 +02:00
< html >
< head >
2020-10-24 17:17:10 +02:00
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
2014-06-04 18:13:06 +02:00
< title > Optional references</ title >
< link rel = "stylesheet" href = "../../../../../../doc/src/boostbook.css" type = "text/css" >
2018-03-24 00:04:15 +01:00
< meta name = "generator" content = "DocBook XSL Stylesheets Vsnapshot" >
2014-06-06 00:53:15 +02:00
< link rel = "home" href = "../../index.html" title = "Boost.Optional" >
2014-06-04 18:13:06 +02:00
< link rel = "up" href = "../../optional/tutorial.html" title = "Tutorial" >
2015-01-21 00:10:51 +01:00
< link rel = "prev" href = "io_operators.html" title = "IO operators" >
2016-02-18 23:18:01 +01:00
< link rel = "next" href = "optional_references/rebinding_semantics_for_assignment_of_optional_references.html" title = "Rebinding semantics for assignment of optional references" >
2014-06-04 18:13:06 +02:00
</ head >
< body bgcolor = "white" text = "black" link = "#0000FF" vlink = "#840084" alink = "#0000FF" >
< table cellpadding = "2" width = "100%" >< tr >
< td valign = "top" >< img alt = "Boost C++ Libraries" width = "277" height = "86" src = "../../../../../../boost.png" ></ td >
< td align = "center" >< a href = "../../../../../../index.html" > Home</ a ></ td >
< td align = "center" >< a href = "../../../../../../libs/libraries.htm" > Libraries</ a ></ td >
< td align = "center" >< a href = "http://www.boost.org/users/people.html" > People</ a ></ td >
< td align = "center" >< a href = "http://www.boost.org/users/faq.html" > FAQ</ a ></ td >
< td align = "center" >< a href = "../../../../../../more/index.htm" > More</ a ></ td >
</ tr ></ table >
< hr >
< div class = "spirit-nav" >
2016-02-18 23:18:01 +01:00
< 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 >
2014-06-04 18:13:06 +02:00
</ div >
< div class = "section" >
< div class = "titlepage" >< div >< div >< h3 class = "title" >
< a name = "boost_optional.tutorial.optional_references" ></ a >< a class = "link" href = "optional_references.html" title = "Optional references" > Optional
references</ a >
</ h3 ></ div ></ div ></ div >
2016-02-18 23:18:01 +01:00
< div class = "section" >
< div class = "titlepage" >< div >< div >< h4 class = "title" >
< 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 >
</ h4 ></ div ></ div ></ div >
2014-06-04 18:13:06 +02:00
< p >
2016-02-18 23:18:01 +01:00
This library allows the template parameter < code class = "computeroutput" >< span class = "identifier" > T</ span ></ code >
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 > .
</ p >
2014-06-04 18:13:06 +02:00
< p >
2016-02-18 23:18:01 +01:00
However, since references are not real objects some restrictions apply
and some operations are not available in this case:
</ p >
2014-06-04 18:13:06 +02:00
< div class = "itemizedlist" >< ul class = "itemizedlist" style = "list-style-type: disc; " >
< li class = "listitem" >
2016-02-18 23:18:01 +01:00
Converting constructors
</ li >
2014-06-04 18:13:06 +02:00
< li class = "listitem" >
2016-02-18 23:18:01 +01:00
Converting assignment
</ li >
2014-06-04 18:13:06 +02:00
< li class = "listitem" >
2016-02-18 23:18:01 +01:00
InPlace construction
</ li >
2014-06-04 18:13:06 +02:00
< li class = "listitem" >
2016-02-18 23:18:01 +01:00
InPlace assignment
</ li >
2014-06-04 18:13:06 +02:00
< li class = "listitem" >
2016-02-18 23:18:01 +01:00
Value-access via pointer
</ li >
2014-06-04 18:13:06 +02:00
</ ul ></ div >
< p >
2016-02-18 23:18:01 +01:00
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
much as a real value, a true real reference is stored so aliasing will
2022-11-01 21:46:30 -04:00
occur:
2016-02-18 23:18:01 +01:00
</ p >
2014-06-04 18:13:06 +02:00
< div class = "itemizedlist" >< ul class = "itemizedlist" style = "list-style-type: disc; " >
< li class = "listitem" >
2016-02-18 23:18:01 +01:00
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
all these references will nonetheless refer to the same object.
</ li >
2014-06-04 18:13:06 +02:00
< li class = "listitem" >
2016-02-18 23:18:01 +01:00
Value-access will actually provide access to the referenced object
rather than the reference itself.
</ li >
2014-06-04 18:13:06 +02:00
</ ul ></ div >
2016-02-18 23:18:01 +01:00
< div class = "caution" >< table border = "0" summary = "Caution" >
2014-06-04 18:13:06 +02:00
< tr >
2016-02-18 23:18:01 +01:00
< td rowspan = "2" align = "center" valign = "top" width = "25" >< img alt = "[Caution]" src = "../../../../../../doc/src/images/caution.png" ></ td >
< th align = "left" > Caution</ th >
2014-06-04 18:13:06 +02:00
</ tr >
< tr >< td align = "left" valign = "top" >< p >
2016-02-18 23:18:01 +01:00
On compilers that do not conform to Standard C++ rules of reference binding,
some operations on optional references are disabled in order to prevent
subtle bugs. For more details see < a class = "link" href = "../dependencies_and_portability/optional_reference_binding.html" title = "Optional Reference Binding" > Dependencies
and Portability section</ a > .
</ p ></ td ></ tr >
2014-06-04 18:13:06 +02:00
</ table ></ div >
2016-02-18 23:18:01 +01:00
< h6 >
< a name = "boost_optional.tutorial.optional_references.overview.h0" ></ a >
< 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
references</ a >
</ h6 >
2014-06-04 18:13:06 +02:00
< p >
2016-02-18 23:18:01 +01:00
Rvalue references and lvalue references to const have the ability in C++
to extend the life time of a temporary they bind to. Optional references
do not have this capability, therefore to avoid surprising effects it is
not possible to initialize an optional references from a temporary. Optional
rvalue references are disabled altogether. Also, the initialization and
assignment of an optional reference to const from rvalue reference is disabled.
</ p >
2014-06-04 18:13:06 +02:00
< 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 >
< 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 >
</ pre >
</ div >
2016-02-18 23:18:01 +01:00
</ div >
2014-06-04 18:13:06 +02:00
< table xmlns:rev = "http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width = "100%" >< tr >
< td align = "left" ></ td >
2021-03-10 23:07:18 +01:00
< td align = "right" >< div class = "copyright-footer" > Copyright © 2003-2007 Fernando Luis Cacciola Carballal< br > Copyright © 2014-2021 Andrzej Krzemieński< p >
2014-06-04 18:13:06 +02:00
Distributed under the Boost Software License, Version 1.0. (See accompanying
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 > )
</ p >
</ div ></ td >
</ tr ></ table >
< hr >
< div class = "spirit-nav" >
2016-02-18 23:18:01 +01:00
< 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 >
2014-06-04 18:13:06 +02:00
</ div >
</ body >
</ html >