outcome/doc/html/tutorial/essential/no-value.html
2019-12-01 13:32:32 +00:00

91 lines
5.4 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>No-value policies - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>
<link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../tutorial/essential/outcome/inspecting.html"><img src="../../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../tutorial/essential.html"><img src="../../images/up.png" alt="Up"></a>
<a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/essential/no-value/custom.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
<div class="titlepage"><div><div><h1 style="clear: both">No-value policies</h1></div></div></div>
<p>In the previous section we have seen that it would be useful if calling member
function <code>.value()</code> on object of type <code>outcome&lt;T&gt;</code> that did not contain a value,
would cause an exception to be thrown according to some user-defined policy.</p>
<p>Let us consider <code>result&lt;T&gt;</code> first. It is an alias to <a href="../../reference/types/basic_result.html" class="api-reference"><code>basic_result&lt;T, E, NoValuePolicy&gt;</code></a>
,
where <code>E</code> is the type storing error information and defaulted to
<code>std::error_code</code>/<code>boost::system::error_code</code>, and <code>NoValuePolicy</code>
is a <em>no-value policy</em> defaulted to <a href="../../reference/aliases/default_policy.html" class="api-reference"><code>default_policy&lt;T, EC, EP&gt;</code></a>
.</p>
<p>The semantics of <code>basic_result::value()</code> are:</p>
<ol>
<li>Calls <code>NoValuePolicy::wide_value_check(*this)</code>.</li>
<li>Return a reference to the contained value. If no value is actually stored,
your program has entered undefined behaviour.</li>
</ol>
<p>Thus, the semantics of function <code>.value()</code> depend on the no-value policy. The
default policy (<code>policy::default_policy&lt;T, EC, void&gt;</code>) for <code>EC</code> of type
<code>std::error_code</code><sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup> does the following:</p>
<ul>
<li>If <code>r.has_value() == false</code> throws exception <code>std::system_error{r.error()}</code>,</li>
<li>otherwise no effect.</li>
</ul>
<div class="notices note" style="background: url('../../images/note.png') top left no-repeat padding-box padding-box;">
<div class="notices heading">note</div>
<div class="notices message"><p>Class templates <a href="../../reference/types/basic_result.html" class="api-reference"><code>basic_result&lt;T, E, NoValuePolicy&gt;</code></a>
and <a href="../../reference/types/basic_outcome.html" class="api-reference"><code>basic_outcome&lt;T, EC, EP, NoValuePolicy&gt;</code></a></p>
<p>never use exceptions. Any exception-related logic is provided exclusively
through no-value policies.</p>
</div>
</div>
<p>When designing your own success-or-failure type using templates
<code>basic_result&lt;&gt;</code> or <code>basic_outcome&lt;&gt;</code> you have to decide what no-value policy
you want to use. Either create your own, or <a href="../../tutorial/essential/no-value/builtin.html">use one of the predefined policies</a>.</p>
<p>You can also use one of the two other predefined aliases for <code>basic_result&lt;&gt;</code>:</p>
<ul>
<li><a href="../../reference/aliases/unchecked.html" class="api-reference"><code>unchecked&lt;T, E = varies&gt;</code></a>
: it uses policy <a href="../../reference/policies/all_narrow.html" class="api-reference"><code>all_narrow</code></a>
, where any observation of a missing value or error is undefined behavior;</li>
<li><a href="../../reference/aliases/checked.html" class="api-reference"><code>checked&lt;T, E = varies&gt;</code></a>
:
it uses policy <a href="../../reference/policies/throw_bad_result_access.html" class="api-reference"><code>throw_bad_result_access&lt;EC&gt;</code></a>
, where any observation of a missing value or error throws <a href="../../reference/types/bad_result_access_with.html" class="api-reference"><code>bad_result_access_with&lt;EC&gt;</code></a>
or <a href="../../reference/types/bad_result_access.html" class="api-reference"><code>bad_result_access</code></a>
respectively.</li>
</ul>
<div class="footnotes">
<hr />
<ol>
<li id="fn:1">Similar overloads exist for throwing <code>boost::system::system_error</code> when <code>EC</code> is <code>boost::system::error_code</code>.
<a class="footnote-return" href="#fnref:1"><sup>[return]</sup></a></li>
</ol>
</div>
</div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../tutorial/essential/outcome/inspecting.html"><img src="../../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../tutorial/essential.html"><img src="../../images/up.png" alt="Up"></a>
<a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/essential/no-value/custom.html"><img src="../../images/next.png" alt="Next"></a></div></body>
</html>