• 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>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
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/value_type.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/worked-example/string_ref.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
13  <div class="titlepage"><div><div><h1 style="clear: both">The constructor</h1></div></div></div>
14  <p>Code domains are 100% constexpr to construct and destruct, as are status codes.
15This enables the compiler to 100% instantiate both only in its mind, and to emit
16zero code and thus zero overhead.</p>
17
18<p>Unfortunately it also means that it must be possible for each domain to be instantiated an infinite
19number of times, and being 100% in constexpr, any instances never have a unique
20address in memory either. Thus we cannot compare domains for equivalence using
21their address in memory, as
22<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>
23 does.</p>
24
25<p>We solve this by using a <em>very</em> random 64 bit number taken from a hard random
26number 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>
27is strongly suggested as the source for this number.</p>
28
29<p>(In case you are wondering about the chance of collision for a 64 bit integer, SG14 estimated that
30approximately 190,000 separate domains would need to exist in a single process
31for there to be a 0.00000001% probability of collision if the random number
32source is very random)</p>
33
34<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
35</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.
36</span><span class="c1"></span>  <span class="c1">// Do NOT make up your own value. Do NOT use zero.
37</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>
38  <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>
39</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>
40
41
42<p>A nice side effect of this approach is that custom error domains in header-only
43libraries are safe, unlike custom <code>&lt;system_error&gt;</code> error categories. Boost.System&rsquo;s
44error categories can now opt into this same safe mechanism in order to also be
45safe in header only library use cases.</p>
46
47
48        </div><p><small>Last revised: January 26, 2019 at 23:38:56 UTC</small></p>
49<hr>
50<div class="spirit-nav">
51<a accesskey="p" href="../../experimental/worked-example/value_type.html"><img src="../../images/prev.png" alt="Prev"></a>
52    <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a>
53    <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>
54</html>
55