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 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/constructor.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/message.html"><img src="../../images/next.png" alt="Next"></a></div><div id="content"> 13 <div class="titlepage"><div><div><h1 style="clear: both">String refs</h1></div></div></div> 14 <p><code><system_error2></code> does not use <code>std::string</code> to return possibly statically 15or dynamically allocated strings, and thus avoids dragging in a lot of the 16standard library which impacts build times.</p> 17 18<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>, 19which 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 20memory allocation using an atomic reference counter</a>. Due to this polymorphism, you don’t 21need to worry which implementation is actually in use under the bonnet 22when you pass around <code>string_ref</code> instances.</p> 23 24<p><code>string_ref</code> provides the same member functions as a <code>span<const char></code>, 25and so participates ordinarily in STL algorithms and containers. In 26particular, if you need to string search or slice it, you can construct a 27<code>string_view</code> on top easily.</p> 28 29<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 30</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 31</span><span class="c1"></span> <span class="p">{</span> 32 <span class="k">static</span> <span class="n">string_ref</span> <span class="n">v</span><span class="p">(</span><span class="s">"file i/o error domain"</span><span class="p">);</span> 33 <span class="k">return</span> <span class="n">v</span><span class="p">;</span> <span class="c1">// NOLINT 34</span><span class="c1"></span> <span class="p">}</span> 35</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> 36 37 38<p>Now you understand what <code>string_ref</code> does, returning the name of the 39status code domain is self describing. Note we use the non-managing 40constructor of <code>string_ref</code>, as the string <code>"file i/o error domain"</code> 41is statically stored. We cache the returned value locally in static 42storage.</p> 43 44 45 </div><p><small>Last revised: January 26, 2019 at 23:38:56 UTC</small></p> 46<hr> 47<div class="spirit-nav"> 48<a accesskey="p" href="../../experimental/worked-example/constructor.html"><img src="../../images/prev.png" alt="Prev"></a> 49 <a accesskey="u" href="../../experimental/worked-example.html"><img src="../../images/up.png" alt="Up"></a> 50 <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> 51</html> 52