constructor.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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>The constructor - 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/value_type.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/worked-example/string_ref.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">The constructor</h1></div></div></div>
  13. <p>Code domains are 100% constexpr to construct and destruct, as are status codes.
  14. This enables the compiler to 100% instantiate both only in its mind, and to emit
  15. zero code and thus zero overhead.</p>
  16. <p>Unfortunately it also means that it must be possible for each domain to be instantiated an infinite
  17. number of times, and being 100% in constexpr, any instances never have a unique
  18. address in memory either. Thus we cannot compare domains for equivalence using
  19. their address in memory, as
  20. <a href="https://en.cppreference.com/w/cpp/error/error_category" class="api-reference" target="_blank"><i class="fa fa-book" aria-hidden="true"></i> <code>std::error_category</code></a>
  21. does.</p>
  22. <p>We solve this by using a <em>very</em> random 64 bit number taken from a hard random
  23. number source. The website <a href="https://www.random.org/cgi-bin/randbyte?nbytes=8&amp;format=h">https://www.random.org/cgi-bin/randbyte?nbytes=8&amp;format=h</a>
  24. is strongly suggested as the source for this number.</p>
  25. <p>(In case you are wondering about the chance of collision for a 64 bit integer, SG14 estimated that
  26. approximately 190,000 separate domains would need to exist in a single process
  27. for there to be a 0.00000001% probability of collision if the random number
  28. source is very random)</p>
  29. <div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"> <span class="c1">// unique id must be from a hard random number source
  30. </span><span class="c1"></span> <span class="c1">// Use https://www.random.org/cgi-bin/randbyte?nbytes=8&amp;format=h to get a hard random 64 bit id.
  31. </span><span class="c1"></span> <span class="c1">// Do NOT make up your own value. Do NOT use zero.
  32. </span><span class="c1"></span> <span class="k">constexpr</span> <span class="k">explicit</span> <span class="nf">_file_io_error_domain</span><span class="p">(</span><span class="k">typename</span> <span class="n">_base</span><span class="o">::</span><span class="n">unique_id_type</span> <span class="n">id</span> <span class="o">=</span> <span class="mh">0x230f170194fcc6c7</span><span class="p">)</span> <span class="k">noexcept</span> <span class="o">:</span> <span class="n">_base</span><span class="p">(</span><span class="n">id</span><span class="p">)</span> <span class="p">{}</span>
  33. <span class="k">static</span> <span class="kr">inline</span> <span class="k">constexpr</span> <span class="k">const</span> <span class="n">_file_io_error_domain</span> <span class="o">&amp;</span><span class="n">get</span><span class="p">();</span>
  34. </code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/experimental_status_code.cpp#L96" class="code-snippet-url" target="_blank">View this code on Github</a></div>
  35. <p>A nice side effect of this approach is that custom error domains in header-only
  36. libraries are safe, unlike custom <code>&lt;system_error&gt;</code> error categories. Boost.System&rsquo;s
  37. error categories can now opt into this same safe mechanism in order to also be
  38. safe in header only library use cases.</p>
  39. </div><p><small>Last revised: January 26, 2019 at 23:38:56 UTC</small></p>
  40. <hr>
  41. <div class="spirit-nav">
  42. <a accesskey="p" href="../../experimental/worked-example/value_type.html"><img src="../../images/prev.png" alt="Prev"></a>
  43. <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a>
  44. <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/worked-example/string_ref.html"><img src="../../images/next.png" alt="Next"></a></div></body>
  45. </html>