mirror of
https://github.com/boostorg/outcome.git
synced 2026-02-19 02:32:08 +00:00
99 lines
10 KiB
HTML
99 lines
10 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>Inspecting result<T, EC> - 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.html"><img src="../../images/prev.png" alt="Prev"></a>
|
|
<a accesskey="u" href="../../tutorial/result.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/result/try.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
|
|
<div class="titlepage"><div><div><h2 class="title" style="clear: both">Inspecting result<T, EC></h2></div></div></div>
|
|
<p>Suppose we will be writing a function <code>print_half</code> that takes an integer number (however big) represented as an <code>std::string</code> and outputs a number which is twice smaller:</p>
|
|
|
|
<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o"><</span><span class="kt">void</span><span class="o">></span> <span class="n">print_half</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&</span> <span class="n">text</span><span class="p">);</span>
|
|
</code></pre></div><a href="https://github.com/ned14/boost-outcome/tree/master/doc/src/snippets/using_result.cpp#L114" class="code-snippet-url" target="_blank">View this code on Github</a></div>
|
|
|
|
|
|
<p>The type <code>result<void></code> means that there is no value to be retuned upon success, but that the operation might still fail, and we may be interested in inspecting the cause of the failure. The class template <code>result<></code> is declared with the attribute <code>[[nodiscard]]</code>, which means the compiler will warn you if you forget to inspect the returned object (in C++ 17 or later).</p>
|
|
|
|
<p>The implementation will do the following: if the integral number can be represented by an <code>int</code>, we will convert to <code>int</code> and use its arithmetical operations. If the number is too large, we will fall back to using a custom <code>BigInt</code> implementation that needs to allocate memory. In the implementation we will use the function <code>convert</code> defined in the previous section.</p>
|
|
|
|
<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o"><</span><span class="kt">void</span><span class="o">></span> <span class="n">print_half</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&</span> <span class="n">text</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="k">if</span> <span class="p">(</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="n">r</span> <span class="o">=</span> <span class="n">convert</span><span class="p">(</span><span class="n">text</span><span class="p">))</span> <span class="c1">// #1
|
|
</span><span class="c1"></span> <span class="p">{</span>
|
|
<span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="p">(</span><span class="n">r</span><span class="p">.</span><span class="n">value</span><span class="p">()</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> <span class="c1">// #2
|
|
</span><span class="c1"></span> <span class="p">}</span>
|
|
<span class="k">else</span>
|
|
<span class="p">{</span>
|
|
<span class="k">if</span> <span class="p">(</span><span class="n">r</span><span class="p">.</span><span class="n">error</span><span class="p">()</span> <span class="o">==</span> <span class="n">ConversionErrc</span><span class="o">::</span><span class="n">TooLong</span><span class="p">)</span> <span class="c1">// #3
|
|
</span><span class="c1"></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="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="n">text</span><span class="p">));</span> <span class="c1">// #4
|
|
</span><span class="c1"></span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o"><<</span> <span class="n">i</span><span class="p">.</span><span class="n">half</span><span class="p">()</span> <span class="o"><<</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
<span class="k">else</span>
|
|
<span class="p">{</span>
|
|
<span class="k">return</span> <span class="n">r</span><span class="p">.</span><span class="n">as_failure</span><span class="p">();</span> <span class="c1">// #5
|
|
</span><span class="c1"></span> <span class="p">}</span>
|
|
<span class="p">}</span>
|
|
<span class="k">return</span> <span class="n">outcome</span><span class="o">::</span><span class="n">success</span><span class="p">();</span> <span class="c1">// #6
|
|
</span><span class="c1"></span><span class="p">}</span>
|
|
</code></pre></div><a href="https://github.com/ned14/boost-outcome/tree/master/doc/src/snippets/using_result.cpp#L118" class="code-snippet-url" target="_blank">View this code on Github</a></div>
|
|
|
|
|
|
<p>#1. You test if <code>result<></code> object represents a successful operation with contextual conversion to <code>bool</code>.</p>
|
|
|
|
<p>#2. The function <code>.value()</code> extracts the successfully returned <code>int</code>.</p>
|
|
|
|
<p>#3. The function <code>.error()</code> allows you to inspect the error sub-object, representing information about the reason for failure.</p>
|
|
|
|
<p>#4. Macro <code>BOOST_OUTCOME_TRY</code> represents a control statement. It implies that the expression in the second argument returns a <code>result<></code>. The function is defined as:</p>
|
|
|
|
<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="cm">/*static*/</span> <span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o"><</span><span class="n">BigInt</span><span class="o">></span> <span class="n">BigInt</span><span class="o">::</span><span class="n">fromString</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&</span> <span class="n">s</span><span class="p">)</span>
|
|
</code></pre></div><a href="https://github.com/ned14/boost-outcome/tree/master/doc/src/snippets/using_result.cpp#L107" class="code-snippet-url" target="_blank">View this code on Github</a></div>
|
|
|
|
|
|
<p>Our control statement means: if <code>fromString</code> returned failure, this same error information should be returned from <code>print_half</code>, even though the type of <code>result<></code> is different. If <code>fromString</code> returned success, we create variable <code>i</code> of type <code>BigInt</code> with the value returned from <code>fromString</code>. If control goes to subsequent line, it means <code>fromString</code> succeeded and variable of type <code>BigInt</code> is in scope.</p>
|
|
|
|
<p>#5. In the return statement we extract the error information and use it to initialize the return value from <code>print_half</code>. We could have written <code>return r.error();</code> instead,
|
|
and it would have the same effect, but <code>r.as_failure()</code> will work when implicit construction from <code>E</code> has been disabled due to <code>T</code> and <code>E</code> having a constructibility relationship.</p>
|
|
|
|
<p>#6. Function <code>success()</code> returns an object of type <code>success<void></code> representing success. This is implicitly converted by
|
|
all <code>result</code> and <code>outcome</code> types into a successful return, default constructing any <code>T</code> if necessary.</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.html"><img src="../../images/prev.png" alt="Prev"></a>
|
|
<a accesskey="u" href="../../tutorial/result.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/result/try.html"><img src="../../images/next.png" alt="Next"></a></div></body>
|
|
</html>
|