inspecting.html 9.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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>Inspecting result&lt;T, EC&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.html"><img src="../../../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../../../tutorial/essential/result.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/result/try.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">Inspecting result<T, EC></h1></div></div></div>
  13. <p>Suppose we will be writing a function <code>print_half</code> that takes a <code>std::string</code> representing an integer and prints half the integer:</p>
  14. <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">&lt;</span><span class="kt">void</span><span class="o">&gt;</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">&amp;</span> <span class="n">text</span><span class="p">);</span>
  15. </code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/using_result.cpp#L144" class="code-snippet-url" target="_blank">View this code on Github</a></div>
  16. <p>The type <code>result&lt;void&gt;</code> means that there is no value to be returned 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&lt;&gt;</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>
  17. <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>
  18. <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">&lt;</span><span class="kt">void</span><span class="o">&gt;</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">&amp;</span> <span class="n">text</span><span class="p">)</span>
  19. <span class="p">{</span>
  20. <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">&lt;</span><span class="kt">int</span><span class="o">&gt;</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
  21. </span><span class="c1"></span> <span class="p">{</span>
  22. <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</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">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span> <span class="c1">// #2
  23. </span><span class="c1"></span> <span class="p">}</span>
  24. <span class="k">else</span>
  25. <span class="p">{</span>
  26. <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
  27. </span><span class="c1"></span> <span class="p">{</span>
  28. <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
  29. </span><span class="c1"></span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">i</span><span class="p">.</span><span class="n">half</span><span class="p">()</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
  30. <span class="p">}</span>
  31. <span class="k">else</span>
  32. <span class="p">{</span>
  33. <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
  34. </span><span class="c1"></span> <span class="p">}</span>
  35. <span class="p">}</span>
  36. <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
  37. </span><span class="c1"></span><span class="p">}</span>
  38. </code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/using_result.cpp#L148" class="code-snippet-url" target="_blank">View this code on Github</a></div>
  39. <p>#1. You test if <code>result&lt;&gt;</code> object represents a successful operation with contextual conversion to <code>bool</code>.</p>
  40. <p>#2. The function <code>.value()</code> extracts the successfully returned <code>int</code>.</p>
  41. <p>#3. The function <code>.error()</code> allows you to inspect the error sub-object, representing information about the reason for failure.</p>
  42. <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&lt;&gt;</code>. The function is defined as:</p>
  43. <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">&lt;</span><span class="n">BigInt</span><span class="o">&gt;</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">&amp;</span> <span class="n">s</span><span class="p">)</span>
  44. </code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/using_result.cpp#L137" class="code-snippet-url" target="_blank">View this code on Github</a></div>
  45. <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&lt;&gt;</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>
  46. <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,
  47. 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>
  48. <p>#6. Function <code>success()</code> returns an object of type <code>success&lt;void&gt;</code> representing success. This is implicitly converted by
  49. all <code>result</code> and <code>outcome</code> types into a successful return, default constructing any <code>T</code> if necessary.</p>
  50. </div><p><small>Last revised: March 22, 2019 at 15:01:40 -0700</small></p>
  51. <hr>
  52. <div class="spirit-nav">
  53. <a accesskey="p" href="../../../tutorial/essential/result.html"><img src="../../../images/prev.png" alt="Prev"></a>
  54. <a accesskey="u" href="../../../tutorial/essential/result.html"><img src="../../../images/up.png" alt="Up"></a>
  55. <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/essential/result/try.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
  56. </html>