value-or-error.html 5.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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>ValueOrError Concept - 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/advanced/interop/problem.html"><img src="../../../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../../../tutorial/advanced/interop.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/advanced/interop/httplib.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">ValueOrError Concept</h1></div></div></div>
  13. <p>Something not really mentioned until now is how Outcome interoperates with the proposed
  14. <a href="https://wg21.link/P0323" class="api-reference" target="_blank"><i class="fa fa-book" aria-hidden="true"></i> P0323 <code>std::expected&lt;T, E&gt;</code></a>
  15. , whose design lands in between <a href="../../../reference/aliases/unchecked.html" class="api-reference"><code>unchecked&lt;T, E = varies&gt;</code></a>
  16. and <a href="../../../reference/aliases/checked.html" class="api-reference"><code>checked&lt;T, E = varies&gt;</code></a>
  17. (both of which are type aliases hard coding no-value
  18. policies <a href="../../../tutorial/essential/no-value/builtin.html">as previously covered in this tutorial</a>).</p>
  19. <p>Expected and Outcome are <a href="../../../faq.html#why-doesn-t-outcome-duplicate-std-expected-t-e-s-design">isomorphic to one another in design intent</a>, but interoperation
  20. for code using Expected and Outcome ought to be seamless thanks to the <a href="https://wg21.link/P0786">proposed <code>ValueOrError</code>
  21. concept framework</a>, a subset of which Outcome implements.</p>
  22. <p>The <a href="../../../reference/types/basic_result/explicit_valueorerror_converting_constructor.html" class="api-reference"><code>explicit basic_result(ValueOrError&lt;T, E&gt; &amp;&amp;)</code></a>
  23. and <a href="../../../reference/types/basic_outcome/explicit_valueorerror_converting_constructor.html" class="api-reference"><code>explicit basic_outcome(ValueOrError&lt;T, E&gt; &amp;&amp;)</code></a>
  24. constructors will explicitly construct from any type matching the <a href="../../../reference/concepts/valueorerror.html" class="api-reference"><code>ValueOrError&lt;T, E&gt;</code></a>
  25. concept, which includes <code>std::expected&lt;A, B&gt;</code>, if <code>A</code> is constructible to <code>X</code>, and <code>B</code> is
  26. constructible to <code>Y</code>. The <code>ValueOrError</code> concept in turn is true if and only if the input type has:</p>
  27. <ol>
  28. <li>A <code>.has_value()</code> observer returning a <code>bool</code>.</li>
  29. <li><code>.value()</code> and <code>.error()</code> observers.</li>
  30. </ol>
  31. <h2 id="implementation">Implementation</h2>
  32. <p>Outcome&rsquo;s machinery for implementing <code>ValueOrError</code> conversion is user extensible by injection
  33. of specialisations of the <a href="../../../reference/converters/value_or_error.html" class="api-reference"><code>value_or_error&lt;T, U&gt;</code></a>
  34. type into the <code>BOOST_OUTCOME_V2_NAMESPACE::convert</code> namespace.</p>
  35. <p>Outcome&rsquo;s default <code>convert::value_or_error&lt;T, U&gt;</code> implementation explicitly
  36. excludes Outcome <code>result</code> and <code>outcome</code> types from the default mechanism as
  37. there is a major gotcha: the <code>ValueOrError</code> matched type&rsquo;s <code>.value()</code> is often
  38. not callable in constexpr as it can throw, which makes this conversion mechanism
  39. pretty much useless for constexpr code. Besides, <code>outcome</code> has a converting
  40. constructor overload for <code>result</code> inputs which is constexpr capable.</p>
  41. <p>Note that if you do enable <code>outcome</code> inputs, a <code>result</code> will match an input
  42. <code>outcome</code>, but silently dropping any exception state. This is probably undesirable.</p>
  43. <p>Examples of how to implement your own <code>convert::value_or_error&lt;T, U&gt;</code> converter
  44. is demonstrated in the worked example, next.</p>
  45. </div><p><small>Last revised: February 11, 2019 at 17:14:51 UTC</small></p>
  46. <hr>
  47. <div class="spirit-nav">
  48. <a accesskey="p" href="../../../tutorial/advanced/interop/problem.html"><img src="../../../images/prev.png" alt="Prev"></a>
  49. <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
  50. <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/advanced/interop/httplib.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
  51. </html>