123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <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.79.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"><</span><span class="identifier">T</span><span class="special">></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"><</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">></span>
- <span class="identifier">optional</span><span class="special"><</span><span class="identifier">size_t</span><span class="special">></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"><</span><span class="identifier">T</span><span class="special">>&</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"><</span><span class="identifier">string</span><span class="special">></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"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></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"><</span><span class="identifier">T</span><span class="special">></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"><</span><span class="keyword">int</span><span class="special">></span></code> and
- signal failure:
- </p>
- <pre class="programlisting"><span class="identifier">optional</span><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="identifier">convert1</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">string</span><span class="special">&</span> <span class="identifier">str</span><span class="special">);</span> <span class="comment">// throws</span>
- <span class="identifier">expected</span><span class="special"><</span><span class="identifier">ErrorT</span><span class="special">,</span> <span class="identifier">optional</span><span class="special"><</span><span class="keyword">int</span><span class="special">>></span> <span class="identifier">convert2</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">string</span><span class="special">&</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 © 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="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>
|