• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
8<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
9<body><div class="spirit-nav">
10<a accesskey="p" href="../../../tutorial/advanced/interop/problem.html"><img src="../../../images/prev.png" alt="Prev"></a>
11    <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
12    <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">
13  <div class="titlepage"><div><div><h1 style="clear: both">ValueOrError Concept</h1></div></div></div>
14
15
16<p>Something not really mentioned until now is how Outcome interoperates with the proposed
17
18<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>
19, whose design lands in between <a href="../../../reference/aliases/unchecked.html" class="api-reference"><code>unchecked&lt;T, E = varies&gt;</code></a>
20
21and <a href="../../../reference/aliases/checked.html" class="api-reference"><code>checked&lt;T, E = varies&gt;</code></a>
22 (both of which are type aliases hard coding no-value
23policies <a href="../../../tutorial/essential/no-value/builtin.html">as previously covered in this tutorial</a>).</p>
24
25<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
26for code using Expected and Outcome ought to be seamless thanks to the <a href="https://wg21.link/P0786">proposed <code>ValueOrError</code>
27concept framework</a>, a subset of which Outcome implements.</p>
28
29<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>
30 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>
31
32constructors will explicitly construct from any type matching the <a href="../../../reference/types/basic_result/explicit_valueorerror_converting_constructor.html" class="api-reference"><code>ValueOrError&lt;T, E&gt;</code></a>
33
34concept, 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
35constructible to <code>Y</code>. The <code>ValueOrError</code> concept in turn is true if and only if the input type has:</p>
36
37<ol>
38<li>A <code>.has_value()</code> observer returning a <code>bool</code>.</li>
39<li><code>.value()</code> and <code>.error()</code> observers.</li>
40</ol>
41
42<h2 id="implementation">Implementation</h2>
43
44<p>Outcome&rsquo;s machinery for implementing <code>ValueOrError</code> conversion is user extensible by injection
45of specialisations of the <a href="../../../reference/converters/value_or_error.html" class="api-reference"><code>value_or_error&lt;T, U&gt;</code></a>
46 type into the <code>BOOST_OUTCOME_V2_NAMESPACE::convert</code> namespace.</p>
47
48<p>Outcome&rsquo;s default <code>convert::value_or_error&lt;T, U&gt;</code> implementation explicitly
49excludes Outcome <code>result</code> and <code>outcome</code> types from the default mechanism as
50there is a major gotcha: the <code>ValueOrError</code> matched type&rsquo;s <code>.value()</code> is often
51not callable in constexpr as it can throw, which makes this conversion mechanism
52pretty much useless for constexpr code. Besides, <code>outcome</code> has a converting
53constructor overload for <code>result</code> inputs which is constexpr capable.</p>
54
55<p>Note that if you do enable <code>outcome</code> inputs, a <code>result</code> will match an input
56<code>outcome</code>, but silently dropping any exception state. This is probably undesirable.</p>
57
58<p>Examples of how to implement your own <code>convert::value_or_error&lt;T, U&gt;</code> converter
59is demonstrated in the worked example, next.</p>
60
61
62        </div><p><small>Last revised: February 11, 2019 at 17:14:51 UTC</small></p>
63<hr>
64<div class="spirit-nav">
65<a accesskey="p" href="../../../tutorial/advanced/interop/problem.html"><img src="../../../images/prev.png" alt="Prev"></a>
66    <a accesskey="u" href="../../../tutorial/advanced/interop.html"><img src="../../../images/up.png" alt="Up"></a>
67    <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>
68</html>
69