mirror of
https://github.com/boostorg/optional.git
synced 2025-07-16 05:42:07 +02:00
Fix: prevented the binding illegal temporary to optional<const int&>
Older MSVC versions add illegal temporary when you want to assign from const integral value.
This commit is contained in:
@ -28,24 +28,30 @@
|
||||
Reference Binding</a>
|
||||
</h3></div></div></div>
|
||||
<p>
|
||||
On compilers that do not conform to Standard C++ rules of reference binding,
|
||||
operations on optional references might give adverse results: rather than
|
||||
binding a reference to a designated object they may create an unexpected
|
||||
temporary and bind to it. Compilers known to have these deficiencies include
|
||||
GCC versions 4.2, 4.3, 4.4, 4.5; QCC 4.4.2; MSVC versions 8.0, 9.0, 10.0,
|
||||
11.0, 12.0. On these compilers prefer using direct-initialization and copy
|
||||
assignment of optional references to copy-initialization and assignment from
|
||||
<code class="computeroutput"><span class="identifier">T</span><span class="special">&</span></code>:
|
||||
A number of compilers incorrectly treat const lvalues of integral type as
|
||||
rvalues, and create an illegal temporary when binding to an lvalue reference
|
||||
to const in some expressions. This could result in creating an optional lvalue
|
||||
reference that is in fact bound to an unexpected temporary rather than to
|
||||
the intended object. In order to prevent hard to find run-time bugs, this
|
||||
library performs compile-time checks to prevent expressions that would otherwise
|
||||
bind an optional reference to an unexpected temporary. As a consequence,
|
||||
on certain compilers certain pieces of functionality in optional references
|
||||
are missing. In order to maintain a portability of your code across diferent
|
||||
compilers, it is recommended that you only stick to the minimum portable
|
||||
interface of optional references: prefer direct-initialization and copy assignment
|
||||
of optional references to copy-initialization and assignment from <code class="computeroutput"><span class="identifier">T</span><span class="special">&</span></code>:
|
||||
</p>
|
||||
<pre class="programlisting"><span class="keyword">const</span> <span class="keyword">int</span> <span class="identifier">i</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</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">or1</span><span class="special">;</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">or2</span> <span class="special">=</span> <span class="identifier">i</span><span class="special">;</span> <span class="comment">// not portable</span>
|
||||
<span class="identifier">or1</span> <span class="special">=</span> <span class="identifier">i</span><span class="special">;</span> <span class="comment">// not portable</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">or2</span> <span class="special">=</span> <span class="identifier">i</span><span class="special">;</span> <span class="comment">// caution: not portable</span>
|
||||
<span class="identifier">or1</span> <span class="special">=</span> <span class="identifier">i</span><span class="special">;</span> <span class="comment">// caution: not portable</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">or3</span><span class="special">(</span><span class="identifier">i</span><span class="special">);</span> <span class="comment">// portable</span>
|
||||
<span class="identifier">or1</span> <span class="special">=</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">i</span><span class="special">);</span> <span class="comment">// portable</span>
|
||||
</pre>
|
||||
<p>
|
||||
Compilers known to have these deficiencies include GCC versions 4.2, 4.3,
|
||||
4.4, 4.5, 5.1, 5.2; QCC 4.4.2; MSVC versions 8.0, 9.0, 10.0, 11.0, 12.0.
|
||||
In order to check if your compiler correctly implements reference binding
|
||||
use this test program.
|
||||
</p>
|
||||
|
@ -189,7 +189,7 @@
|
||||
</li>
|
||||
<li class="listitem">
|
||||
<span class="bold"><strong>Notes:</strong></span> This behaviour is called <span class="emphasis"><em>rebinding
|
||||
semantics</em></span>. See <a class="link" href="../../tutorial/rebinding_semantics_for_assignment_of_optional_references.html" title="Rebinding semantics for assignment of optional references">here</a>
|
||||
semantics</em></span>. See <a class="link" href="../../tutorial/optional_references/rebinding_semantics_for_assignment_of_optional_references.html" title="Rebinding semantics for assignment of optional references">here</a>
|
||||
for details.
|
||||
</li>
|
||||
<li class="listitem">
|
||||
|
@ -43,6 +43,10 @@
|
||||
<li class="listitem">
|
||||
some bugs connected to copying optional references are gone,
|
||||
</li>
|
||||
<li class="listitem">
|
||||
all run-time bugs caused by incorrect reference binding on some compilers
|
||||
are now turned into compile-time errors,
|
||||
</li>
|
||||
<li class="listitem">
|
||||
you can swap optional references: it is like swapping pointers: shalow,
|
||||
underlying objects are not affected,
|
||||
|
@ -142,10 +142,10 @@
|
||||
about the possibly uninitialized state appealing to the familiar pointer
|
||||
semantics w.r.t. to null pointers.
|
||||
</p>
|
||||
<div class="warning"><table border="0" summary="Warning">
|
||||
<div class="caution"><table border="0" summary="Caution">
|
||||
<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>
|
||||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../../doc/src/images/caution.png"></td>
|
||||
<th align="left">Caution</th>
|
||||
</tr>
|
||||
<tr><td align="left" valign="top"><p>
|
||||
However, it is particularly important to note that <code class="computeroutput"><span class="identifier">optional</span><span class="special"><></span></code> objects are not pointers. <span class="underline"><code class="computeroutput"><span class="identifier">optional</span><span class="special"><></span></code> is not, and does not model, a
|
||||
|
@ -6,7 +6,7 @@
|
||||
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
|
||||
<link rel="home" href="../../index.html" title="Boost.Optional">
|
||||
<link rel="up" href="../../optional/tutorial.html" title="Tutorial">
|
||||
<link rel="prev" href="rebinding_semantics_for_assignment_of_optional_references.html" title="Rebinding semantics for assignment of optional references">
|
||||
<link rel="prev" href="optional_references/rebinding_semantics_for_assignment_of_optional_references.html" title="Rebinding semantics for assignment of optional references">
|
||||
<link rel="next" href="a_note_about_optional_bool_.html" title="A note about optional<bool>">
|
||||
</head>
|
||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||||
@ -20,7 +20,7 @@
|
||||
</tr></table>
|
||||
<hr>
|
||||
<div class="spirit-nav">
|
||||
<a accesskey="p" href="rebinding_semantics_for_assignment_of_optional_references.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="a_note_about_optional_bool_.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
||||
<a accesskey="p" href="optional_references/rebinding_semantics_for_assignment_of_optional_references.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="a_note_about_optional_bool_.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
||||
</div>
|
||||
<div class="section">
|
||||
<div class="titlepage"><div><div><h3 class="title">
|
||||
@ -191,7 +191,7 @@
|
||||
</tr></table>
|
||||
<hr>
|
||||
<div class="spirit-nav">
|
||||
<a accesskey="p" href="rebinding_semantics_for_assignment_of_optional_references.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="a_note_about_optional_bool_.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
||||
<a accesskey="p" href="optional_references/rebinding_semantics_for_assignment_of_optional_references.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="a_note_about_optional_bool_.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -7,7 +7,7 @@
|
||||
<link rel="home" href="../../index.html" title="Boost.Optional">
|
||||
<link rel="up" href="../../optional/tutorial.html" title="Tutorial">
|
||||
<link rel="prev" href="io_operators.html" title="IO operators">
|
||||
<link rel="next" href="rebinding_semantics_for_assignment_of_optional_references.html" title="Rebinding semantics for assignment of optional references">
|
||||
<link rel="next" href="optional_references/rebinding_semantics_for_assignment_of_optional_references.html" title="Rebinding semantics for assignment of optional references">
|
||||
</head>
|
||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||||
<table cellpadding="2" width="100%"><tr>
|
||||
@ -20,85 +20,87 @@
|
||||
</tr></table>
|
||||
<hr>
|
||||
<div class="spirit-nav">
|
||||
<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="rebinding_semantics_for_assignment_of_optional_references.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
||||
<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>
|
||||
</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>
|
||||
<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>
|
||||
<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>
|
||||
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>
|
||||
However, since references are not real objects some restrictions apply
|
||||
and some operations are not available in this case:
|
||||
</p>
|
||||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||||
<li class="listitem">
|
||||
Converting constructors
|
||||
</li>
|
||||
Converting constructors
|
||||
</li>
|
||||
<li class="listitem">
|
||||
Converting assignment
|
||||
</li>
|
||||
Converting assignment
|
||||
</li>
|
||||
<li class="listitem">
|
||||
InPlace construction
|
||||
</li>
|
||||
InPlace construction
|
||||
</li>
|
||||
<li class="listitem">
|
||||
InPlace assignment
|
||||
</li>
|
||||
InPlace assignment
|
||||
</li>
|
||||
<li class="listitem">
|
||||
Value-access via pointer
|
||||
</li>
|
||||
Value-access via pointer
|
||||
</li>
|
||||
</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>
|
||||
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>
|
||||
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
||||
<li class="listitem">
|
||||
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>
|
||||
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>
|
||||
<li class="listitem">
|
||||
Value-access will actually provide access to the referenced object rather
|
||||
than the reference itself.
|
||||
</li>
|
||||
Value-access will actually provide access to the referenced object
|
||||
rather than the reference itself.
|
||||
</li>
|
||||
</ul></div>
|
||||
<div class="warning"><table border="0" summary="Warning">
|
||||
<div class="caution"><table border="0" summary="Caution">
|
||||
<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>
|
||||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../../../doc/src/images/caution.png"></td>
|
||||
<th align="left">Caution</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
|
||||
binding a reference to a designated object they may create an unexpected
|
||||
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
|
||||
and Portability section</a>.
|
||||
</p></td></tr>
|
||||
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>
|
||||
</table></div>
|
||||
<h5>
|
||||
<a name="boost_optional.tutorial.optional_references.h0"></a>
|
||||
<span class="phrase"><a name="boost_optional.tutorial.optional_references.rvalue_references"></a></span><a class="link" href="optional_references.html#boost_optional.tutorial.optional_references.rvalue_references">Rvalue
|
||||
references</a>
|
||||
</h5>
|
||||
<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>
|
||||
<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>
|
||||
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>
|
||||
</div>
|
||||
</div>
|
||||
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
|
||||
<td align="left"></td>
|
||||
<td align="right"><div class="copyright-footer">Copyright © 2003-2007 Fernando Luis Cacciola Carballal<br>Copyright © 2014-2016 Andrzej Krzemieński<p>
|
||||
@ -109,7 +111,7 @@
|
||||
</tr></table>
|
||||
<hr>
|
||||
<div class="spirit-nav">
|
||||
<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="rebinding_semantics_for_assignment_of_optional_references.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
||||
<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>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
Reference in New Issue
Block a user