2007-05-29 06:40:25 +00:00
< html >
< head >
2011-04-06 20:54:18 +00:00
< meta http-equiv = "Content-Type" content = "text/html; charset=US-ASCII" >
2007-05-29 06:40:25 +00:00
< title > Optional references< / title >
2014-04-12 20:54:37 +02:00
< link rel = "stylesheet" href = "../../../../../doc/src/boostbook.css" type = "text/css" >
< meta name = "generator" content = "DocBook XSL Stylesheets V1.78.1" >
2011-04-06 20:54:18 +00:00
< link rel = "home" href = "../index.html" title = "Chapter 1. Boost.Optional" >
< link rel = "up" href = "../index.html" title = "Chapter 1. Boost.Optional" >
2014-06-04 18:13:06 +02:00
< link rel = "prev" href = "detailed_semantics.html" title = "Detailed Semantics" >
2009-02-12 14:01:48 +00:00
< link rel = "next" href = "rebinding_semantics_for_assignment_of_optional_references.html" title = "Rebinding semantics for assignment of optional references" >
2007-05-29 06:40:25 +00:00
< / head >
< body bgcolor = "white" text = "black" link = "#0000FF" vlink = "#840084" alink = "#0000FF" >
2008-02-14 17:43:52 +00:00
< table cellpadding = "2" width = "100%" > < tr >
< td valign = "top" > < img alt = "Boost C++ Libraries" width = "277" height = "86" src = "../../../../../boost.png" > < / td >
2009-02-12 14:01:48 +00:00
< td align = "center" > < a href = "../../../../../index.html" > Home< / a > < / td >
2011-04-06 20:54:18 +00:00
< td align = "center" > < a href = "../../../../../libs/libraries.htm" > Libraries< / a > < / td >
2009-02-12 14:01:48 +00:00
< 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 >
2008-02-14 17:43:52 +00:00
< td align = "center" > < a href = "../../../../../more/index.htm" > More< / a > < / td >
< / tr > < / table >
2007-05-29 06:40:25 +00:00
< hr >
< div class = "spirit-nav" >
2014-06-04 18:13:06 +02:00
< 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 >
2007-05-29 06:40:25 +00:00
< / div >
2014-04-11 23:36:24 +02:00
< div class = "section" >
2007-05-29 06:40:25 +00:00
< div class = "titlepage" > < div > < div > < h2 class = "title" style = "clear: both" >
2009-02-12 14:01:48 +00:00
< a name = "boost_optional.optional_references" > < / a > < a class = "link" href = "optional_references.html" title = "Optional references" > Optional references< / a >
2007-05-29 06:40:25 +00:00
< / h2 > < / div > < / div > < / div >
< p >
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 >
< p >
However, since references are not real objects some restrictions apply and
some operations are not available in this case:
< / p >
2013-02-16 19:42:42 +00:00
< div class = "itemizedlist" > < ul class = "itemizedlist" style = "list-style-type: disc; " >
2009-11-20 10:26:23 +00:00
< li class = "listitem" >
2011-04-06 20:54:18 +00:00
Converting constructors
< / li >
2009-11-20 10:26:23 +00:00
< li class = "listitem" >
2011-04-06 20:54:18 +00:00
Converting assignment
< / li >
2009-11-20 10:26:23 +00:00
< li class = "listitem" >
2011-04-06 20:54:18 +00:00
InPlace construction
< / li >
2009-11-20 10:26:23 +00:00
< li class = "listitem" >
2011-04-06 20:54:18 +00:00
InPlace assignment
< / li >
2009-11-20 10:26:23 +00:00
< li class = "listitem" >
2011-04-06 20:54:18 +00:00
Value-access via pointer
< / li >
2007-05-29 06:40:25 +00:00
< / ul > < / div >
< p >
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 ocurr:
< / p >
2013-02-16 19:42:42 +00:00
< div class = "itemizedlist" > < ul class = "itemizedlist" style = "list-style-type: disc; " >
2009-11-20 10:26:23 +00:00
< li class = "listitem" >
2011-04-06 20:54:18 +00: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 >
2014-05-07 17:07:12 +02:00
will copy the references but all these references will nonetheless refer
2011-04-06 20:54:18 +00:00
to the same object.
< / li >
2009-11-20 10:26:23 +00:00
< li class = "listitem" >
2011-04-06 20:54:18 +00:00
Value-access will actually provide access to the referenced object rather
than the reference itself.
< / li >
2007-05-29 06:40:25 +00:00
< / ul > < / div >
2014-05-07 17:07:12 +02:00
< div class = "warning" > < table border = "0" summary = "Warning" >
< tr >
< td rowspan = "2" align = "center" valign = "top" width = "25" > < img alt = "[Warning]" src = "../../../../../doc/src/images/warning.png" > < / td >
< th align = "left" > Warning< / th >
< / tr >
< tr > < td align = "left" valign = "top" > < p >
On compilers that do not conform to Standard C++ rules of reference binding,
operations on optional references might give adverse results: rather than
2014-05-08 21:50:41 +02:00
binding a reference to a designated object they may create an unexpected
2014-06-04 18:13:06 +02:00
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
2014-05-08 21:50:41 +02:00
and Portability section< / a > .
2014-05-07 17:07:12 +02:00
< / p > < / td > < / tr >
< / table > < / div >
2014-04-29 01:06:14 +02:00
< h4 >
< a name = "boost_optional.optional_references.h0" > < / a >
< 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
references< / a >
< / h4 >
< p >
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 >
< 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 >
2007-05-29 06:40:25 +00:00
< / div >
< table xmlns:rev = "http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width = "100%" > < tr >
< td align = "left" > < / td >
2014-05-05 19:08:11 +02:00
< td align = "right" > < div class = "copyright-footer" > Copyright © 2003-2007 Fernando Luis Cacciola Carballal< br > Copyright © 2014 Andrzej Krzemień ski< p >
2008-02-14 17:43:52 +00: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 >
2007-05-29 06:40:25 +00:00
< / tr > < / table >
< hr >
< div class = "spirit-nav" >
2014-06-04 18:13:06 +02:00
< 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 >
2007-05-29 06:40:25 +00:00
< / div >
< / body >
< / html >