]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/optional/doc/html/boost_optional/tutorial/type_requirements.html
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / optional / doc / html / boost_optional / tutorial / type_requirements.html
CommitLineData
7c673cae
FG
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4<title>Type requirements</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="../../optional/tutorial.html" title="Tutorial">
9<link rel="prev" href="exception_safety_guarantees.html" title="Exception Safety Guarantees">
10<link rel="next" href="performance_considerations.html" title="Performance considerations">
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="exception_safety_guarantees.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="performance_considerations.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h3 class="title">
27<a name="boost_optional.tutorial.type_requirements"></a><a class="link" href="type_requirements.html" title="Type requirements">Type requirements</a>
28</h3></div></div></div>
29<p>
30 The very minimum requirement of <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
31 is that <code class="computeroutput"><span class="identifier">T</span></code> is a complete type
32 and that it has a publicly accessible destructor. <code class="computeroutput"><span class="identifier">T</span></code>
33 doesn't even need to be constructible. You can use a very minimum interface:
34 </p>
35<pre class="programlisting"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">o</span><span class="special">;</span> <span class="comment">// uninitialized</span>
36<span class="identifier">assert</span><span class="special">(</span><span class="identifier">o</span> <span class="special">==</span> <span class="identifier">none</span><span class="special">);</span> <span class="comment">// check if initialized</span>
37<span class="identifier">assert</span><span class="special">(!</span><span class="identifier">o</span><span class="special">);</span> <span class="comment">//</span>
38<span class="identifier">o</span><span class="special">.</span><span class="identifier">value</span><span class="special">();</span> <span class="comment">// always throws</span>
39</pre>
40<p>
41 But this is practically useless. In order for <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
42 to be able to do anything useful and offer all the spectrum of ways of accessing
43 the contained value, <code class="computeroutput"><span class="identifier">T</span></code> needs
44 to have at least one accessible constructor. In that case you need to initialize
45 the optional object with function <code class="computeroutput"><span class="identifier">emplace</span><span class="special">()</span></code>, or if your compiler does not support it,
46 resort to <a class="link" href="in_place_factories.html" title="In-Place Factories">In-Place
47 Factories</a>:
48 </p>
49<pre class="programlisting"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">o</span><span class="special">;</span>
50<span class="identifier">o</span><span class="special">.</span><span class="identifier">emplace</span><span class="special">(</span><span class="string">"T"</span><span class="special">,</span> <span class="string">"ctor"</span><span class="special">,</span> <span class="string">"params"</span><span class="special">);</span>
51</pre>
52<p>
53 If <code class="computeroutput"><span class="identifier">T</span></code> is <code class="computeroutput"><span class="identifier">MoveConstructible</span></code>,
54 <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> is
55 also <code class="computeroutput"><span class="identifier">MoveConstructible</span></code> and
56 can be easily initialized from an rvalue of type <code class="computeroutput"><span class="identifier">T</span></code>
57 and be passed by value:
58 </p>
59<pre class="programlisting"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">o</span> <span class="special">=</span> <span class="identifier">make_T</span><span class="special">();</span>
60<span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;();</span>
61</pre>
62<p>
63 If <code class="computeroutput"><span class="identifier">T</span></code> is <a href="../../../../../utility/CopyConstructible.html" target="_top"><code class="computeroutput"><span class="identifier">CopyConstructible</span></code></a>, <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> is
64 also <a href="../../../../../utility/CopyConstructible.html" target="_top"><code class="computeroutput"><span class="identifier">CopyConstructible</span></code></a>
65 and can be easily initialized from an lvalue of type <code class="computeroutput"><span class="identifier">T</span></code>:
66 </p>
67<pre class="programlisting"><span class="identifier">T</span> <span class="identifier">v</span> <span class="special">=</span> <span class="identifier">make_T</span><span class="special">();</span>
68<span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">o</span> <span class="special">=</span> <span class="identifier">v</span><span class="special">;</span>
69<span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">p</span> <span class="special">=</span> <span class="identifier">o</span><span class="special">;</span>
70</pre>
71<p>
72 If <code class="computeroutput"><span class="identifier">T</span></code> is not <code class="computeroutput"><span class="identifier">MoveAssignable</span></code>, it is still possible to
73 reset the value of <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
74 using function <code class="computeroutput"><span class="identifier">emplace</span><span class="special">()</span></code>:
75 </p>
76<pre class="programlisting"><span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="identifier">o</span> <span class="special">=</span> <span class="identifier">make_T</span><span class="special">();</span>
77<span class="identifier">o</span><span class="special">.</span><span class="identifier">emplace</span><span class="special">(</span><span class="identifier">make_another_T</span><span class="special">());</span>
78</pre>
79<p>
80 If <code class="computeroutput"><span class="identifier">T</span></code> is <code class="computeroutput"><span class="identifier">Moveable</span></code>
81 (both <code class="computeroutput"><span class="identifier">MoveConstructible</span></code> and
82 <code class="computeroutput"><span class="identifier">MoveAssignable</span></code>) then <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code> is
83 also <code class="computeroutput"><span class="identifier">Moveable</span></code> and additionally
84 can be constructed and assigned from an rvalue of type <code class="computeroutput"><span class="identifier">T</span></code>.
85 </p>
86<p>
87 Similarly, if <code class="computeroutput"><span class="identifier">T</span></code> is <code class="computeroutput"><span class="identifier">Copyable</span></code> (both <a href="../../../../../utility/CopyConstructible.html" target="_top"><code class="computeroutput"><span class="identifier">CopyConstructible</span></code></a> and <code class="computeroutput"><span class="identifier">CopyAssignable</span></code>) then <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
88 is also <code class="computeroutput"><span class="identifier">Copyable</span></code> and additionally
89 can be constructed and assigned from an lvalue of type <code class="computeroutput"><span class="identifier">T</span></code>.
90 </p>
91<p>
92 <code class="computeroutput"><span class="identifier">T</span></code> <span class="emphasis"><em>is not</em></span>
93 required to be <a href="http://www.sgi.com/tech/stl/DefaultConstructible.html" target="_top"><code class="computeroutput"><span class="identifier">DefaultConstructible</span></code></a>.
94 </p>
95</div>
96<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
97<td align="left"></td>
98<td align="right"><div class="copyright-footer">Copyright &#169; 2003-2007 Fernando Luis Cacciola Carballal<br>Copyright &#169; 2014-2016 Andrzej Krzemie&#324;ski<p>
99 Distributed under the Boost Software License, Version 1.0. (See accompanying
100 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>)
101 </p>
102</div></td>
103</tr></table>
104<hr>
105<div class="spirit-nav">
106<a accesskey="p" href="exception_safety_guarantees.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="performance_considerations.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
107</div>
108</body>
109</html>