1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 3<title>No-value policies - Boost.Outcome documentation</title> 4<link rel="stylesheet" href="../../css/boost.css" type="text/css"> 5<meta name="generator" content="Hugo 0.52 with Boostdoc theme"> 6<meta name="viewport" content="width=device-width,initial-scale=1.0"/> 7 8<link rel="icon" href="../../images/favicon.ico" type="image/ico"/> 9<body><div class="spirit-nav"> 10<a accesskey="p" href="../../tutorial/essential/outcome/inspecting.html"><img src="../../images/prev.png" alt="Prev"></a> 11 <a accesskey="u" href="../../tutorial/essential.html"><img src="../../images/up.png" alt="Up"></a> 12 <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"> 13 14 <div class="titlepage"><div><div><h1 style="clear: both">No-value policies</h1></div></div></div> 15<p>In the previous section we have seen that it would be useful if calling member 16function <code>.value()</code> on object of type <code>outcome<T></code> that did not contain a value, 17would cause an exception to be thrown according to some user-defined policy.</p> 18 19<p>Let us consider <code>result<T></code> first. It is an alias to <a href="../../reference/types/basic_result.html" class="api-reference"><code>basic_result<T, E, NoValuePolicy></code></a> 20, 21where <code>E</code> is the type storing error information and defaulted to 22<code>std::error_code</code>/<code>boost::system::error_code</code>, and <code>NoValuePolicy</code> 23is a <em>no-value policy</em> defaulted to <a href="../../reference/aliases/default_policy.html" class="api-reference"><code>default_policy<T, EC, EP></code></a> 24.</p> 25 26<p>The semantics of <code>basic_result::value()</code> are:</p> 27 28<ol> 29<li>Calls <code>NoValuePolicy::wide_value_check(*this)</code>.</li> 30<li>Return a reference to the contained value. If no value is actually stored, 31your program has entered undefined behaviour.</li> 32</ol> 33 34<p>Thus, the semantics of function <code>.value()</code> depend on the no-value policy. The 35default policy (<code>policy::default_policy<T, EC, void></code>) for <code>EC</code> of type 36<code>std::error_code</code><sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup> does the following:</p> 37 38<ul> 39<li>If <code>r.has_value() == false</code> throws exception <code>std::system_error{r.error()}</code>,</li> 40<li>otherwise no effect.</li> 41</ul> 42 43<div class="notices note" style="background: url('../../images/note.png') top left no-repeat padding-box padding-box;"> 44<div class="notices heading">note</div> 45<div class="notices message"><p>Class templates <a href="../../reference/types/basic_result.html" class="api-reference"><code>basic_result<T, E, NoValuePolicy></code></a> 46 and <a href="../../reference/types/basic_outcome.html" class="api-reference"><code>basic_outcome<T, EC, EP, NoValuePolicy></code></a></p> 47 48<p>never use exceptions. Any exception-related logic is provided exclusively 49through no-value policies.</p> 50</div> 51</div> 52 53 54<p>When designing your own success-or-failure type using templates 55<code>basic_result<></code> or <code>basic_outcome<></code> you have to decide what no-value policy 56you want to use. Either create your own, or <a href="../../tutorial/essential/no-value/builtin.html">use one of the predefined policies</a>.</p> 57 58<p>You can also use one of the two other predefined aliases for <code>basic_result<></code>:</p> 59 60<ul> 61<li><a href="../../reference/aliases/unchecked.html" class="api-reference"><code>unchecked<T, E = varies></code></a> 62: it uses policy <a href="../../reference/policies/all_narrow.html" class="api-reference"><code>all_narrow</code></a> 63, where any observation of a missing value or error is undefined behavior;</li> 64<li><a href="../../reference/aliases/checked.html" class="api-reference"><code>checked<T, E = varies></code></a> 65: 66it uses policy <a href="../../reference/policies/throw_bad_result_access.html" class="api-reference"><code>throw_bad_result_access<EC></code></a> 67, 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<EC></code></a> 68 or <a href="../../reference/types/bad_result_access.html" class="api-reference"><code>bad_result_access</code></a> 69 70respectively.</li> 71</ul> 72<div class="footnotes"> 73 74<hr /> 75 76<ol> 77<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>. 78 <a class="footnote-return" href="#fnref:1"><sup>[return]</sup></a></li> 79</ol> 80</div> 81 82 83 84 </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p> 85<hr> 86<div class="spirit-nav"> 87<a accesskey="p" href="../../tutorial/essential/outcome/inspecting.html"><img src="../../images/prev.png" alt="Prev"></a> 88 <a accesskey="u" href="../../tutorial/essential.html"><img src="../../images/up.png" alt="Up"></a> 89 <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> 90</html> 91