string_ref.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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>String refs - 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/constructor.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/message.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content">
  12. <div class="titlepage"><div><div><h1 style="clear: both">String refs</h1></div></div></div>
  13. <p><code>&lt;system_error2&gt;</code> does not use <code>std::string</code> to return possibly statically
  14. or dynamically allocated strings, and thus avoids dragging in a lot of the
  15. standard library which impacts build times.</p>
  16. <p>Instead status code domains have a <a href="https://ned14.github.io/status-code/doc_status_code_domain.html#standardese-system_error2__status_code_domain__string_ref"><code>string_ref</code></a>,
  17. which has a polymorphic implementation which may or may not <a href="https://ned14.github.io/status-code/doc_status_code_domain.html#standardese-system_error2__status_code_domain__atomic_refcounted_string_ref">manage a dynamic
  18. memory allocation using an atomic reference counter</a>. Due to this polymorphism, you don&rsquo;t
  19. need to worry which implementation is actually in use under the bonnet
  20. when you pass around <code>string_ref</code> instances.</p>
  21. <p><code>string_ref</code> provides the same member functions as a <code>span&lt;const char&gt;</code>,
  22. and so participates ordinarily in STL algorithms and containers. In
  23. particular, if you need to string search or slice it, you can construct a
  24. <code>string_view</code> on top easily.</p>
  25. <div class="code-snippet"><div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"> <span class="c1">// Return the name of our custom code domain
  26. </span><span class="c1"></span> <span class="k">virtual</span> <span class="n">_base</span><span class="o">::</span><span class="n">string_ref</span> <span class="n">name</span><span class="p">()</span> <span class="k">const</span> <span class="k">noexcept</span> <span class="k">override</span> <span class="k">final</span> <span class="c1">// NOLINT
  27. </span><span class="c1"></span> <span class="p">{</span>
  28. <span class="k">static</span> <span class="n">string_ref</span> <span class="n">v</span><span class="p">(</span><span class="s">&#34;file i/o error domain&#34;</span><span class="p">);</span>
  29. <span class="k">return</span> <span class="n">v</span><span class="p">;</span> <span class="c1">// NOLINT
  30. </span><span class="c1"></span> <span class="p">}</span>
  31. </code></pre></div><a href="https://github.com/boostorg/outcome/tree/master/doc/src/snippets/experimental_status_code.cpp#L103" class="code-snippet-url" target="_blank">View this code on Github</a></div>
  32. <p>Now you understand what <code>string_ref</code> does, returning the name of the
  33. status code domain is self describing. Note we use the non-managing
  34. constructor of <code>string_ref</code>, as the string <code>&quot;file i/o error domain&quot;</code>
  35. is statically stored. We cache the returned value locally in static
  36. storage.</p>
  37. </div><p><small>Last revised: January 26, 2019 at 23:38:56 UTC</small></p>
  38. <hr>
  39. <div class="spirit-nav">
  40. <a accesskey="p" href="../../experimental/worked-example/constructor.html"><img src="../../images/prev.png" alt="Prev"></a>
  41. <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a>
  42. <a accesskey="h" href="../../index.html"><img src="../../images/home.png" alt="Home"></a><a accesskey="n" href="../../experimental/worked-example/message.html"><img src="../../images/next.png" alt="Next"></a></div></body>
  43. </html>