mirror of
https://github.com/boostorg/outcome.git
synced 2026-02-18 14:22:08 +00:00
101 lines
7.7 KiB
HTML
101 lines
7.7 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>outcome<> - 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 id="boost-common-heading-doc" style="background: #574D74 url('../images/header-bg.png') repeat-x top left;">
|
|
<div class="heading-inner" style="background: url('../images/header-fg.png') no-repeat top left;">
|
|
<div class="heading-placard"></div>
|
|
|
|
<h1 class="heading-title">
|
|
<a href="../">
|
|
<img src="../images/space.png" alt="Boost C++ Libraries" class="heading-logo" />
|
|
<span class="heading-boost">Boost</span>
|
|
<span class="heading-cpplibraries">C++ Libraries</span>
|
|
</a>
|
|
</h1>
|
|
|
|
<p class="heading-quote">
|
|
<q>...one of the most highly
|
|
regarded and expertly designed C++ library projects in the
|
|
world.</q> <span class="heading-attribution">— <a href=
|
|
"http://www.gotw.ca/" class="external">Herb Sutter</a> and <a href=
|
|
"http://en.wikipedia.org/wiki/Andrei_Alexandrescu" class="external">Andrei
|
|
Alexandrescu</a>, <a href=
|
|
"http://safari.awprofessional.com/?XmlId=0321113586" class="external">C++
|
|
Coding Standards</a></span></p>
|
|
</div>
|
|
</div>
|
|
<div id="boost-common-heading-doc-spacer"></div>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="../tutorial/result/try.html"><img src="../images/prev.png" alt="Prev"></a>
|
|
<a accesskey="u" href="../tutorial.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/outcome/inspecting.html"><img src="../images/next.png" alt="Next"></a></div><div id="content">
|
|
|
|
<div class="titlepage"><div><div><h2 class="title" style="clear: both">outcome<></h2></div></div></div>
|
|
|
|
|
|
<h2 id="outcome"><code>outcome<></code></h2>
|
|
|
|
<p>Type <a href="reference/outcome/#standardese-outcome_v2_xxx__outcome-R-S-P-NoValuePolicy-" class="api-reference"><i class="fa fa-book" aria-hidden="true"></i> outcome<T, EC, EP, NVP></a>
|
|
represets either a successfully computed value of type <code>T</code> or a reason 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>. <code>EC</code> defaults to <code>std::error_code</code> and <code>EP</code> defaults to <code>std::exception_ptr</code>. The distinction is made into two types, <code>EC</code> and <code>EP</code>:</p>
|
|
|
|
<ul>
|
|
<li><code>EC</code> represents a failue from lower-layer function which was retured through <a href="reference/result/#standardese-outcome_v2_xxx__result-R-S-NoValuePolicy-" class="api-reference"><i class="fa fa-book" aria-hidden="true"></i> result<T, EC></a>
|
|
.</li>
|
|
<li><code>EP</code> represents pointer to an exception thrown in a lower-layer function to signal a failure; but at the current level we do not want to proceed with stack unwinding.</li>
|
|
</ul>
|
|
|
|
<p><code>outcome</code> is useful for transporting exceptions across layers of the program that were never designed with exception safety in mind.</p>
|
|
|
|
<p>Consider a program consisting of three layers:</p>
|
|
|
|
|
|
|
|
<p>The highest-level layer, <code>Layer3</code>, uses exceptions for signalling failures. The middle layer, <code>Layer2_old</code>,
|
|
was not designed with exception safety in mind and functions need to return information about failures in return value.
|
|
But down in the implementation details, in <code>Layer1</code>, another library is used that again throws exceptions. The goal is
|
|
to be able to transfer an exception thrown in <code>Layer1</code> through <code>Layer2_old</code>, which is not exception-safe,
|
|
and be able to rethrow it in <code>Layer3</code>.</p>
|
|
|
|
<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>
|
|
|
|
<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
|
|
</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>
|
|
</code></pre></div><a href="https://github.com/ned14/boost-outcome/tree/master/doc/src/snippets/using_outcome.cpp#L7" class="code-snippet-url" target="_blank">View this code on Github</a></div>
|
|
|
|
|
|
<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>
|
|
|
|
<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>
|
|
<span class="p">{</span>
|
|
<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
|
|
</span><span class="c1"></span>
|
|
<span class="k">try</span> <span class="p">{</span>
|
|
<span class="k">return</span> <span class="n">i</span> <span class="o">+</span> <span class="n">f</span><span class="p">();</span>
|
|
<span class="p">}</span>
|
|
<span class="k">catch</span> <span class="p">(...)</span> <span class="p">{</span>
|
|
<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
|
|
</span><span class="c1"></span> <span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</code></pre></div><a href="https://github.com/ned14/boost-outcome/tree/master/doc/src/snippets/using_outcome.cpp#L27" class="code-snippet-url" target="_blank">View this code on Github</a></div>
|
|
|
|
|
|
<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>
|
|
|
|
<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
|
|
(provided that <code>EP</code> is <code>std::exception_ptr</code>).</p>
|
|
|
|
|
|
|
|
</div><p><small>Last revised: December 05, 2018 at 14:18:52 UTC</small></p>
|
|
<hr>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="../tutorial/result/try.html"><img src="../images/prev.png" alt="Prev"></a>
|
|
<a accesskey="u" href="../tutorial.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/outcome/inspecting.html"><img src="../images/next.png" alt="Next"></a></div></body>
|
|
</html>
|