summaryrefslogtreecommitdiff
path: root/libs/optional/doc/html/boost_optional/tutorial/when_to_use_optional.html
blob: fd27d802628dea4d103d1cd6781efccbaf569c07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>When to use Optional</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.78.1">
<link rel="home" href="../../index.html" title="Boost.Optional">
<link rel="up" href="../../optional/tutorial.html" title="Tutorial">
<link rel="prev" href="design_overview/the_interface.html" title="The Interface">
<link rel="next" href="relational_operators.html" title="Relational operators">
</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="design_overview/the_interface.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="relational_operators.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.when_to_use_optional"></a><a class="link" href="when_to_use_optional.html" title="When to use Optional">When to
      use Optional</a>
</h3></div></div></div>
<p>
        It is recommended to use <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
        in situations where there is exactly one, clear (to all parties) reason for
        having no value of type <code class="computeroutput"><span class="identifier">T</span></code>,
        and where the lack of value is as natural as having any regular value of
        <code class="computeroutput"><span class="identifier">T</span></code>. One example of such situation
        is asking the user in some GUI form to optionally specify some limit on an
        <code class="computeroutput"><span class="keyword">int</span></code> value, but the user is allowed
        to say 'I want the number not to be constrained by the maximum'. For another
        example, consider a config parameter specifying how many threads the application
        should launch. Leaving this parameter unspecified means that the application
        should decide itself. For yet another example, consider a function returning
        the index of the smallest element in a <code class="computeroutput"><span class="identifier">vector</span></code>.
        We need to be prepared for the situation, where the <code class="computeroutput"><span class="identifier">vector</span></code>
        is empty. Therefore a natural signature for such function would be:
      </p>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">&gt;</span>
<span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">size_t</span><span class="special">&gt;</span> <span class="identifier">find_smallest_elem</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;&amp;</span> <span class="identifier">vec</span><span class="special">);</span>
</pre>
<p>
        Here, having received an empty <code class="computeroutput"><span class="identifier">vec</span></code>
        and having no <code class="computeroutput"><span class="identifier">size_t</span></code> to return
        is not a <span class="emphasis"><em>failure</em></span> but a <span class="emphasis"><em>normal</em></span>,
        albeit irregular, situation.
      </p>
<p>
        Another typical situation is to indicate that we do not have a value yet,
        but we expect to have it later. This notion can be used in implementing solutions
        like lazy initialization or a two-phase initialization.
      </p>
<p>
        <code class="computeroutput"><span class="identifier">optional</span></code> can be used to take
        a non-<a href="http://www.sgi.com/tech/stl/DefaultConstructible.html" target="_top"><code class="computeroutput"><span class="identifier">DefaultConstructible</span></code></a> type <code class="computeroutput"><span class="identifier">T</span></code> and create a sibling type with a default
        constructor. This is a way to add a <span class="emphasis"><em>null-state</em></span> to any
        type that doesn't have it already.
      </p>
<p>
        Sometimes type <code class="computeroutput"><span class="identifier">T</span></code> already
        provides a built-in null-state, but it may still be useful to wrap it into
        <code class="computeroutput"><span class="identifier">optional</span></code>. Consider <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span></code>.
        When you read a piece of text from a GUI form or a DB table, it is hardly
        ever that the empty string indicates anything else but a missing text. And
        some data bases do not even distinguish between a null string entry and a
        non-null string of length 0. Still, it may be practical to use <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span></code>
        to indicate in the returned type that we want to treat the empty string in
        a special dedicated program path:
      </p>
<pre class="programlisting"><span class="keyword">if</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">&gt;</span> <span class="identifier">name</span> <span class="special">=</span> <span class="identifier">ask_user_name</span><span class="special">())</span> <span class="special">{</span>
    <span class="identifier">assert</span><span class="special">(*</span><span class="identifier">name</span> <span class="special">!=</span> <span class="string">""</span><span class="special">);</span>
    <span class="identifier">logon_as</span><span class="special">(*</span><span class="identifier">name</span><span class="special">);</span>
<span class="special">}</span>
<span class="keyword">else</span> <span class="special">{</span>
    <span class="identifier">skip_logon</span><span class="special">();</span>
<span class="special">}</span>
</pre>
<p>
        In the example above, the assertion indicates that if we choose to use this
        technique, we must translate the empty string state to an optional object
        with no contained value (inside function <code class="computeroutput"><span class="identifier">ask_user_name</span></code>).
      </p>
<h5>
<a name="boost_optional.tutorial.when_to_use_optional.h0"></a>
        <span class="phrase"><a name="boost_optional.tutorial.when_to_use_optional.not_recommended_usages"></a></span><a class="link" href="when_to_use_optional.html#boost_optional.tutorial.when_to_use_optional.not_recommended_usages">Not
        recommended usages</a>
      </h5>
<p>
        It is not recommended to use <code class="computeroutput"><span class="identifier">optional</span></code>
        to indicate that we were not able to compute a value because of a <span class="emphasis"><em>failure</em></span>.
        It is difficult to define what a failure is, but it usually has one common
        characteristic: an associated information on the cause of the failure. This
        can be the type and member data of an exception object, or an error code.
        It is a bad design to signal a failure and not inform about the cause. If
        you do not want to use exceptions, and do not like the fact that by returning
        error codes you cannot return the computed value, you can use <a href="https://github.com/ptal/Boost.Expected" target="_top">Expected</a>
        library. It is sort of <a href="../../../../../variant/index.html" target="_top">Boost.Variant</a>
        that contains either a computed value or a reason why the computation failed.
      </p>
<p>
        Sometimes the distinction into what is a failure and what is a valid but
        irregular result is blurry and depends on a particular usage and personal
        preference. Consider a function that converts a <code class="computeroutput"><span class="identifier">string</span></code>
        to an <code class="computeroutput"><span class="keyword">int</span></code>. Is it a failure that
        you cannot convert? It might in some cases, but in other you may call it
        exactly for the purpose of figuring out if a given <code class="computeroutput"><span class="identifier">string</span></code>
        is convertible, and you are not even interested in the resulting value. Sometimes
        when a conversion fails you may not consider it a failure, but you need to
        know why it cannot be converted; for instance at which character it is determined
        that the conversion is impossible. In this case returning <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;</span></code>
        will not suffice. Finally, there is a use case where an input string that
        does not represent an <code class="computeroutput"><span class="keyword">int</span></code> is
        not a failure condition, but during the conversion we use resources whose
        acquisition may fail. In that case the natural representation is to both
        return <code class="computeroutput"><span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span></code> and
        signal failure:
      </p>
<pre class="programlisting"><span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">convert1</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">string</span><span class="special">&amp;</span> <span class="identifier">str</span><span class="special">);</span> <span class="comment">// throws</span>
<span class="identifier">expected</span><span class="special">&lt;</span><span class="identifier">ErrorT</span><span class="special">,</span> <span class="identifier">optional</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;&gt;</span> <span class="identifier">convert2</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">string</span><span class="special">&amp;</span> <span class="identifier">str</span><span class="special">);</span> <span class="comment">// return either optional or error</span>
</pre>
</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 &#169; 2003-2007 Fernando Luis Cacciola Carballal<br>Copyright &#169; 2014, 2015 Andrzej Krzemie&#324;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="design_overview/the_interface.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="relational_operators.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>