• 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>Implicit conversion - 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="../../experimental/worked-example/source.html"><img src="../../images/prev.png" alt="Prev"></a>
11    <a accesskey="u" href="../../experimental/worked-example.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="../../experimental/outcome.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
13  <div class="titlepage"><div><div><h1 style="clear: both">Implicit conversion</h1></div></div></div>
14  <p>Back in <a href="../../experimental/worked-example/value_type.html">The payload</a>, we
15mentioned that there was no default implicit conversion of <code>file_io_error</code>
16(<code>status_code&lt;_file_io_error_domain&gt;</code>) to <code>error</code>, as <code>error</code> is too small
17to hold <code>_file_io_error_domain::value_type</code>.</p>
18
19<p>We can tell the framework about available implicit conversions by defining
20an ADL discovered free function <code>make_status_code()</code> which takes our
21custom status code as input, and returns an <code>error</code>:</p>
22
23<div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="c1">// Now tell `error` how it can implicitly construct from `file_io_error`.
24</span><span class="c1">// This is done by us defining a free function called `make_status_code()`
25</span><span class="c1">// which is discovered using ADL. `error` is an alias to the refinement
26</span><span class="c1">// `status_code&lt;erased&lt;intptr_t&gt;&gt;` which is a status code whose value type
27</span><span class="c1">// has been erased into an `intptr_t`. `status_code&lt;erased&lt;intptr_t&gt;&gt;`
28</span><span class="c1">// (i.e. `error`) are move bitcopying (P1029) i.e. they are move-only
29</span><span class="c1">// types whose move operation is defined to leave the source in the same
30</span><span class="c1">// representation as a default constructed instance, and for whose
31</span><span class="c1">// non-trivial destructor when called upon a default constructed instance
32</span><span class="c1">// is guaranteed to do nothing.
33</span><span class="c1"></span><span class="kr">inline</span> <span class="n">outcome_e</span><span class="o">::</span><span class="n">system_code</span> <span class="n">make_status_code</span><span class="p">(</span><span class="n">file_io_error</span> <span class="n">v</span><span class="p">)</span>
34<span class="p">{</span>
35  <span class="c1">// `make_status_code_ptr()` dynamically allocates memory to store an
36</span><span class="c1"></span>  <span class="c1">// instance of `file_io_error`, then returns a status code whose domain
37</span><span class="c1"></span>  <span class="c1">// specifies that its value type is a pointer to `file_io_error`. The
38</span><span class="c1"></span>  <span class="c1">// domain is a templated instance which indirects all observers of the
39</span><span class="c1"></span>  <span class="c1">// status code to the pointed-to status code.
40</span><span class="c1"></span>  <span class="c1">//
41</span><span class="c1"></span>  <span class="c1">// Note that the status code returned&#39;s value type is a pointer, which
42</span><span class="c1"></span>  <span class="c1">// by definition fits into `intptr_t` and is trivially copyable.
43</span><span class="c1"></span>  <span class="c1">// Therefore `system_code` (which is also a type alias to
44</span><span class="c1"></span>  <span class="c1">// `status_code&lt;erased&lt;intptr_t&gt;&gt;`) is happy to implicitly construct
45</span><span class="c1"></span>  <span class="c1">// from the status code returned by `make_status_code_ptr()`.
46</span><span class="c1"></span>  <span class="k">return</span> <span class="n">make_status_code_ptr</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">move</span><span class="p">(</span><span class="n">v</span><span class="p">));</span>
47<span class="p">}</span>
48</code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/experimental_status_code.cpp#L152" class="code-snippet-url" target="_blank">View this code on Github</a></div>
49
50
51<p>We are now ready to use Experimental Outcome!</p>
52
53
54        </div><p><small>Last revised: January 26, 2019 at 23:38:56 UTC</small></p>
55<hr>
56<div class="spirit-nav">
57<a accesskey="p" href="../../experimental/worked-example/source.html"><img src="../../images/prev.png" alt="Prev"></a>
58    <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a>
59    <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/outcome.html"><img src="../../images/next.png" alt="Next"></a></div></body>
60</html>
61