implicit_conversion.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. <link rel="icon" href="../../images/favicon.ico" type="image/ico"/>
  8. <body><div class="spirit-nav">
  9. <a accesskey="p" href="../../experimental/worked-example/source.html"><img src="../../images/prev.png" alt="Prev"></a>
  10. <a accesskey="u" href="../../experimental/worked-example.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/outcome.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">Implicit conversion</h1></div></div></div>
  13. <p>Back in <a href="../../experimental/worked-example/value_type.html">The payload</a>, we
  14. mentioned that there was no default implicit conversion of <code>file_io_error</code>
  15. (<code>status_code&lt;_file_io_error_domain&gt;</code>) to <code>error</code>, as <code>error</code> is too small
  16. to hold <code>_file_io_error_domain::value_type</code>.</p>
  17. <p>We can tell the framework about available implicit conversions by defining
  18. an ADL discovered free function <code>make_status_code()</code> which takes our
  19. custom status code as input, and returns an <code>error</code>:</p>
  20. <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`.
  21. </span><span class="c1">// This is done by us defining a free function called `make_status_code()`
  22. </span><span class="c1">// which is discovered using ADL. `error` is an alias to the refinement
  23. </span><span class="c1">// `status_code&lt;erased&lt;intptr_t&gt;&gt;` which is a status code whose value type
  24. </span><span class="c1">// has been erased into an `intptr_t`. `status_code&lt;erased&lt;intptr_t&gt;&gt;`
  25. </span><span class="c1">// (i.e. `error`) are move relocating (P1029) i.e. they are move-only
  26. </span><span class="c1">// types whose move operation is defined to leave the source in the same
  27. </span><span class="c1">// representation as a default constructed instance, and for whose
  28. </span><span class="c1">// non-trivial destructor when called upon a default constructed instance
  29. </span><span class="c1">// is guaranteed to do nothing.
  30. </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>
  31. <span class="p">{</span>
  32. <span class="c1">// `make_status_code_ptr()` dynamically allocates memory to store an
  33. </span><span class="c1"></span> <span class="c1">// instance of `file_io_error`, then returns a status code whose domain
  34. </span><span class="c1"></span> <span class="c1">// specifies that its value type is a pointer to `file_io_error`. The
  35. </span><span class="c1"></span> <span class="c1">// domain is a templated instance which indirects all observers of the
  36. </span><span class="c1"></span> <span class="c1">// status code to the pointed-to status code.
  37. </span><span class="c1"></span> <span class="c1">//
  38. </span><span class="c1"></span> <span class="c1">// Note that the status code returned&#39;s value type is a pointer, which
  39. </span><span class="c1"></span> <span class="c1">// by definition fits into `intptr_t` and is trivially copyable.
  40. </span><span class="c1"></span> <span class="c1">// Therefore `system_code` (which is also a type alias to
  41. </span><span class="c1"></span> <span class="c1">// `status_code&lt;erased&lt;intptr_t&gt;&gt;`) is happy to implicitly construct
  42. </span><span class="c1"></span> <span class="c1">// from the status code returned by `make_status_code_ptr()`.
  43. </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>
  44. <span class="p">}</span>
  45. </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>
  46. <p>We are now ready to use Experimental Outcome!</p>
  47. </div><p><small>Last revised: January 26, 2019 at 23:38:56 UTC</small></p>
  48. <hr>
  49. <div class="spirit-nav">
  50. <a accesskey="p" href="../../experimental/worked-example/source.html"><img src="../../images/prev.png" alt="Prev"></a>
  51. <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a>
  52. <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>
  53. </html>