185 lines
13 KiB
HTML
185 lines
13 KiB
HTML
<html>
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
|
|
<title>Design Overview</title>
|
|
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
|
|
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
|
|
<link rel="home" href="../../index.html" title="Boost.Optional">
|
|
<link rel="up" href="../../optional/tutorial.html" title="Tutorial">
|
|
<link rel="prev" href="../../optional/tutorial.html" title="Tutorial">
|
|
<link rel="next" href="design_overview/the_semantics.html" title="The semantics">
|
|
</head>
|
|
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
|
<table cellpadding="2" width="100%"><tr>
|
|
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
|
|
<td align="center"><a href="../../../../../../index.html">Home</a></td>
|
|
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
|
|
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
|
|
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="../../optional/tutorial.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="design_overview/the_semantics.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.design_overview"></a><a class="link" href="design_overview.html" title="Design Overview">Design Overview</a>
|
|
</h3></div></div></div>
|
|
<div class="section">
|
|
<div class="titlepage"><div><div><h4 class="title">
|
|
<a name="boost_optional.tutorial.design_overview.the_models"></a><a class="link" href="design_overview.html#boost_optional.tutorial.design_overview.the_models" title="The models">The
|
|
models</a>
|
|
</h4></div></div></div>
|
|
<p>
|
|
In C++, we can <span class="emphasis"><em>declare</em></span> an object (a variable) of type
|
|
<code class="computeroutput"><span class="identifier">T</span></code>, and we can give this
|
|
variable an <span class="emphasis"><em>initial value</em></span> (through an <span class="emphasis"><em>initializer</em></span>.
|
|
(cf. 8.5)). When a declaration includes a non-empty initializer (an initial
|
|
value is given), it is said that the object has been initialized. If the
|
|
declaration uses an empty initializer (no initial value is given), and
|
|
neither default nor value initialization applies, it is said that the object
|
|
is <span class="bold"><strong>uninitialized</strong></span>. Its actual value exist
|
|
but has an <span class="emphasis"><em>indeterminate initial value</em></span> (cf. 8.5/11).
|
|
<code class="computeroutput"><span class="identifier">optional</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code>
|
|
intends to formalize the notion of initialization (or lack of it) allowing
|
|
a program to test whether an object has been initialized and stating that
|
|
access to the value of an uninitialized object is undefined behavior. That
|
|
is, when a variable is declared as <code class="computeroutput"><span class="identifier">optional</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code> and no initial value is given, the
|
|
variable is <span class="emphasis"><em>formally</em></span> uninitialized. A formally uninitialized
|
|
optional object has conceptually no value at all and this situation can
|
|
be tested at runtime. It is formally <span class="emphasis"><em>undefined behavior</em></span>
|
|
to try to access the value of an uninitialized optional. An uninitialized
|
|
optional can be assigned a value, in which case its initialization state
|
|
changes to initialized. Furthermore, given the formal treatment of initialization
|
|
states in optional objects, it is even possible to reset an optional to
|
|
<span class="emphasis"><em>uninitialized</em></span>.
|
|
</p>
|
|
<p>
|
|
In C++ there is no formal notion of uninitialized objects, which means
|
|
that objects always have an initial value even if indeterminate. As discussed
|
|
on the previous section, this has a drawback because you need additional
|
|
information to tell if an object has been effectively initialized. One
|
|
of the typical ways in which this has been historically dealt with is via
|
|
a special value: <code class="computeroutput"><span class="identifier">EOF</span></code>,
|
|
<code class="computeroutput"><span class="identifier">npos</span></code>, -1, etc... This is
|
|
equivalent to adding the special value to the set of possible values of
|
|
a given type. This super set of <code class="computeroutput"><span class="identifier">T</span></code>
|
|
plus some <span class="emphasis"><em>nil_t</em></span>—where <code class="computeroutput"><span class="identifier">nil_t</span></code>
|
|
is some stateless POD—can be modeled in modern languages as a <span class="bold"><strong>discriminated union</strong></span> of T and nil_t. Discriminated
|
|
unions are often called <span class="emphasis"><em>variants</em></span>. A variant has a
|
|
<span class="emphasis"><em>current type</em></span>, which in our case is either <code class="computeroutput"><span class="identifier">T</span></code> or <code class="computeroutput"><span class="identifier">nil_t</span></code>.
|
|
Using the <a href="../../../../../variant/index.html" target="_top">Boost.Variant</a>
|
|
library, this model can be implemented in terms of <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">variant</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span><span class="identifier">nil_t</span><span class="special">></span></code>. There is precedent for a discriminated
|
|
union as a model for an optional value: the <a href="http://www.haskell.org/" target="_top">Haskell</a>
|
|
<span class="bold"><strong>Maybe</strong></span> built-in type constructor. Thus,
|
|
a discriminated union <code class="computeroutput"><span class="identifier">T</span><span class="special">+</span><span class="identifier">nil_t</span></code>
|
|
serves as a conceptual foundation.
|
|
</p>
|
|
<p>
|
|
A <code class="computeroutput"><span class="identifier">variant</span><span class="special"><</span><span class="identifier">T</span><span class="special">,</span><span class="identifier">nil_t</span><span class="special">></span></code> follows naturally from the traditional
|
|
idiom of extending the range of possible values adding an additional sentinel
|
|
value with the special meaning of <span class="emphasis"><em>Nothing</em></span>. However,
|
|
this additional <span class="emphasis"><em>Nothing</em></span> value is largely irrelevant
|
|
for our purpose since our goal is to formalize the notion of uninitialized
|
|
objects and, while a special extended value can be used to convey that
|
|
meaning, it is not strictly necessary in order to do so.
|
|
</p>
|
|
<p>
|
|
The observation made in the last paragraph about the irrelevant nature
|
|
of the additional <code class="computeroutput"><span class="identifier">nil_t</span></code>
|
|
with respect to <span class="underline">purpose</span> of <code class="computeroutput"><span class="identifier">optional</span><span class="special"><</span><span class="identifier">T</span><span class="special">></span></code>
|
|
suggests an alternative model: a <span class="emphasis"><em>container</em></span> that either
|
|
has a value of <code class="computeroutput"><span class="identifier">T</span></code> or nothing.
|
|
</p>
|
|
<p>
|
|
As of this writing I don't know of any precedent for a variable-size fixed-capacity
|
|
(of 1) stack-based container model for optional values, yet I believe this
|
|
is the consequence of the lack of practical implementations of such a container
|
|
rather than an inherent shortcoming of the container model.
|
|
</p>
|
|
<p>
|
|
In any event, both the discriminated-union or the single-element container
|
|
models serve as a conceptual ground for a class representing optional—i.e.
|
|
possibly uninitialized—objects. For instance, these models show the
|
|
<span class="emphasis"><em>exact</em></span> semantics required for a wrapper of optional
|
|
values:
|
|
</p>
|
|
<p>
|
|
Discriminated-union:
|
|
</p>
|
|
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
|
<li class="listitem">
|
|
<span class="bold"><strong>deep-copy</strong></span> semantics: copies of the
|
|
variant implies copies of the value.
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="bold"><strong>deep-relational</strong></span> semantics: comparisons
|
|
between variants matches both current types and values
|
|
</li>
|
|
<li class="listitem">
|
|
If the variant's current type is <code class="computeroutput"><span class="identifier">T</span></code>,
|
|
it is modeling an <span class="emphasis"><em>initialized</em></span> optional.
|
|
</li>
|
|
<li class="listitem">
|
|
If the variant's current type is not <code class="computeroutput"><span class="identifier">T</span></code>,
|
|
it is modeling an <span class="emphasis"><em>uninitialized</em></span> optional.
|
|
</li>
|
|
<li class="listitem">
|
|
Testing if the variant's current type is <code class="computeroutput"><span class="identifier">T</span></code>
|
|
models testing if the optional is initialized
|
|
</li>
|
|
<li class="listitem">
|
|
Trying to extract a <code class="computeroutput"><span class="identifier">T</span></code>
|
|
from a variant when its current type is not <code class="computeroutput"><span class="identifier">T</span></code>,
|
|
models the undefined behavior of trying to access the value of an uninitialized
|
|
optional
|
|
</li>
|
|
</ul></div>
|
|
<p>
|
|
Single-element container:
|
|
</p>
|
|
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
|
|
<li class="listitem">
|
|
<span class="bold"><strong>deep-copy</strong></span> semantics: copies of the
|
|
container implies copies of the value.
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="bold"><strong>deep-relational</strong></span> semantics: comparisons
|
|
between containers compare container size and if match, contained value
|
|
</li>
|
|
<li class="listitem">
|
|
If the container is not empty (contains an object of type <code class="computeroutput"><span class="identifier">T</span></code>), it is modeling an <span class="emphasis"><em>initialized</em></span>
|
|
optional.
|
|
</li>
|
|
<li class="listitem">
|
|
If the container is empty, it is modeling an <span class="emphasis"><em>uninitialized</em></span>
|
|
optional.
|
|
</li>
|
|
<li class="listitem">
|
|
Testing if the container is empty models testing if the optional is
|
|
initialized
|
|
</li>
|
|
<li class="listitem">
|
|
Trying to extract a <code class="computeroutput"><span class="identifier">T</span></code>
|
|
from an empty container models the undefined behavior of trying to
|
|
access the value of an uninitialized optional
|
|
</li>
|
|
</ul></div>
|
|
</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-2018 Andrzej Krzemieński<p>
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
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>)
|
|
</p>
|
|
</div></td>
|
|
</tr></table>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="../../optional/tutorial.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="design_overview/the_semantics.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
|
|
</div>
|
|
</body>
|
|
</html>
|