• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
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.79.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">
11</head>
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>
20</tr></table>
21<hr>
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>
24</div>
25<div class="section">
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>
30<p>
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
34          example.
35        </p>
36<pre class="programlisting"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;&gt;</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">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="number">1</span><span class="special">)};</span>
37<span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">unique_ptr</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;&gt;</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>
40</pre>
41<p>
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">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
46          to be trivially copyable when <code class="computeroutput"><span class="identifier">T</span></code>
47          is trivial.
48        </p>
49</div>
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-2018 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>)
55      </p>
56</div></td>
57</tr></table>
58<hr>
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>
61</div>
62</body>
63</html>
64