2
0
mirror of https://github.com/boostorg/outcome.git synced 2026-01-23 05:42:25 +00:00
Files
outcome/doc/html/tutorial/advanced/interop/value-or-error.html
2026-01-18 22:37:02 +00:00

69 lines
5.1 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>value_or_error Concept - 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 class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/interop/problem.html"><img src="../../../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../../tutorial/advanced/interop.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/advanced/interop/httplib.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
<div class="titlepage"><div><div><h1 style="clear: both">value_or_error Concept</h1></div></div></div>
<p>Something not really mentioned until now is how Outcome interoperates with the proposed
<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>
, whose design lands in between <a href="../../../reference/aliases/unchecked.html" class="api-reference"><code>unchecked&lt;T, E = varies&gt;</code></a>
and <a href="../../../reference/aliases/checked.html" class="api-reference"><code>checked&lt;T, E = varies&gt;</code></a>
(both of which are type aliases hard coding no-value
policies <a href="../../../tutorial/essential/no-value/builtin.html">as previously covered in this tutorial</a>).</p>
<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
for code using Expected and Outcome ought to be seamless thanks to the <a href="https://wg21.link/P0786">proposed <code>ValueOrError</code>
concept framework</a>, a subset of which Outcome implements.</p>
<p>The <a href="../../../reference/types/basic_result/explicit_valueorerror_converting_constructor.html" class="api-reference"><code>explicit basic_result(concepts::value_or_error&lt;T, E&gt; &amp;&amp;)</code></a>
and <a href="../../../reference/types/basic_outcome/explicit_valueorerror_converting_constructor.html" class="api-reference"><code>explicit basic_outcome(concepts::value_or_error&lt;T, E&gt; &amp;&amp;)</code></a>
constructors will explicitly construct from any type matching the <a href="../../../reference/types/basic_result/explicit_valueorerror_converting_constructor.html" class="api-reference"><code>concepts::value_or_error&lt;T, E&gt;</code></a>
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
constructible to <code>Y</code>. The <code>value_or_error</code> concept in turn is true if and only if the input type has:</p>
<ol>
<li>A <code>.has_value()</code> observer returning a <code>bool</code>.</li>
<li><code>.value()</code> and <code>.error()</code> observers.</li>
</ol>
<h2 id="implementation">Implementation</h2>
<p>Outcome&rsquo;s machinery for implementing <code>concepts::value_or_error</code> conversion is user extensible by injection
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>
type into the <code>BOOST_OUTCOME_V2_NAMESPACE::convert</code> namespace.</p>
<p>Outcome&rsquo;s default <code>convert::value_or_error&lt;T, U&gt;</code> implementation explicitly
excludes Outcome <code>result</code> and <code>outcome</code> types from the default mechanism as
there is a major gotcha: the <code>value_or_error</code> matched type&rsquo;s <code>.value()</code> is often
not callable in constexpr as it can throw, which makes this conversion mechanism
pretty much useless for constexpr code. Besides, <code>outcome</code> has a converting
constructor overload for <code>result</code> inputs which is constexpr capable.</p>
<p>Note that if you do enable <code>outcome</code> inputs, a <code>result</code> will match an input
<code>outcome</code>, but silently dropping any exception state. This is probably undesirable.</p>
<p>Examples of how to implement your own <code>convert::value_or_error&lt;T, U&gt;</code> converter
is demonstrated in the worked example, next.</p>
</div><p><small>Last revised: December 15, 2020 at 12:22:39 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/advanced/interop/problem.html"><img src="../../../images/prev.png" alt="Prev"></a>
<a accesskey="u" href="../../../tutorial/advanced/interop.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/advanced/interop/httplib.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>