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<> - 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/result/try_greedy.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/outcome/inspecting.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content"> 13 14 <div class="titlepage"><div><div><h1 style="clear: both">outcome<></h1></div></div></div> 15 16 17<p>Type <a href="../../reference/aliases/outcome.html" class="api-reference"><code>outcome<T, EC = varies, EP = varies, NoValuePolicy = policy::default_policy<T, EC, EP>></code></a> 18 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>. 19Similarly 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> 20 21<p>The distinction is made into two types, <code>EC</code> and <code>EP</code>:</p> 22 23<ul> 24<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<T, E = varies, NoValuePolicy = policy::default_policy<T, E, void>></code></a> 25.</li> 26<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> 27</ul> 28 29<p>It should be emphasised that this distinction is by convention only, but it will be confusing to your 30users if you deviate significantly from this convention.</p> 31 32<hr> 33 34<h3 id="legacy-codebases">Legacy codebases</h3> 35 36<p><code>outcome</code> is useful for transporting exceptions across layers of the program that were never designed with exception safety in mind.</p> 37 38<p>Consider a program consisting of three layers:</p> 39 40 41<center><img src="../../tutorial/essential/outcome/layer_chart.gif"></center> 42 43<p>The highest-level layer, <code>Layer3</code>, uses exceptions for signalling failures. The middle layer, <code>Layer2_old</code>, 44was not designed with exception safety in mind and functions need to return information about failures in return value. 45But down in the implementation details, in <code>Layer1</code>, another library is used that again throws exceptions. The goal is 46to be able to transfer an exception thrown in <code>Layer1</code> through <code>Layer2_old</code>, which is not exception-safe, 47and be able to rethrow it in <code>Layer3</code>.</p> 48 49<p>In <code>Layer1</code> we have two functions from two libraries: one reports failures by throwing exceptions, the other by returning <code>result<></code>:</p> 50 51<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">-></span> <span class="kt">int</span><span class="p">;</span> <span class="c1">// throws on failure 52</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">-></span> <span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o"><</span><span class="kt">int</span><span class="o">></span><span class="p">;</span> 53</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> 54 55 56<p>In <code>Layer2_old</code> we cannot use exceptions, so its function <code>h</code> uses return type <code>outcome<></code> to report failures. It is using functions <code>f</code> and <code>g</code> and reports their failures inside <code>outcome<></code>:</p> 57 58<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">-></span> <span class="n">outcome</span><span class="o">::</span><span class="n">outcome</span><span class="o"><</span><span class="kt">int</span><span class="o">></span> 59<span class="p">{</span> 60 <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 61</span><span class="c1"></span> 62 <span class="k">try</span> <span class="p">{</span> 63 <span class="k">return</span> <span class="n">i</span> <span class="o">+</span> <span class="n">f</span><span class="p">();</span> 64 <span class="p">}</span> 65 <span class="k">catch</span> <span class="p">(...)</span> <span class="p">{</span> 66 <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 67</span><span class="c1"></span> <span class="p">}</span> 68<span class="p">}</span> 69</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> 70 71 72<p>#1. Operator <code>TRY</code> can forward failures encoded in <code>result<T, EC></code> as <code>outcome<T, EC, EP></code> without any loss in information. You can also use <code>TRY</code> to forward failure from one <code>outcome<></code> to another.</p> 73 74<p>#2. You can store the current exception through <code>std::exception_ptr</code> inside <code>outcome<T, EC, EP></code> without any loss in information 75 (provided that <code>EP</code> is <code>std::exception_ptr</code>).</p> 76 77 78 79 </div><p><small>Last revised: March 19, 2019 at 22:57:48 +0100</small></p> 80<hr> 81<div class="spirit-nav"> 82<a accesskey="p" href="../../tutorial/essential/result/try_greedy.html"><img src="../../images/prev.png" alt="Prev"></a> 83 <a accesskey="u" href="../../tutorial/essential.html"><img src="../../images/up.png" alt="Up"></a> 84 <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> 85</html> 86