status_result.html 6.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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>status_result and status_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. <link rel="icon" href="../images/favicon.ico" type="image/ico"/>
  8. <body><div class="spirit-nav">
  9. <a accesskey="p" href="../experimental/differences.html"><img src="../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../experimental.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="../experimental/worked-example.html"><img src="../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both"><code>status_result</code> and <code>status_outcome</code></h1></div></div></div>
  13. <p><code>status_result</code> and <code>status_outcome</code> are type aliases to <a href="../reference/types/basic_result.html" class="api-reference"><code>basic_result&lt;T, E, NoValuePolicy&gt;</code></a>
  14. and <a href="../reference/types/basic_outcome.html" class="api-reference"><code>basic_outcome&lt;T, EC, EP, NoValuePolicy&gt;</code></a>
  15. in the usual way, but
  16. with a defaulted <code>NoValuePolicy</code> which selects on the basis of <code>status_code&lt;DomainType&gt;</code>
  17. instead.</p>
  18. <div class="notices note" style="background: url('../images/note.png') top left no-repeat padding-box padding-box;">
  19. <div class="notices heading">note</div>
  20. <div class="notices message"><p>If the <code>E</code> type is not some <code>status_code&lt;&gt;</code>, the default policy selector
  21. will complain.</p>
  22. </div>
  23. </div>
  24. <p>The specifications are:</p>
  25. <div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="n">experimental</span><span class="o">::</span><span class="n">status_result</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="n">E</span> <span class="o">=</span> <span class="n">experimental</span><span class="o">::</span><span class="n">system_code</span><span class="o">&gt;</span>
  26. <span class="n">experimental</span><span class="o">::</span><span class="n">status_outcome</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="n">E</span> <span class="o">=</span> <span class="n">experimental</span><span class="o">::</span><span class="n">system_code</span><span class="p">,</span> <span class="n">EP</span> <span class="o">=</span> <span class="n">std</span><span class="o">::</span><span class="n">exception_ptr</span><span class="o">&gt;</span>
  27. </code></pre></div>
  28. <p>So, the default <code>E</code> is the erased status code <code>system_code</code>, which can represent
  29. any <code>generic_code</code>, <code>posix_code</code>, <code>win32_code</code>, <code>nt_code</code>, <code>com_code</code> and many
  30. other integer error and status
  31. codings. <strong>Note</strong> that <code>system_code</code> may represent successes as well as failures.
  32. This mirrors, somewhat, how <code>std::error_code</code> can have an all bits zero defaulted
  33. state.</p>
  34. <p>You can absolutely choose an <code>E</code> type which is non-erased e.g. <code>posix_code</code> directly.
  35. You can also choose an <code>E</code> type which is contract guaranteed to be a failure
  36. rather than an unknown success or failure &ndash; see <code>errored_status_code</code>.</p>
  37. <p>Whether to choose typed status codes versus the erased status codes depends on your
  38. use cases. Outcome replicates faithfully the implicit and explicit conversion
  39. semantics of its underlying types, so you can mix results and outcomes of
  40. <code>&lt;system_error2&gt;</code> types exactly as you can the <code>&lt;system_error2&gt;</code> types themselves
  41. e.g. typed forms will implicitly convert into erased forms if the source type
  42. is trivially copyable or move relocating. This means that you can return a
  43. <code>generic_code</code> from a function returning a <code>system_code</code> or <code>error</code>, and it&rsquo;ll
  44. work exactly as you&rsquo;d expect (implicit conversion).</p>
  45. <div class="notices note" style="background: url('../images/note.png') top left no-repeat padding-box padding-box;">
  46. <div class="notices heading">note</div>
  47. <div class="notices message"><p>As <code>status_code&lt;erased&lt;T&gt;&gt;</code> is move-only, so is any <code>status_result</code> or <code>status_outcome</code>.
  48. For some reason this surprises a lot of people, and they tend to react by not using the erased
  49. form because it seems &ldquo;difficult&rdquo;.</p>
  50. </div>
  51. </div>
  52. <p>It is actually, in fact, a wise discipline to follow to make all functions return
  53. move-only types if you care about determinism and performance. Whilst C++ 17 onwards
  54. does much to have the compiler avoid copying of identical function return values thanks to
  55. guaranteed copy elision, when a chain of functions return different types, if the
  56. programmer forgets to scatter <code>std::move()</code> appropriately, copies rather than moves
  57. tend to occur in non-obvious ways. No doubt future C++ standards will improve on the
  58. automatic use of moves instead of copies where possible, but until then making all
  59. your <code>result</code> and <code>outcome</code> types move-only is an excellent discipline.</p>
  60. <p>Note that move-only <code>result</code> and <code>outcome</code> capable code (i.e. your project is in Experimental
  61. Outcome configuration) usually compiles fine when <code>result</code> and <code>outcome</code> are copyable
  62. (i.e. your project is in Standard Outcome configuration), albeit sometimes with a few
  63. compiler warnings about unnecessary use of <code>std::move()</code>.</p>
  64. </div><p><small>Last revised: February 05, 2019 at 17:14:18 UTC</small></p>
  65. <hr>
  66. <div class="spirit-nav">
  67. <a accesskey="p" href="../experimental/differences.html"><img src="../images/prev.png" alt="Prev"></a>
  68. <a accesskey="u" href="../experimental.html"><img src="../images/up.png" alt="Up"></a>
  69. <a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="../experimental/worked-example.html"><img src="../images/next.png" alt="Next"></a></div></body>
  70. </html>