no-value.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. <link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
  8. <body><div class="spirit-nav">
  9. <a accesskey="p" href="../../tutorial/essential/outcome/inspecting.html"><img src="../../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../../tutorial/essential.html"><img src="../../images/up.png" alt="Up"></a>
  11. <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">
  12. <div class="titlepage"><div><div><h1 style="clear: both">No-value policies</h1></div></div></div>
  13. <p>In the previous section we have seen that it would be useful if calling member
  14. function <code>.value()</code> on object of type <code>outcome&lt;T&gt;</code> that did not contain a value,
  15. would cause an exception to be thrown according to some user-defined policy.</p>
  16. <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>
  17. ,
  18. where <code>E</code> is the type storing error information and defaulted to
  19. <code>std::error_code</code>/<code>boost::system::error_code</code>, and <code>NoValuePolicy</code>
  20. 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>
  21. .</p>
  22. <p>The semantics of <code>basic_result::value()</code> are:</p>
  23. <ol>
  24. <li>Calls <code>NoValuePolicy::wide_value_check(*this)</code>.</li>
  25. <li>Return a reference to the contained value. If no value is actually stored,
  26. your program has entered undefined behaviour.</li>
  27. </ol>
  28. <p>Thus, the semantics of function <code>.value()</code> depend on the no-value policy. The
  29. default policy (<code>policy::default_policy&lt;T, EC, void&gt;</code>) for <code>EC</code> of type
  30. <code>std::error_code</code><sup class="footnote-ref" id="fnref:1"><a href="#fn:1">1</a></sup> does the following:</p>
  31. <ul>
  32. <li>If <code>r.has_value() == false</code> throws exception <code>std::system_error{r.error()}</code>,</li>
  33. <li>otherwise no effect.</li>
  34. </ul>
  35. <div class="notices note" style="background: url('../../images/note.png') top left no-repeat padding-box padding-box;">
  36. <div class="notices heading">note</div>
  37. <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>
  38. and <a href="../../reference/types/basic_outcome.html" class="api-reference"><code>basic_outcome&lt;T, EC, EP, NoValuePolicy&gt;</code></a></p>
  39. <p>never use exceptions. Any exception-related logic is provided exclusively
  40. through no-value policies.</p>
  41. </div>
  42. </div>
  43. <p>When designing your own success-or-failure type using templates
  44. <code>basic_result&lt;&gt;</code> or <code>basic_outcome&lt;&gt;</code> you have to decide what no-value policy
  45. 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>
  46. <p>You can also use one of the two other predefined aliases for <code>basic_result&lt;&gt;</code>:</p>
  47. <ul>
  48. <li><a href="../../reference/aliases/unchecked.html" class="api-reference"><code>unchecked&lt;T, E = varies&gt;</code></a>
  49. : it uses policy <a href="../../reference/policies/all_narrow.html" class="api-reference"><code>all_narrow</code></a>
  50. , where any observation of a missing value or error is undefined behavior;</li>
  51. <li><a href="../../reference/aliases/checked.html" class="api-reference"><code>checked&lt;T, E = varies&gt;</code></a>
  52. :
  53. 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>
  54. , 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>
  55. or <a href="../../reference/types/bad_result_access.html" class="api-reference"><code>bad_result_access</code></a>
  56. respectively.</li>
  57. </ul>
  58. <div class="footnotes">
  59. <hr />
  60. <ol>
  61. <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>.
  62. <a class="footnote-return" href="#fnref:1"><sup>[return]</sup></a></li>
  63. </ol>
  64. </div>
  65. </div><p><small>Last revised: February 08, 2019 at 22:18:08 UTC</small></p>
  66. <hr>
  67. <div class="spirit-nav">
  68. <a accesskey="p" href="../../tutorial/essential/outcome/inspecting.html"><img src="../../images/prev.png" alt="Prev"></a>
  69. <a accesskey="u" href="../../tutorial/essential.html"><img src="../../images/up.png" alt="Up"></a>
  70. <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>
  71. </html>