3 <meta http-equiv=
"Content-Type" content=
"text/html; charset=US-ASCII">
4 <title>The semantics
</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=
"../design_overview.html" title=
"Design Overview">
9 <link rel=
"prev" href=
"../design_overview.html" title=
"Design Overview">
10 <link rel=
"next" href=
"the_interface.html" title=
"The Interface">
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=
"../design_overview.html"><img src=
"../../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../design_overview.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=
"the_interface.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.design_overview.the_semantics"></a><a class=
"link" href=
"the_semantics.html" title=
"The semantics">The
29 </h4></div></div></div>
31 Objects of type
<code class=
"computeroutput"><span class=
"identifier">optional
</span><span class=
"special"><</span><span class=
"identifier">T
</span><span class=
"special">></span></code> are intended to be used in places where
32 objects of type
<code class=
"computeroutput"><span class=
"identifier">T
</span></code> would
33 but which might be uninitialized. Hence,
<code class=
"computeroutput"><span class=
"identifier">optional
</span><span class=
"special"><</span><span class=
"identifier">T
</span><span class=
"special">></span></code>'s purpose is to formalize the additional
34 possibly uninitialized state. From the perspective of this role,
<code class=
"computeroutput"><span class=
"identifier">optional
</span><span class=
"special"><</span><span class=
"identifier">T
</span><span class=
"special">></span></code>
35 can have the same operational semantics of
<code class=
"computeroutput"><span class=
"identifier">T
</span></code>
36 plus the additional semantics corresponding to this special state. As such,
37 <code class=
"computeroutput"><span class=
"identifier">optional
</span><span class=
"special"><</span><span class=
"identifier">T
</span><span class=
"special">></span></code>
38 could be thought of as a
<span class=
"emphasis"><em>supertype
</em></span> of
<code class=
"computeroutput"><span class=
"identifier">T
</span></code>. Of course, we can't do that in C++,
39 so we need to compose the desired semantics using a different mechanism.
40 Doing it the other way around, that is, making
<code class=
"computeroutput"><span class=
"identifier">optional
</span><span class=
"special"><</span><span class=
"identifier">T
</span><span class=
"special">></span></code> a
<span class=
"emphasis"><em>subtype
</em></span> of
41 <code class=
"computeroutput"><span class=
"identifier">T
</span></code> is not only conceptually
42 wrong but also impractical: it is not allowed to derive from a non-class
43 type, such as a built-in type.
46 We can draw from the purpose of
<code class=
"computeroutput"><span class=
"identifier">optional
</span><span class=
"special"><</span><span class=
"identifier">T
</span><span class=
"special">></span></code> the required basic semantics:
48 <div class=
"itemizedlist"><ul class=
"itemizedlist" style=
"list-style-type: disc; ">
50 <span class=
"bold"><strong>Default Construction:
</strong></span> To introduce
51 a formally uninitialized wrapped object.
54 <span class=
"bold"><strong>Direct Value Construction via copy:
</strong></span>
55 To introduce a formally initialized wrapped object whose value is obtained
56 as a copy of some object.
59 <span class=
"bold"><strong>Deep Copy Construction:
</strong></span> To obtain
60 a new yet equivalent wrapped object.
63 <span class=
"bold"><strong>Direct Value Assignment (upon initialized):
</strong></span>
64 To assign a value to the wrapped object.
67 <span class=
"bold"><strong>Direct Value Assignment (upon uninitialized):
</strong></span>
68 To initialize the wrapped object with a value obtained as a copy of
72 <span class=
"bold"><strong>Assignment (upon initialized):
</strong></span> To
73 assign to the wrapped object the value of another wrapped object.
76 <span class=
"bold"><strong>Assignment (upon uninitialized):
</strong></span> To
77 initialize the wrapped object with value of another wrapped object.
80 <span class=
"bold"><strong>Deep Relational Operations (when supported by
81 the type T):
</strong></span> To compare wrapped object values taking into
82 account the presence of uninitialized states.
85 <span class=
"bold"><strong>Value access:
</strong></span> To unwrap the wrapped
89 <span class=
"bold"><strong>Initialization state query:
</strong></span> To determine
90 if the object is formally initialized or not.
93 <span class=
"bold"><strong>Swap:
</strong></span> To exchange wrapped objects.
94 (with whatever exception safety guarantees are provided by
<code class=
"computeroutput"><span class=
"identifier">T
</span></code>'s swap).
97 <span class=
"bold"><strong>De-initialization:
</strong></span> To release the
98 wrapped object (if any) and leave the wrapper in the uninitialized
103 Additional operations are useful, such as converting constructors and converting
104 assignments, in-place construction and assignment, and safe value access
105 via a pointer to the wrapped object or null.
108 <table xmlns:
rev=
"http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width=
"100%"><tr>
109 <td align=
"left"></td>
110 <td align=
"right"><div class=
"copyright-footer">Copyright
© 2003-
2007 Fernando Luis Cacciola Carballal
<br>Copyright
© 2014-
2016 Andrzej Krzemie
ński
<p>
111 Distributed under the Boost Software License, Version
1.0. (See accompanying
112 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>)
117 <div class=
"spirit-nav">
118 <a accesskey=
"p" href=
"../design_overview.html"><img src=
"../../../../../../../doc/src/images/prev.png" alt=
"Prev"></a><a accesskey=
"u" href=
"../design_overview.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=
"the_interface.html"><img src=
"../../../../../../../doc/src/images/next.png" alt=
"Next"></a>