3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>Moved-from optional
</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=
"../gotchas.html" title=
"Gotchas">
9 <link rel=
"prev" href=
"../gotchas.html" title=
"Gotchas">
10 <link rel=
"next" href=
"mixed_relational_comparisons.html" title=
"Mixed relational comparisons">
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=
"../gotchas.html"><img src=
"../../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../gotchas.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=
"mixed_relational_comparisons.html"><img src=
"../../../../../../../doc/src/images/next.png" alt=
"Next"></a>
26 <div class=
"titlepage"><div><div><h4 class=
"title">
27 <a name=
"boost_optional.tutorial.gotchas.moved_from__optional_"></a><a class=
"link" href=
"moved_from__optional_.html" title=
"Moved-from optional">Moved-from
28 <code class=
"computeroutput"><span class=
"identifier">optional
</span></code></a>
29 </h4></div></div></div>
31 When an optional object that contains a value is moved from (is a source
32 of move constructor or assignment) it still contains a value and its contained
33 value is left in a moved-from state. This can be illustrated with the following
36 <pre class=
"programlisting"><span class=
"identifier">optional
</span><span class=
"special"><</span><span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">unique_ptr
</span><span class=
"special"><</span><span class=
"keyword">int
</span><span class=
"special">>></span> <span class=
"identifier">opi
</span> <span class=
"special">{
</span><span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">make_unique
</span><span class=
"special"><</span><span class=
"keyword">int
</span><span class=
"special">>(
</span><span class=
"number">1</span><span class=
"special">)};
</span>
37 <span class=
"identifier">optional
</span><span class=
"special"><</span><span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">unique_ptr
</span><span class=
"special"><</span><span class=
"keyword">int
</span><span class=
"special">>></span> <span class=
"identifier">opj
</span> <span class=
"special">=
</span> <span class=
"identifier">std
</span><span class=
"special">::
</span><span class=
"identifier">move
</span><span class=
"special">(
</span><span class=
"identifier">opi
</span><span class=
"special">);
</span>
38 <span class=
"identifier">assert
</span> <span class=
"special">(
</span><span class=
"identifier">opi
</span><span class=
"special">);
</span>
39 <span class=
"identifier">assert
</span> <span class=
"special">(*
</span><span class=
"identifier">opi
</span> <span class=
"special">==
</span> <span class=
"keyword">nullptr
</span><span class=
"special">);
</span>
42 Quite a lot of people expect that when an object that contains a value
43 is moved from, its contained value should be destroyed. This is not so,
44 for performance reasons. Current semantics allow the implementation of
45 <code class=
"computeroutput"><span class=
"identifier">boost
</span><span class=
"special">::
</span><span class=
"identifier">opiotnal
</span><span class=
"special"><</span><span class=
"identifier">T
</span><span class=
"special">></span></code>
46 to be trivially copyable when
<code class=
"computeroutput"><span class=
"identifier">T
</span></code>
50 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
51 <td align=
"left"></td>
52 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2003-
2007 Fernando Luis Cacciola Carballal
<br>Copyright
© 2014-
2016 Andrzej Krzemie
ński
<p>
53 Distributed under the Boost Software License, Version
1.0. (See accompanying
54 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>)
59 <div class=
"spirit-nav">
60 <a accesskey=
"p" href=
"../gotchas.html"><img src=
"../../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../gotchas.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=
"mixed_relational_comparisons.html"><img src=
"../../../../../../../doc/src/images/next.png" alt=
"Next"></a>