outcome.html 7.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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>outcome&lt;&gt; - 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/result/try.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/outcome/inspecting.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">outcome&lt;&gt;</h1></div></div></div>
  13. <p>Type <a href="../../reference/aliases/outcome.html" class="api-reference"><code>outcome&lt;T, EC = varies, EP = varies, NoValuePolicy = policy::default_policy&lt;T, EC, EP&gt;&gt;</code></a>
  14. represents either a successfully computed value of type <code>T</code>, or one or two reasons for failure. Failure can be represented by <code>EC</code>, or <code>EP</code>, or both, although usually it will either be an <code>EC</code> or an <code>EP</code>.
  15. Similarly to <code>result</code>, <code>EC</code> defaults to <code>std::error_code</code>/<code>boost::system::error_code</code>, and <code>EP</code> defaults to <code>std::exception_ptr</code>/<code>boost::exception_ptr</code>.</p>
  16. <p>The distinction is made into two types, <code>EC</code> and <code>EP</code>:</p>
  17. <ul>
  18. <li><code>EC</code> represents a <em>recoverable</em> failure from a lower-layer function, perhaps which was returned through <a href="../../reference/aliases/result.html" class="api-reference"><code>result&lt;T, E = varies, NoValuePolicy = policy::default_policy&lt;T, E, void&gt;&gt;</code></a>
  19. .</li>
  20. <li><code>EP</code> represents an <em>unrecoverable</em> failure where a C++ exception would ordinarily have been thrown. This is usually a pointer to an exception throw.</li>
  21. </ul>
  22. <p>It should be emphasised that this distinction is by convention only, but it will be confusing to your
  23. users if you deviate significantly from this convention.</p>
  24. <hr>
  25. <h3 id="legacy-codebases">Legacy codebases</h3>
  26. <p><code>outcome</code> is useful for transporting exceptions across layers of the program that were never designed with exception safety in mind.</p>
  27. <p>Consider a program consisting of three layers:</p>
  28. <center><img src="../../tutorial/essential/outcome/layer_chart.gif"></center>
  29. <p>The highest-level layer, <code>Layer3</code>, uses exceptions for signalling failures. The middle layer, <code>Layer2_old</code>,
  30. was not designed with exception safety in mind and functions need to return information about failures in return value.
  31. But down in the implementation details, in <code>Layer1</code>, another library is used that again throws exceptions. The goal is
  32. to be able to transfer an exception thrown in <code>Layer1</code> through <code>Layer2_old</code>, which is not exception-safe,
  33. and be able to rethrow it in <code>Layer3</code>.</p>
  34. <p>In <code>Layer1</code> we have two functions from two libraries: one reports failures by throwing exceptions, the other by returning <code>result&lt;&gt;</code>:</p>
  35. <div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">auto</span> <span class="nf">f</span><span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">int</span><span class="p">;</span> <span class="c1">// throws on failure
  36. </span><span class="c1"></span><span class="k">auto</span> <span class="nf">g</span><span class="p">()</span> <span class="k">noexcept</span> <span class="o">-&gt;</span> <span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="p">;</span>
  37. </code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/using_outcome.cpp#L36" class="code-snippet-url" target="_blank">View this code on Github</a></div>
  38. <p>In <code>Layer2_old</code> we cannot use exceptions, so its function <code>h</code> uses return type <code>outcome&lt;&gt;</code> to report failures. It is using functions <code>f</code> and <code>g</code> and reports their failures inside <code>outcome&lt;&gt;</code>:</p>
  39. <div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="k">auto</span> <span class="n">old</span><span class="o">::</span><span class="n">h</span><span class="p">()</span> <span class="k">noexcept</span> <span class="o">-&gt;</span> <span class="n">outcome</span><span class="o">::</span><span class="n">outcome</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span>
  40. <span class="p">{</span>
  41. <span class="n">BOOST_OUTCOME_TRY</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">g</span><span class="p">()));</span> <span class="c1">// #1
  42. </span><span class="c1"></span>
  43. <span class="k">try</span> <span class="p">{</span>
  44. <span class="k">return</span> <span class="n">i</span> <span class="o">+</span> <span class="n">f</span><span class="p">();</span>
  45. <span class="p">}</span>
  46. <span class="k">catch</span> <span class="p">(...)</span> <span class="p">{</span>
  47. <span class="k">return</span> <span class="n">std</span><span class="o">::</span><span class="n">current_exception</span><span class="p">();</span> <span class="c1">// #2
  48. </span><span class="c1"></span> <span class="p">}</span>
  49. <span class="p">}</span>
  50. </code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/using_outcome.cpp#L56" class="code-snippet-url" target="_blank">View this code on Github</a></div>
  51. <p>#1. Operator <code>TRY</code> can forward failures encoded in <code>result&lt;T, EC&gt;</code> as <code>outcome&lt;T, EC, EP&gt;</code> without any loss in information. You can also use <code>TRY</code> to forward failure from one <code>outcome&lt;&gt;</code> to another.</p>
  52. <p>#2. You can store the current exception through <code>std::exception_ptr</code> inside <code>outcome&lt;T, EC, EP&gt;</code> without any loss in information
  53. (provided that <code>EP</code> is <code>std::exception_ptr</code>).</p>
  54. </div><p><small>Last revised: March 19, 2019 at 22:57:48 &#43;0100</small></p>
  55. <hr>
  56. <div class="spirit-nav">
  57. <a accesskey="p" href="../../tutorial/essential/result/try.html"><img src="../../images/prev.png" alt="Prev"></a>
  58. <a accesskey="u" href="../../tutorial/essential.html"><img src="../../images/up.png" alt="Up"></a>
  59. <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../tutorial/essential/outcome/inspecting.html"><img src="../../images/next.png" alt="Next"></a></div></body>
  60. </html>