<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jared Miller on Medium]]></title>
        <description><![CDATA[Stories by Jared Miller on Medium]]></description>
        <link>https://medium.com/@jaredmil?source=rss-b54a8b7d894c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Y5k5_TIw2Fwz2G98ndIL0A.jpeg</url>
            <title>Stories by Jared Miller on Medium</title>
            <link>https://medium.com/@jaredmil?source=rss-b54a8b7d894c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 10:10:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jaredmil/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[C++ Beyond the Syllabus #11: Address Sanitizers]]></title>
            <link>https://jaredmil.medium.com/c-beyond-the-syllabus-11-address-sanitizers-9cc98d1e5b15?source=rss-b54a8b7d894c------2</link>
            <guid isPermaLink="false">https://medium.com/p/9cc98d1e5b15</guid>
            <category><![CDATA[cplusplus]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[memory-error]]></category>
            <category><![CDATA[c-plus-plus-language]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Jared Miller]]></dc:creator>
            <pubDate>Fri, 23 Aug 2024 12:37:46 GMT</pubDate>
            <atom:updated>2024-08-23T12:37:46.722Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>Protecting against undefined behavior.</em></h4><p><em>Not a Medium member? View this entire article </em><a href="https://jaredmil.medium.com/9cc98d1e5b15?source=friends_link&amp;sk=5be806016f6b51f0b7f3c722c69858c3"><em>here</em></a><em>!</em></p><p><em>This article is a part of the </em><a href="https://jaredmil.medium.com/free-links-to-all-c-beyond-the-syllabus-articles-259c26a682c8"><em>C++ Beyond the Syllabus</em></a><em> series. Subscribe </em><a href="https://jaredmil.medium.com/subscribe"><em>here</em></a><em> to receive each new issue directly in your inbox.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*DjoUB39cSIA10Gcb" /></figure><p>Generally speaking, catching undefined behavior and ensuring memory safety are crucial aspects of software engineering.</p><p>In C++, pointers, references, and non-owning objects like std::string_view and std::span all provide ample opportunity for programmers to introduce these unwanted behaviors into their applications.</p><p>Address sanitizers provide a robust solution to undefined behavior and memory safety by catching issues at runtime and throwing detailed error messages.</p><h3><strong>Address Sanitizers</strong></h3><h4><strong>What can it do?</strong></h4><p>Address sanitizers can generally catch a few kinds of bugs:</p><ul><li>Use-After-Free</li><li>Out of Bounds Accesses</li><li>Heap Overflow</li><li>Stack Overflow (depending on sanitizer being used)</li></ul><p>Just because an address sanitizer <em>can</em> catch certain types of errors, doesn’t mean it always will. It’s a powerful tool, but it’s not foolproof.</p><p>Let’s talk about how this tool works, and then get into why there might be false negatives.</p><h4><strong>How does it work?</strong></h4><p>The address sanitizer adds <em>shadow memory</em> to track the allocation state of each byte in your program.</p><p>For learning purposes, imagine a sanitizer object maintaining a boolean for each byte in the program’s address space representing whether that byte is valid to access. Any call which allocates memory on the heap (e.g. — via the new or malloc keywords) sets the boolean representing each byte to true. Any call that frees memory (e.g. — via the delete keyword), sets the corresponding booleans to false. Then, any code accessing dynamic memory will first verify with the sanitizer object that the bytes being accessed are currently valid.</p><p>The actual implementation is more complex, but this still illustrates a few important observations:</p><ul><li>An address sanitizer can only catch bugs in code that is compiled with <em>and</em> linked against the sanitizer. Any library code compiled separately cannot have its memory interactions tracked by the sanitizer.</li><li>The address sanitizer adds at least <em>O(n)</em> time and memory overhead to any program because it will require extra work for each memory allocation, access, and deallocation. You should expect an address sanitizer to slow down your program 2–3x.</li></ul><h4><strong>Are there other shortcomings?</strong></h4><p>There are a few:</p><ul><li><em>Performance Overhead</em>… As mentioned above, an address sanitizer will make your code 2–3x slower. Moreover, in order to get very informative stack traces in sanitizer-generated error messages, you’ll need to manually disable a handful of compiler optimizations. (See more on this in the “How do I use it?” section below.)</li><li><em>Increased Memory Usage</em>… By keeping shadow memory to track byte initialization statuses, up to <em>O(n)</em> extra memory may be used, where <em>n</em> is the amount of memory your application allocates. This can be problematic for large applications with strict memory requirements.</li><li><em>False Negatives</em>… While the documentation states there will rarely be false positives, false negatives can be quite frequent. This is especially the case for memory access errors on the stack and when certain compiler optimizations are enabled.</li></ul><h4><strong>How do I use it?</strong></h4><p>Address sanitizers come built into modern versions of both LLVM/Clang and GCC compilers.</p><p>Using the sanitizer is actually quite simple, just add the compiler flag -fsanitize=address (same regardless of compiler). This will get you some bare bones sanitizing, but a few additional flags can help avoid some of the aforementioned shortcomings and generate more easily readable error messages:</p><ul><li>-g compiles debug information into executables, allowing error messages to include line numbers, variable names and function names.</li><li>-fno-omit-frame-pointer will prevent the compiler from optimizing away the frame pointer, which could hinder the accuracy of stack traces.</li><li>-O1 will prevent the compiler from rearranging code to maximize optimizations. With greater optimizations enabled (e.g. — -O2 or -O3), the rearranged code could mask real errors from the sanitizer’s memory checks.</li><li>-fno-optimize-sibling-calls prevents the compiler from performing <a href="https://www.geeksforgeeks.org/tail-call-optimisation-in-c/">tail call optimization (TCO)</a>, which helps the sanitizer accurately display the call stack leading to an error.</li></ul><p>With these compiler options, you’ll easily be able to compile and build executables with address sanitizing enabled.</p><p>Let’s take another look at the following example code from <a href="https://jaredmil.medium.com/89df5ba6b25e?sk=adc91ab1a8832c497fec2f5dcef3d28e">C++ Beyond The Syllabus #9: </a><a href="https://jaredmil.medium.com/89df5ba6b25e?sk=adc91ab1a8832c497fec2f5dcef3d28e">std::string_view</a>:</p><pre>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>#include &lt;string_view&gt;<br><br>std::string_view generate_string()<br>{<br>  const std::string temp = &quot;Temporary String&quot;;<br>  std::string_view temp_view_1(temp.data(), temp.size());<br>  std::cout &lt;&lt; &quot;temp_view_1: &quot; &lt;&lt; temp_view_1 &lt;&lt; &quot;\n&quot;;<br>  return temp_view_1;<br>}<br><br>int main()<br>{<br>  std::string_view temp_view_2 = generate_string();<br>  std::cout &lt;&lt; &quot;temp_view_2: &quot; &lt;&lt; temp_view_2;<br>}</pre><p>I noted in that article how the printed value of temp_view_2 was undefined:</p><pre>temp_view_1: Temporary String<br>temp_view_2: C��Oaq�</pre><p>This is an example of a common error that occurs when a pointer, reference, std::string_view, or std::span outlives the underlying data.</p><p>An address sanitizer could have saved us here!</p><p>Simply <a href="https://godbolt.org/#g:!((g:!((g:!((g:!((h:codeEditor,i:(filename:&#39;1&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,selection:(endColumn:1,endLineNumber:4,positionColumn:1,positionLineNumber:4,selectionStartColumn:1,selectionStartLineNumber:4,startColumn:1,startLineNumber:4),source:&#39;%23include+%3Ciostream%3E%0A%23include+%3Cstring%3E%0A%23include+%3Cstring_view%3E%0A%0Astd::string_view+generate_string()%0A%7B%0A%09const+std::string+temp+%3D+%22Temporary+String%22%3B%0A%09std::string_view+temp_view_1(temp.data()%0A++++++++++++++++++++++++++++++++,+temp.size())%3B%0A%09std::cout+%3C%3C+%22temp_view_1:+%22+%3C%3C+temp_view_1+%3C%3C+%22%5Cn%22%3B%0A%09return+temp_view_1%3B%0A%7D%0A%0Aint+main()+%7B%0A++++std::string_view+temp_view_2+%3D+generate_string()%3B%0A%09std::cout+%3C%3C+%22temp_view_2:+%22+%3C%3C+temp_view_2%3B%0A%7D&#39;),l:&#39;5&#39;,n:&#39;1&#39;,o:&#39;C%2B%2B+source+%231&#39;,t:&#39;0&#39;)),k:50,l:&#39;4&#39;,m:65.6319290465632,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;),(g:!((h:executor,i:(argsPanelShown:&#39;1&#39;,compilationPanelShown:&#39;0&#39;,compiler:clang1400,compilerName:&#39;&#39;,compilerOutShown:&#39;0&#39;,execArgs:&#39;&#39;,execStdin:&#39;&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,libs:!(),options:&#39;-std%3Dc%2B%2B20&#39;,overrides:!(),runtimeTools:!(),source:1,stdinPanelShown:&#39;1&#39;,tree:0,wrap:&#39;0&#39;),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;Executor+x86-64+clang+14.0.0+(C%2B%2B,+Editor+%231)&#39;,t:&#39;0&#39;)),header:(),l:&#39;4&#39;,m:34.3680709534368,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),k:45.23676880222841,l:&#39;3&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;),(g:!((h:executor,i:(argsPanelShown:&#39;1&#39;,compilationPanelShown:&#39;0&#39;,compiler:g141,compilerName:&#39;&#39;,compilerOutShown:&#39;0&#39;,execArgs:&#39;&#39;,execStdin:&#39;&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:2,lang:c%2B%2B,libs:!(),options:&#39;-std%3Dc%2B%2B20+-fsanitize%3Daddress+-g+-O1+-fno-omit-frame-pointer+-fno-optimize-sibling-calls&#39;,overrides:!(),runtimeTools:!(),source:1,stdinPanelShown:&#39;1&#39;,wrap:&#39;1&#39;),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;Executor+x86-64+gcc+14.1+(C%2B%2B,+Editor+%231)&#39;,t:&#39;0&#39;)),header:(),k:54.76323119777159,l:&#39;4&#39;,m:99.99999999999999,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),l:&#39;2&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;)),version:4">plugging this example into Compiler Explorer</a> with the aforementioned compiler options will catch the bug:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*iLcpCYIAL8b1o4sE" /><figcaption>Annotated Compiler Explorer example of Address Sanitizer catching heap-use-after-free error.</figcaption></figure><p>On the bottom left, we see the erroneous output with no address sanitizer in use. On the right, we see the address sanitizer identified a heap-use-after-free error and threw a runtime error including three detailed stack traces: One of the bad access, one of the original allocation of the accessed memory, and one of the freeing of the accessed memory.</p><h3><strong>Address Sanitizers in Practice</strong></h3><p>Slowing down your code 2–3x is pretty drastic… and C++ is supposed to be fast, right?</p><p>The good news is address sanitizers can be useful without slowing down your production releases.</p><p>The simplest way to start using address sanitizers today is to build your unit and integration tests with the necessary compiler options. (Just be sure not to use a sanitizer for any <a href="https://jaredmil.medium.com/c-beyond-the-syllabus-2-intro-to-benchmarking-and-macro-benchmarking-deep-dive-a465fc73a604?sk=cb14ece9c861a3bdf6b963693d550beb">benchmarking tests</a>!)</p><p>Beyond that, adding an address sanitizer to your continuous integration (CI) pipeline can be a good way to enforce team-wide adoption.</p><h3><strong>What’s Next?</strong></h3><p>If you liked this article, please consider <em>clapping</em> and following!</p><p>Subscribe <a href="https://jaredmil.medium.com/subscribe">here</a> to receive weekly-ish issues of <em>C++ Beyond the Syllabus</em> directly in your inbox :)</p><h3><strong>Sources &amp; More Info</strong></h3><ul><li><a href="https://www.youtube.com/watch?v=cUvdtLTJeec">Jason Turner’s C++ Weekly — Ep 425 — Using string_view, span, and Pointers Safely!</a></li><li><a href="https://releases.llvm.org/11.0.1/tools/clang/docs/AddressSanitizer.html">LLVM docs: Clang AddressSanitizer</a></li><li><a href="https://github.com/google/sanitizers/wiki/AddressSanitizer">Google’s AddressSanitizer (ASan) Git Repo</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9cc98d1e5b15" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[C++20 std::span Explained]]></title>
            <link>https://jaredmil.medium.com/c-20-std-span-explained-db82d7e9d4f8?source=rss-b54a8b7d894c------2</link>
            <guid isPermaLink="false">https://medium.com/p/db82d7e9d4f8</guid>
            <category><![CDATA[cplusplus20]]></category>
            <category><![CDATA[standard-template-library]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[c-plus-plus-language]]></category>
            <dc:creator><![CDATA[Jared Miller]]></dc:creator>
            <pubDate>Thu, 15 Aug 2024 12:17:38 GMT</pubDate>
            <atom:updated>2024-08-15T12:17:38.721Z</atom:updated>
            <content:encoded><![CDATA[<h3>C++ Beyond the Syllabus #10: C++20 std::span</h3><h4><em>Efficient, non-owning contiguous memory access.</em></h4><p><em>Not a Medium member? View this entire article </em><a href="https://jaredmil.medium.com/db82d7e9d4f8?source=friends_link&amp;sk=d6e36e4e26b4b7f4abedc582b9a56009"><em>here</em></a><em>!</em></p><p><em>This article is a part of the </em><a href="https://jaredmil.medium.com/free-links-to-all-c-beyond-the-syllabus-articles-259c26a682c8"><em>C++ Beyond the Syllabus</em></a><em> series. Subscribe </em><a href="https://jaredmil.medium.com/subscribe"><em>here</em></a><em> to receive each new issue directly in your inbox.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*HZEYQVhaP0JmDNoZ" /></figure><p>Introduced in C++20, std::span provides a lightweight, non-owning, and modifiable reference to contiguously stored data.</p><p>The use case is simple:</p><blockquote>std::span can drastically simplify code by acting as a uniform interface to operate on different representations of contiguous sequences of elements.</blockquote><p>For example, you can write a single function to operate on a C-style array, std::array, std::vector, and std::string.</p><h3><strong>How’s It Work?</strong></h3><p>std::span is actually very similar to std::string_view (with a few differences).</p><h4><strong>Like `std::string_view`</strong></h4><p>Discussed in <a href="https://jaredmil.medium.com/89df5ba6b25e?sk=adc91ab1a8832c497fec2f5dcef3d28e">this article</a>, std::string_view is optimized for read-only string operations. It can only be used on char arrays and does not allow the programmer to directly modify the underlying data.</p><p>std::span is a little different. It’s a template, so it can be used with any data type, not just chars. std::span also allows for modification of the underlying data.</p><p>Both std::string_view and std::span provide <em>non-owning</em> references, so you’ll need to be sure std::span doesn’t outlive the underlying data.</p><p>They are actually implemented very similarly as well. Just like std::string_view, std::span consists of a pointer to the first item and the size of the sequence.</p><h4><strong>Constructing `std::span`</strong></h4><p>The constructors are pretty versatile. You can construct a std::span…</p><ul><li>from a C-style array</li></ul><pre>int c_arr[] = { 1, 2, 3};<br>std::span&lt;int&gt; c_arr_span( c_arr );</pre><ul><li>directly from std::array or std::vector</li></ul><pre>std::vector&lt;char&gt; vec = { &#39;a&#39;, &#39;b&#39;, &#39;c&#39; };<br>std::span&lt;char&gt; vec_span( vec );</pre><ul><li>with iterators to the front and end of a sequence</li></ul><pre>std::array&lt;double, 3&gt; arr = { 0.1, 0.2, 0.3 };<br>std::span&lt;double&gt; arr_span( arr.begin(), arr.end() );</pre><ul><li>with a pointer to the first element and the size</li></ul><pre>char* c_str = new char[]{ &#39;h&#39;, &#39;e&#39;, &#39;l&#39;, &#39;l&#39;, &#39;o&#39;, &#39;\n&#39;};<br>std::span&lt;char&gt; c_str_span( c_str, strlen(c_str) );<br>// ...<br>delete[] c_str; // Note: Usage of `c_str_span` after this<br>                // line would access undefined memory!</pre><p>Here’s a <a href="https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QAJgAMfAQQCqAZ0wAFAB6cpvAFYgupWkwahkAUgkAhcxdLL6yAngGVG6AMKpaAVxYNJpVwAZPAZMADkfACNMYkkJUgAHVEVCJwYPb19/JJTHAWDQiJZo2Il4u0wHNKECJmICDJ8/csx7PIYauoIC8KiYuNta%2Bsas8qHukN7i/rKASltUL2Jkdg5zAGYQ5G8sAGpTdbc64iYATwPsUykAQQ2tncx9w6wARy9MC6vbiU2Gba89gc3IoEkZPjc7n8Hk9gQRiCFgAA6BDg76/f6Aw4AN0qRGIqK%2BIQIuxYTBCEFm%2BwA7FYbrt6bsibtkAB9Y6mACsVg5ABEnnzTDTdvpdvFdutSLsACySjnUnkHWnXBm7RQEdAgEAgsGHIkXZls4jEFnahgQA3HXazRX7CF0hlqjUgHEOEhA5AIOr6l386kWXZgDhMQOSwOREMBjjIQPyxVfFWOzWm92e/HrbC7F0m0FmzOVK1xu3Kh3qzXHM5A9CLSL0SXrfWWg4CoVSREi1ti1vrWPrJUJ0tanOV6v0BtG7NGc3HRHRYDk%2Ba7aeuCkF3tfeMMj11ABUBrVxF9oQA7szU5zuQqhYGEBHA5hbxxaA%2Bn7xI6gH5y3AxA4KFWv7fSiaDjqbhbmmGasvuE65pBcKSvu9BmrBxCUta/7FvSWD0AQHxcpyfLITaAD0RG7GEqA4SAuxKEwwCPKgVD7AAbFIyHQaYLGLlQOEHgQCB4IoG4qsJIkMiRolHostDoIuyArIoii7F4DBYDQoQyWwLAkKcYBgOuEJUjyHDzLQnAcrwfjcLwqCcG41jWKqizLI8Gw8KQBCaMZ8wANYgByMimRwUq8CwehSDIllaKQNkcLwiggDIHkcFo8xwLAMCICAmBqJUXh4uQlB1MAigqEYrRCAgqBHpZbloCwCR0Ew7SlaEtAVVVkW8HVDX0LE2xGMAXBSuFpDdXQMRhKwqxdag9XjcQADyeXtdVnkBDlyDXMQxWcLw2WVDU%2BCWbw/CCCIYjsNIsiCEoqgaMlOh6AYA0oPZlgGHgkTxZA8yoAk7TxRwAC0jpNmYljWNIuxAwtUWoDiRp4Fg30UqQxDKY4bAACqoJ4KPzIoTkrE9johC15WVatVnucQmDsDIR4nAknkmWZFlrTF2AbXlJC7GoAAcTFA0xUrMoYxjClKiKtlIuwQHZEPvbsuCELzrmzLwSUpfMCCYEwWCxKjvn%2BQYnDBaQoU/Ii/PrByACc0hcFSVLrDb9vxJ10W7bYCXuSzpBpZlY29QVEDB/0/XGENI00LQPHxRAkRrZEIR1KcnBuSnzDEKcC2RNouIZzNLBsIIC0MLQ6cPaQWCRF4wBHLQtCA25WCksY4jV/gtNVDigNRftyB5asblEq0a20J9Jw5x4WBrXCeChdTCORMkmA8pg7fAJPxj%2B1QhjFQAangmBHgtCSMEX13CKI4hXadCjKOoa26Po4smG9NiT198C/f9aSAxBuqMGn8oYwzhgjeEyNf62FaLiNILgVIjD8PoIIkwiglCejkVIAhkFYOSDghgPQMH9H0BUKoAhOjDE8E0EmcCKEdHGMQvosQyHjDwWwrozDpisIJkTdgXBWYcHMqQT2MU%2BaC2FqLSOwBJbS2lnLFWeJ9g/EEZrFmOs9YG0oEI82oUqRMTbCLKQVI7b8zKExLgItREc29vFRKGjSDGwCpwdY7Nq4xXUQ9WYQiJDuLht7LWXlSAIxSM4KUQA%3D">Compiler Explorer link</a> with the above examples.</p><h4><strong>Accessing Elements</strong></h4><p>Just like a std::vector or array, you can access elements in a std::span via the bracket operator ([]) for unchecked access. This means if you access an index greater than the size of the span (or the underlying data), you’ll get undefined behavior.</p><p>In C++26, support will be added for the std::span::at function, which will include a bounds check for further safety, just like std::vector::at. Like its std::vector counterpart, this check would really just cause unnecessary overhead <em>if</em> your surrounding code already ensures only valid indexes will be accessed.</p><p>Here’s a snippet showing how each of these accesses might be used:</p><pre>#include &lt;span&gt;<br>#include &lt;iostream&gt;<br><br>int main()<br>{<br>  std::array&lt;int, 3&gt; arr = { 1, 2, 3 };<br>  std::span&lt;int&gt; s( arr );<br><br>  for ( size_t i = 0; i &lt; s.size(); ++i )<br>  {<br>    std::cout &lt;&lt; s[ i ] &lt;&lt; &quot; &quot;; // unchecked access<br>  }<br>  std::cout &lt;&lt; std::endl;<br>  <br>  try<br>  {<br>    std::cout &lt;&lt; s.at( 3 ) &lt;&lt; std::endl; // bounds-checked access<br>  }<br>  catch ( const std::out_of_range&amp; /*e*/ )<br>  {<br>    std::cout &lt;&lt; &quot;Out of range!&quot; &lt;&lt; std::endl;<br>  }<br>}</pre><h4><strong>Subspans</strong></h4><p>Like std::string’s sub<em>string</em> functionality, std::span provides sub<em>span</em> functionality. This can be especially useful when implementing algorithms where you may need to traverse small portions of a sequence.</p><pre>#include &lt;span&gt;<br>#include &lt;iostream&gt;<br><br>int main()<br>{<br>  std::array&lt;int, 5&gt; arr = { 1, 2, 3, 4, 5 };<br>  std::span&lt;int&gt; arr_span( arr );<br>  <br>  // create subspan of size 3 beginning at index 1<br>  std::span&lt;int&gt; subspan = arr_span.subspan( 1, 3 );<br>  <br>  for ( const auto i: subspan )<br>  {<br>    std::cout &lt;&lt; i &lt;&lt; &quot; &quot;;<br>  }<br>}</pre><p>This would output:</p><pre>2 3 4</pre><p>You can also use the std::span::subspan::first(x) or std::span::subspan::last(x) helper functions to retrieve subspans of the first or last x elements.</p><h4><strong>Some Other Properties</strong></h4><p>Similar to STL containers, std::span provides (or will soon provide in C++23,26) many member functions to help operate on the underlying data. Though, note that a std::span itself is not a container because it doesn’t really own anything!</p><p>You can access the underlying data directly via the std::span::data function. Beginning with C++26, iterators will be supported for std::span, which will allow them to be used with a variety of STL algorithms, like std::sort, std::binary_search, bounds functions, and many more.</p><p>Just like all other C++ types, you can create const spans, span references, and span pointers as well.</p><h4><strong>Multi-Dimensional Spans</strong></h4><p>These aren’t likely to be used as frequently as std::span for most C++ programmers, but it’s worth noting that std::mdspan allows you to view, access, and operate on contiguous sequences of memory as if it were multi dimensional. I’m not going to dive deep into this today, but it’s good to know that this exists.</p><h3><strong>Disclaimers</strong></h3><p>std::span is non-owning. The side effects of this can lead to undefined behavior and unexpected crashes if you are not careful. I provided an in-depth overview of this disclaimer in <a href="https://jaredmil.medium.com/89df5ba6b25e?sk=adc91ab1a8832c497fec2f5dcef3d28e">my </a><a href="https://jaredmil.medium.com/89df5ba6b25e?sk=adc91ab1a8832c497fec2f5dcef3d28e">std::string_view article</a>, but here are the (much abbreviated) takeaways:</p><ul><li>You must make sure the underlying data outlives all std::spans created upon that data.</li><li>Unless you are very careful, you probably want to avoid std::span in multi-threaded environments.</li><li>Since growth beyond the current capacity of any std::string or std::vector will lead to reallocation and copying the underlying data to a different place in memory, any append operations on those objects should be expected to invalidate any std::spans referencing their underlying data.</li></ul><p>Once a std::span is created, it cannot be resized. You can always create a subspan of smaller size, but you will not be able to safely increase the size.</p><h3><strong>The Main Use Case</strong></h3><p>Large C++ applications often include various libraries, input sources, and other logic, which employ several different representations of contiguous data. You can simplify libraries and consolidate code by using std::span to uniformly view and operate on any of those representations.</p><h3><strong>What’s Next?</strong></h3><p>If you liked this article, please consider <em>clapping</em> and following!</p><p>Subscribe <a href="https://jaredmil.medium.com/subscribe">here</a> to receive weekly-ish new issues of <em>C++ Beyond the Syllabus</em> directly in your inbox.</p><h3><strong>Sources &amp; More Info</strong></h3><ul><li><a href="https://en.cppreference.com/w/cpp/container/span">std::span C++ Docs</a></li><li><a href="https://www.youtube.com/watch?v=cUvdtLTJeec">Jason Turner’s C++ Weekly — Ep 425 — Using string_view, span, and Pointers Safely!</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=db82d7e9d4f8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[C++ std::string_view Explained]]></title>
            <link>https://jaredmil.medium.com/c-std-string-view-explained-89df5ba6b25e?source=rss-b54a8b7d894c------2</link>
            <guid isPermaLink="false">https://medium.com/p/89df5ba6b25e</guid>
            <category><![CDATA[string]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[c-plus-plus-language]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[cplusplus]]></category>
            <dc:creator><![CDATA[Jared Miller]]></dc:creator>
            <pubDate>Thu, 08 Aug 2024 12:52:32 GMT</pubDate>
            <atom:updated>2025-01-09T13:11:38.534Z</atom:updated>
            <content:encoded><![CDATA[<h3>C++ Beyond the Syllabus #9: std::string_view</h3><h4>The modern C++ solution for read-only access of string data.</h4><p><em>Not a Medium member? View this entire article </em><a href="https://jaredmil.medium.com/89df5ba6b25e?source=friends_link&amp;sk=adc91ab1a8832c497fec2f5dcef3d28e"><em>here</em></a><em>!</em></p><p><em>This article is a part of the </em><a href="https://jaredmil.medium.com/free-links-to-all-c-beyond-the-syllabus-articles-259c26a682c8"><em>C++ Beyond the Syllabus</em></a><em> series. Subscribe </em><a href="https://jaredmil.medium.com/subscribe"><em>here</em></a><em> to receive each new issue directly in your inbox.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*auwbGNjEe6nCVJl3" /></figure><p>You’re probably familiar with both C-strings and C++’s std::string. You might be wondering why we need yet <em>another</em> string type. The answer is that std::string_view isn’t really another string type — it doesn’t actually <em>own</em> any string-like state.</p><h3><strong>A Quick Recap</strong></h3><h4><strong>C-Strings</strong></h4><p>C-strings are null-terminated (i.e. — end with ‘\0’) arrays of characters used in both C and C++. These require careful management to avoid common issues like buffer overflows.</p><p>C-strings can be allocated pretty much anywhere depending on how they are defined…</p><pre>const char* str_literal = &quot;Hello!&quot;; // Allocated in read-only static memory<br>char stack_str[100];                // Allocates memory on the stack<br>char* heap_str = new char[100];     // Allocates memory on the heap</pre><p>If you use the new keyword to allocate a C-string (char array) on the heap, you must remember the delete[] keyword as well.</p><p>The only way to know the size of a C-string without caching it somewhere is by traversing until you come across the null-character.</p><h4><strong>std::string</strong></h4><p>The C++ STL introduced std::string to provide safer and more convenient string usage.</p><p>std::string maintains a C-string under the hood along with a ton of helper functions to help you efficiently operate on it.</p><p>For very short strings, std::string uses small string optimization (SSO) and avoids heap allocation, but this is very much the exception.</p><h4><strong>Why don’t these suffice?</strong></h4><p>Well, a lot of applications work with both C-strings and std::strings. Even when an application uses a single string representation throughout, it may use libraries that expect a different string type, requiring either redundant library code or unnecessary conversions and copies of data.</p><p>Prior to std::string_view, code had three main options to be compatible with both string types:</p><ol><li><em>Duplicate all logic</em>: For any code operating on std::string, write equivalent code for C-strings. This doesn’t even work all of the time — it falls apart (or becomes far more convoluted), when you need both string types to be represented in a container.</li><li><em>Handle all </em><em>std::strings as C-strings</em>: We <em>can</em> efficiently grab the underlying C-string from any std::string object, but then we’re effectively disregarding all of the great features that come with std::string.</li><li><em>Convert all C-strings to </em><em>std::strings</em>: This requires traversing the entire C-string (which can be arbitrarily long!) to find the size, then allocating the memory to store it within a new std::string, and then traversing the entire C-string again to copy its contents over to the std::string! Needless to say this option won’t suffice in any performance-sensitive application.</li></ol><h3><strong>Introducing std::string_view</strong></h3><p>Hopefully now you’re onboard with the need for a new way to efficiently handle strings of either type.</p><p>Unfortunately for us, we can’t have it all…</p><p>std::string is the C++ mechanism for making string manipulation safe and convenient — that already exists. If we want to <em>manipulate</em> strings of type C-string and std::string in a uniform way, we’re still going to have to convert all C-strings to std::strings or vice versa.</p><p>What we <em>can</em> do, however, is create an efficient <em>read-only</em> wrapper around C-strings, which can be used for std::string’s underlying C-string as well.</p><h4><strong>Designing std::string_view</strong></h4><p>If you were to design such a wrapper, how would you do it?</p><p>Here are a few observations/requirements to get us started. We want…</p><ul><li>to make initializing the wrapper efficient.</li><li>instances of the wrapper to have a small memory footprint.</li><li>the wrapper to be capable of performing all of the same read-only operations as std::string.</li></ul><p>All of these requirements are actually pretty simple to achieve.</p><p>The std::string_view implementation only has two member variables…</p><ul><li>const char* pointing to the first character</li><li>size_t representing the size of the string</li></ul><p>Together, these members require 16 bytes of memory (or 8B on a 32-bit system), which is pretty small in the grand scheme of things.</p><p>std::string_view also provides nearly all of the <a href="https://en.cppreference.com/w/cpp/string/basic_string_view">read-only helper functions</a> that std::string provides, enabling convenient comparison, substring, and hashing functionality.</p><h4><strong>Constructing std::string_view</strong></h4><p>There are many constructors, but there are two that I think are worthy of a shoutout:</p><ol><li>constexpr string_view( const CharT* start, size_type size );<br> <br>This constructor is trivial. You pass in a potentially-const char* and string size (not including the null character). The time complexity to construct the std::string_view is then constant because the member variables can be directly initialized with these arguments.<br> <br>This is generally the constructor you’d use to build a std::string_view from a std::string because you’ll already have a pointer to the first character (std::string::data()) and the size (std::string::size()). If you happen to be storing the size of a C-string, you can use this constructor for those too.</li><li>constexpr string_view( const CharT* start );<br> <br>This is generally the constructor you’ll use when building a std::string_view from a C-string because C-strings do not cache their size. This constructor has linear time complexity as it must traverse the C-string until it finds a null character.</li></ol><p>Notably, there is no constructor with a std::string parameter. This is presumably because implicit construction of a std::string_view from a std::string would lead to confusing scenarios where the programmer is unsure if an owning or non-owning object was created. We’ll touch on other gotchas regarding ownership in the next section.</p><h3><strong>What’s the catch?</strong></h3><p>This isn’t the end-all-be-all future of string manipulation in C++. There are actually quite a few drawbacks and reasons not to use std::string_view.</p><h4><strong>They Are Non-Owning</strong></h4><p>If you use them right, this is a good thing. The fact that std::string_view does exactly what its name suggests (i.e. — view strings), as opposed to owning them, makes them an ultra-light weight way to view both C-strings and std::strings.</p><p>This same property can cause some hiccups, though. What do you suspect the following example will output?</p><pre>#include &lt;iostream&gt;<br>#include &lt;string&gt;<br>#include &lt;string_view&gt;<br><br>std::string_view generate_string()<br>{<br>  const std::string temp = &quot;Temporary String&quot;;<br>  std::string_view temp_view_1(temp.data(), temp.size());<br>  std::cout &lt;&lt; &quot;temp_view_1: &quot; &lt;&lt; temp_view_1 &lt;&lt; &quot;\n&quot;;<br>  return temp_view_1;<br>}<br><br>int main()<br>{<br>  std::string_view temp_view_2 = generate_string();<br>  std::cout &lt;&lt; &quot;temp_view_2: &quot; &lt;&lt; temp_view_2;<br>}</pre><p>Let’s walk through it.</p><p>The std::string_view temp_view_1 is created atop of the <em>local</em> string temp. When generate_string returns temp_view_1, the underlying data goes out of scope!</p><p>If we run <a href="https://godbolt.org/#g:!((g:!((g:!((g:!((h:codeEditor,i:(filename:&#39;1&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,selection:(endColumn:34,endLineNumber:15,positionColumn:34,positionLineNumber:15,selectionStartColumn:34,selectionStartLineNumber:15,startColumn:34,startLineNumber:15),source:&#39;%23include+%3Ciostream%3E%0A%23include+%3Cstring%3E%0A%23include+%3Cstring_view%3E%0A%0Astd::string_view+generate_string()%0A%7B%0A%09const+std::string+temp+%3D+%22Temporary+String%22%3B%0A%09std::string_view+temp_view_1(temp.data(),+temp.size())%3B%0A%09std::cout+%3C%3C+%22temp_view_1:+%22+%3C%3C+temp_view_1+%3C%3C+%22%5Cn%22%3B%0A%09return+temp_view_1%3B%0A%7D%0A%0Aint+main()+%7B%0A++++std::string_view+temp_view_2+%3D+generate_string()%3B%0A%09std::cout+%3C%3C+%22temp_view_2:+%22+%3C%3C+temp_view_2%3B%0A%7D&#39;),l:&#39;5&#39;,n:&#39;1&#39;,o:&#39;C%2B%2B+source+%231&#39;,t:&#39;0&#39;)),k:28.696820966819853,l:&#39;4&#39;,m:60.97560975609756,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;),(g:!((h:executor,i:(argsPanelShown:&#39;1&#39;,compilationPanelShown:&#39;0&#39;,compiler:clang1400,compilerName:&#39;&#39;,compilerOutShown:&#39;0&#39;,execArgs:&#39;&#39;,execStdin:&#39;&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,libs:!(),options:&#39;-std%3Dc%2B%2B20+-O0&#39;,overrides:!(),runtimeTools:!(),source:1,stdinPanelShown:&#39;1&#39;,tree:0,wrap:&#39;0&#39;),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;Executor+x86-64+clang+14.0.0+(C%2B%2B,+Editor+%231)&#39;,t:&#39;0&#39;)),header:(),l:&#39;4&#39;,m:39.02439024390244,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),k:52.83746556473831,l:&#39;3&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;),(g:!((h:compiler,i:(compiler:clang1400,filters:(b:&#39;0&#39;,binary:&#39;1&#39;,binaryObject:&#39;1&#39;,commentOnly:&#39;0&#39;,debugCalls:&#39;1&#39;,demangle:&#39;0&#39;,directives:&#39;0&#39;,execute:&#39;1&#39;,intel:&#39;0&#39;,libraryCode:&#39;1&#39;,trim:&#39;1&#39;,verboseDemangling:&#39;0&#39;),flagsViewOpen:&#39;1&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,libs:!(),options:&#39;-std%3Dc%2B%2B20+-O0&#39;,overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;+x86-64+clang+14.0.0+(Editor+%231)&#39;,t:&#39;0&#39;)),k:47.16253443526172,l:&#39;4&#39;,m:100,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),l:&#39;2&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;)),version:4">this example in Compiler Explorer</a>, we unsurprisingly see that temp_view_2 is observing some garbage, undefined memory:</p><pre>temp_view_1: Temporary String<br>temp_view_2: C��Oaq�</pre><p>As the example above illustrates, std::string_view is similar to an lvalue reference or pointer in the sense that you need to make sure the underlying data outlives the view. If not, any accesses of the std::string_view will read undefined memory.</p><p>Likewise, std::string can grow in size (similar to std::vector). If the size of a std::string outgrows its capacity, a new, larger section of memory will be allocated and all of the underlying data will be copied to the new location. This will invalidate any std::string_view that pointed to the first character of the std::string before it grew. Any future accesses to such a std::string_view will read undefined memory.</p><p>The fix is simple:</p><blockquote>Your code must ensure the lifetime of a std::string_view never outlives the underlying data it was constructed from.</blockquote><h4><strong>When Not To Use</strong></h4><p>There are a few clear scenarios outlining when not to use std::string_view:</p><ol><li>If your application only works with std::strings, const std::string&amp; is always more space-efficient than std::string_view as the single reference will be half the size of a std::string_view.</li><li>When the lifetime of the underlying data is unknown. For example, you may not want to use std::string_view to represent string-like data returned from a function or when processing string data across multiple threads.</li></ol><h3><strong>Wrapping Up</strong></h3><p>Large C++ applications often include various libraries, input sources, and other logic, which employ both C-string and std::string representations of string-like data. std::string_view is the go-to modern C++ solution for read-only access of string data in these applications.</p><h3><strong>What’s Next?</strong></h3><p>If you liked this article, please consider <em>clapping</em> and following!</p><p>Subscribe <a href="https://jaredmil.medium.com/subscribe">here</a> to receive weekly-ish new issues of <em>C++ Beyond the Syllabus</em> directly in your inbox.</p><h3><strong>Sources &amp; More Info</strong></h3><ul><li><a href="https://en.cppreference.com/w/cpp/string/basic_string_view">std::basic_string_view C++ Docs</a></li><li><a href="https://godbolt.org/#g:!((g:!((g:!((g:!((h:codeEditor,i:(filename:&#39;1&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,selection:(endColumn:23,endLineNumber:2,positionColumn:23,positionLineNumber:2,selectionStartColumn:23,selectionStartLineNumber:2,startColumn:23,startLineNumber:2),source:&#39;%23include+%3Cstring%3E%0A%23include+%3Cstring_view%3E%0A%23include+%3Ciostream%3E%0A%0Aint+main()%0A%7B%0A++++const+char+c_str%5B%5D+%3D+%22Hello!!%22%3B%0A++++std::string+cpp_str+%3D+c_str%3B%0A%0A++++char*+c_str_2+%3D+new+char%5B%5D+%7B%22Hello+From+The+Heap!!%22%7D%3B%0A++++std::string+cpp_str_2+%3D+c_str_2%3B%0A%0A++++const+char*+c_str_3+%3D+cpp_str_2.data()%3B%0A%0A++++delete%5B%5D+c_str_2%3B%0A%0A++++std::string_view+sv(c_str_2)%3B%0A++++std::cout+%3C%3C+sizeof(sv)%3B%0A%7D&#39;),l:&#39;5&#39;,n:&#39;1&#39;,o:&#39;C%2B%2B+source+%231&#39;,t:&#39;0&#39;)),k:28.696820966819853,l:&#39;4&#39;,m:60.97560975609756,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;),(g:!((h:executor,i:(argsPanelShown:&#39;1&#39;,compilationPanelShown:&#39;0&#39;,compiler:clang1400,compilerName:&#39;&#39;,compilerOutShown:&#39;0&#39;,execArgs:&#39;&#39;,execStdin:&#39;&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,libs:!(),options:&#39;-std%3Dc%2B%2B20+-O0&#39;,overrides:!(),runtimeTools:!(),source:1,stdinPanelShown:&#39;1&#39;,tree:0,wrap:&#39;0&#39;),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;Executor+x86-64+clang+14.0.0+(C%2B%2B,+Editor+%231)&#39;,t:&#39;0&#39;)),header:(),l:&#39;4&#39;,m:39.02439024390244,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),k:52.83746556473831,l:&#39;3&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;),(g:!((h:compiler,i:(compiler:clang1400,filters:(b:&#39;0&#39;,binary:&#39;1&#39;,binaryObject:&#39;1&#39;,commentOnly:&#39;0&#39;,debugCalls:&#39;1&#39;,demangle:&#39;0&#39;,directives:&#39;0&#39;,execute:&#39;1&#39;,intel:&#39;0&#39;,libraryCode:&#39;1&#39;,trim:&#39;1&#39;,verboseDemangling:&#39;0&#39;),flagsViewOpen:&#39;1&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,libs:!(),options:&#39;-std%3Dc%2B%2B20+-O0&#39;,overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;+x86-64+clang+14.0.0+(Editor+%231)&#39;,t:&#39;0&#39;)),k:47.16253443526172,l:&#39;4&#39;,m:100,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),l:&#39;2&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;)),version:4">Some experimentation in Compiler Explorer</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89df5ba6b25e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[C++ Move Semantics Pt. 4 — Forwarding References & std::forward]]></title>
            <link>https://jaredmil.medium.com/c-move-semantics-pt-4-forwarding-references-std-forward-e32e95c72a7e?source=rss-b54a8b7d894c------2</link>
            <guid isPermaLink="false">https://medium.com/p/e32e95c72a7e</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[move-semantics]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[c-plus-plus-language]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Jared Miller]]></dc:creator>
            <pubDate>Thu, 01 Aug 2024 14:32:55 GMT</pubDate>
            <atom:updated>2025-05-16T02:16:59.281Z</atom:updated>
            <content:encoded><![CDATA[<h3>C++ Beyond the Syllabus #8: Move Semantics Pt. 4 — Forwarding References &amp; std::forward</h3><h4><em>Plus, implementing `</em><em>std::move` and `</em><em>std::make_unique`</em></h4><p><em>Not a Medium member? View this entire article for free, </em><a href="https://jaredmil.medium.com/e32e95c72a7e?source=friends_link&amp;sk=54c6f09a5a7860e8d9cbd66cfdc1eac1"><em>here</em></a><em>.</em></p><p><em>This article is a part of the </em><a href="https://jaredmil.medium.com/free-links-to-all-c-beyond-the-syllabus-articles-259c26a682c8"><em>C++ Beyond the Syllabus</em></a><em> series. Subscribe </em><a href="https://jaredmil.medium.com/subscribe"><em>here</em></a><em> to receive each new issue directly in your inbox.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*6bKLaAqAtlyyop8N" /></figure><p>What should you do when you want two functions to have the same code, but the argument passed in can be an lvalue or an rvalue?</p><p>Enter: <em>Forwarding References</em></p><p>In this article, I’ll cover what a forwarding reference is along with its partner in crime, std::forward.</p><p>Together, these tools are used in a C++ idiom called <em>perfect forwarding</em>.</p><h3><strong>Learn These First</strong></h3><p>If you aren’t already familiar with <em>lvalue references</em>, <em>rvalue references</em>, and std::move, I would definitely take a look at the previous few articles in this series.</p><p>Here are “friend links” (<em>they won’t count toward your free article limit on Medium</em>) to those articles:</p><ul><li><a href="https://jaredmil.medium.com/move-semantics-pt-1-lvalues-rvalues-a-case-for-shallow-copies-b66c3504c1c3?sk=aae4c7cf522f79a4e7b3ab36ba52c9ef">Move Semantics Pt. 1 — Lvalues, Rvalues &amp; A Case For Shallow Copies</a></li><li><a href="https://jaredmil.medium.com/move-semantics-pt-2-rvalue-references-the-rule-of-five-80a4fd5f64d6?sk=72545c60ef2dbb60647d7247d64ce4f8">Move Semantics Pt. 2 — Rvalue References &amp; The Rule of Five</a></li><li><a href="https://jaredmil.medium.com/move-semantics-pt-3-std-move-explained-simply-337aa3061d0d?sk=f14892e55be8090187f59d7c7e63eb94">Move Semantics Pt. 3 — std::move Explained Simply</a></li></ul><p>A strong foundation in these topics is important for truly understanding perfect forwarding. Without this, you may very well find yourself making mistakes and not even realizing it.</p><p>Now, assuming you have read the previous articles, understanding perfect forwarding probably won’t be too hard. Let’s get to it!</p><h3><strong>Short &amp; Sweet Definitions</strong></h3><blockquote>A <strong>forwarding reference</strong> (or universal reference) is denoted as T&amp;&amp; for some <em>template</em> type T. This reference generally takes the form of whatever reference type (and constness) it is initialized with.</blockquote><p>Here are some comparisons between forwarding references and rvalue references:</p><ul><li>An rvalue reference is also represented with two ampersands, but an rvalue reference requires a <em>non-template</em> type, while a forwarding reference is always templated.</li><li>Like rvalue references, forwarding references have a name, so while the reference can bind to an lvalue or rvalue, the type of the forwarding reference itself is an lvalue.</li></ul><blockquote><strong>std::forward</strong> is a function that accepts a forwarding reference as a parameter and conditionally casts it to an rvalue, if and only if the forwarding reference is bound to an rvalue.</blockquote><blockquote>Used in conjunction, a forwarding reference and std::forward can eliminate the need for function overloads that differ only by the lvalue/rvalue-type of their parameter(s). This concept is commonly referred to as perfect forwarding.</blockquote><p>Unless you are writing libraries or other heavily templated code, the above explanations might suffice. If you are templating heavily, want a more holistic understanding, or want to see how any of std::move, std::forward, or std::make_unique are implemented… then read on!</p><h3><strong>Perfect Forwarding</strong></h3><p>There are a handful of use cases for functions overloaded to accept either an lvalue reference or an rvalue reference parameter.</p><p>For starters, the Rule of Five states that every object with dynamically allocated resources should implement an overloaded copy constructor and copy-assignment operator. Another example is function std::vector::push_back, which has <a href="https://en.cppreference.com/w/cpp/container/vector/push_back">overloaded implementations</a> for with either an lvalue reference or an rvalue reference parameter.</p><p>Now, let’s consider a third example, std::make_unique, which I introduced in the article <a href="https://jaredmil.medium.com/c-beyond-the-syllabus-4-raii-smart-pointers-7c784134ace5?source=friends_link&amp;sk=15aa8222ac7c3a1e52101a466f59a172">RAII &amp; Smart Pointers</a>. Let’s forget this is a templated function for a moment and write up a naive implementation specifically for an object, Object, that accepts a single argument of type ArgType:</p><pre>unique_ptr&lt;Object&gt; make_unique( const ArgType&amp; arg )<br>{<br>  return new Object( arg );<br>}<br><br>unique_ptr&lt;Object&gt; make_unique( ArgType&amp;&amp; arg )<br>{<br>  return new Object( std::forward( arg ) );<br>}</pre><p>Let’s walk through this:</p><ul><li>The first overload accepts a const lvalue reference. This means the function can bind to any lvalue, lvalue reference, rvalue, or rvalue reference for some argument of type ArgType. The argument is then passed into the const lvalue reference Object constructor overload.</li><li>The second make_unique overload accepts an rvalue reference. From our overload resolution priorities (discussed <a href="https://jaredmil.medium.com/move-semantics-pt-3-std-move-explained-simply-337aa3061d0d?sk=f14892e55be8090187f59d7c7e63eb94">here</a> &amp; shown in chart below), we know any calls to make_unique with an rvalue will go to this overload, not the first overload. Then, using std::forward to pass arg into the Object constructor preserves the rvalueness of the argument and invokes an rvalue reference constructor overload (or const lvalue reference overload, if the former is not defined).</li></ul><p>This simplified example works, but we made quite a few assumptions and it’s not very realistic. So what changes would we have to make if we wanted std::make_unique to work as a library function that can be templated on any type?</p><p>Here’s a few of the necessary changes:</p><ul><li>Object needs replaced with a template type</li><li>ArgType needs replaced with a variadic template <a href="https://en.cppreference.com/w/cpp/language/parameter_pack">parameter pack</a></li></ul><p>In short, a variadic template parameter pack is a template type that encompasses some variable number of template arguments that can be of the same or different types. Parameter packs are represented by ellipses (…) throughout C++.</p><p>Alright, so what does that look like?</p><p>Here is the make_unique function stub from the <a href="https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique">C++ docs</a>:</p><pre>template&lt; class T, class… Args &gt;<br>unique_ptr&lt;T&gt; make_unique( Args&amp;&amp;… args );</pre><p>If we were to modify our attempted implementation to resemble this use of parameter packs, we’d get the following:</p><pre>template&lt; class T, class… Args &gt;<br>unique_ptr&lt;T&gt; make_unique( const Args&amp; arg… )<br>{<br>  return new T( arg… );<br>}<br><br>template&lt; class T, class… Args &gt;<br>std::unique_ptr&lt;T&gt; make_unique( Args&amp;&amp;… args )<br>{<br>  return new T( std::forward&lt;Args&gt;( args )… );<br>}</pre><p>So of the two overloads above, the latter is definitely more interesting. But first, do we even need the first overload?</p><p>The second overload accepts a parameter pack of universal references, meaning it accepts any number of any arguments of any object type(s) of any value type (lvalue/rvalue) of any constness. Soooo, that covers all of the bases that the first overload covers?</p><p>Yep — let’s get rid of the first overload.</p><p>This leaves us with the following implementation of std::make_unique:</p><pre>template&lt; class T, class… Args &gt;<br>std::unique_ptr&lt;T&gt; make_unique( Args&amp;&amp;… args )<br>{<br>  return new T( std::forward&lt;Args&gt;( args )… );<br>}</pre><p>The ellipses after the std::forward function call is to “unpack” the parameter pack. It essentially will expand to a comma separated list of the preceding expression applied to each value in the pack. In this case, the unpacked expression will evaluate to:</p><pre>std::forward&lt;Arg1Type&gt;( arg1 ), std::forward&lt;Arg1Type&gt;( arg2 ), // and so on</pre><p>Now that we’ve covered forwarding references in depth, we can add it to our handy reference-binding chart from the previous articles:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CjBn5xHe-lXAJSeV" /></figure><p>One interesting observation here is that in overload resolution (at compile time), the forwarding reference overloads have lower priority than any overload with an explicit type/constness match, but higher priority than any other viable overload.</p><p>Though in practice, if you write a function accepting a forwarding reference, you shouldn’t really need any other function overloads.</p><p>Luckily for us, there’s a couple more use cases of forwarding references that we’ve already discussed in depth, which would be quite interesting to look at…</p><h3><strong>Implementing `</strong><strong>std::move`</strong></h3><p>The implementation of std::move follows:</p><pre>#include &lt;type_traits&gt;<br><br>namespace std<br>{<br><br>template&lt;class T&gt;<br>typename remove_reference&lt;T&gt;::type&amp;&amp; move(T&amp;&amp; a) noexcept<br>{<br>  typedef typename remove_reference&lt;T&gt;::type&amp;&amp; RvalRef;<br>  return static_cast&lt;RvalRef&gt;(a);<br>}<br><br>} // namespace std</pre><p>There’s some new syntax in here, so let’s break it down.</p><ul><li>Value category (lvalue/rvalue) and constness aside, we’re working with a template type T.</li><li>The parameter of this function, T&amp;&amp; a, is a forwarding reference because it is denoted by a double ampersand following a template type.</li><li>The return value type is typename remove_reference&lt;T&gt;::type&amp;&amp;. The typename keyword here just tells the compiler that it should expect the following expression to describe a type.</li><li>This return type also appears in the function body following the typedef keyword. This keyword just allows us to refer to the rather complex type name as its alias, RvalRef, for the remainder of the scope.</li><li>The remove_reference&lt;T&gt;::type portion of our return value type simply says we should ignore the value category (lvalue/rvalue) and reference type and just deduce the underlying type of T (eg. int, std::string, Object, etc.). This deduced type is no longer a template. <em>We</em> can’t see the type when looking at the code, but to the compiler, it is now some concrete type. Adding the &amp;&amp; on the end of the deduced type of T makes the type an rvalue reference (recall, double ampersands following non-template types represent rvalue references).</li><li>noexcept can be ignored for now if you aren’t already familiar. The TLDR is that it assures the compiler a function will never throw any exceptions and can be optimized a little extra. See the <a href="https://en.cppreference.com/w/cpp/language/noexcept">C++ docs</a> for more info.</li><li>The only line with side effects in the entire function body is the static_cast of our forwarding reference parameter to the rvalue reference type, which is returned from the function. (And this line is still evaluated at compile time!)</li></ul><p>Hopefully that wasn’t too tricky to follow along with… and maybe now you see why I suggested std::move actually be called std::cast_to_rvalue… literally all it does is unconditionally cast its argument to an rvalue.</p><h3><strong>Implementing `std::forward`</strong></h3><p>std::forward also uses forwarding references and is pretty interesting to look at. Here’s one possible implementation:</p><pre>#include &lt;type_traits&gt;<br><br>namespace std<br>{<br><br>template &lt;typename T&gt;<br>T&amp;&amp; forward(typename std::remove_reference&lt;T&gt;::type&amp; t) noexcept<br>{<br>  return static_cast&lt;T&amp;&amp;&gt;(t);<br>}<br><br>template &lt;typename T&gt;<br>T&amp;&amp; forward(typename std::remove_reference&lt;T&gt;::type&amp;&amp; t) noexcept<br>{<br>  static_assert(!std::is_lvalue_reference&lt;T&gt;::value<br>                , &quot;Cannot forward an rvalue as an lvalue.&quot;);<br>  return static_cast&lt;T&amp;&amp;&gt;(t);<br>}<br><br>} // namespace std</pre><p>Let’s break this down:</p><ul><li>Recall from our walkthrough of std::move that std::remove_reference&lt;T&gt;::type deduces a concrete, non-template type. Given that, we can see the first overload accepts an lvalue reference and the second overload accepts an rvalue reference (i.e. — neither of these overloads accept forwarding references as parameters).</li><li>We see that the only non-assertion line in each overload’s function body is exactly the same! They simply cast the lvalue or rvalue reference parameter to a forwarding reference (because T is a template type), which is now bound to either an lvalue or rvalue respectively (based on what overload we’re working with).</li><li>The static_assert in the second overload checks the condition !std::is_lvalue_reference&lt;T&gt;::value, which will throw a compiler error if the template type T is actually an lvalue reference. You might be wondering why this assert is needed, <em>“wouldn’t lvalue reference types just invoke the first overload?” </em>Usually, yes. But, you could invoke std::forward like std::forward&lt;rvalue_type&gt;(lvalue), which would fail this assertion. You likely won’t run into this case in practice and if you do, the compiler will keep you in check.</li></ul><p>Interesting that after seeing the implementation, it almost looks like std::forward provides an overloaded library function we can use just so <em>we</em> don’t have to duplicate the code we write when overloads would differ only by their parameters’ lvalue/rvalue type.</p><p>And yea… that is basically the purpose.</p><h3><strong>What’s Next?</strong></h3><p>You now know enough to understand and use forwarding references in your code!</p><p>If you found this post helpful, please consider <em>clapping</em>, following, and sharing with your peers :)</p><h3><strong>Sources &amp; More Info</strong></h3><ul><li><em>Effective Modern C++</em> by Scott Meyers, Items 23–25, 28</li><li><a href="http://thbecker.net/articles/rvalue_references/section_01.html">“C++ Rvalue References Explained” by Thomas Becker</a></li><li><a href="https://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c/">“Understanding lvalues and rvalues in C and C++” by Eli Bendersky</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e32e95c72a7e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Move Semantics Pt. 3 — std::move Explained (Simply)]]></title>
            <link>https://jaredmil.medium.com/move-semantics-pt-3-std-move-explained-simply-337aa3061d0d?source=rss-b54a8b7d894c------2</link>
            <guid isPermaLink="false">https://medium.com/p/337aa3061d0d</guid>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[c-plus-plus-language]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[move-semantics]]></category>
            <dc:creator><![CDATA[Jared Miller]]></dc:creator>
            <pubDate>Thu, 25 Jul 2024 15:28:39 GMT</pubDate>
            <atom:updated>2024-07-25T15:28:39.546Z</atom:updated>
            <content:encoded><![CDATA[<h3>C++ Beyond the Syllabus #7: Move Semantics Pt. 3 — std::move Explained (Simply)</h3><h4><em>Transferring ownership of an lvalue’s resources.</em></h4><p><em>Not a Medium member? View this entire article </em><a href="https://jaredmil.medium.com/337aa3061d0d?source=friends_link&amp;sk=f14892e55be8090187f59d7c7e63eb94"><em>here</em></a><em>!</em></p><p><em>This article is a part of the </em><a href="https://jaredmil.medium.com/free-links-to-all-c-beyond-the-syllabus-articles-259c26a682c8"><em>C++ Beyond the Syllabus</em></a><em> series. Subscribe </em><a href="https://jaredmil.medium.com/subscribe"><em>here</em></a><em> to receive each new issue directly in your inbox.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/0*I_tN28pXs8o847nE" /></figure><p>In this article, I’ll cover everything you need to know about how to use (and when not to use!) std::move effectively.</p><p>But first, keep in mind this one thing…</p><blockquote>The name of the function std::move is misleading and it doesn’t move anything. In fact, std::move only does one thing: unconditionally cast its argument to an rvalue.</blockquote><p>The STL authors really should have called it something like std::cast_to_rvalue.</p><h3><strong>Learn These First</strong></h3><h4><strong>A Brief Recap</strong></h4><p>Recall the following definitions:</p><ul><li><strong><em>lvalues</em></strong> — have a name and address</li><li><strong><em>rvalues</em></strong> — are temporary values that disappear after the expression they appear in</li><li><strong><em>lvalue</em></strong><em> </em>&amp;<em> </em><strong><em>rvalue references</em></strong><em> </em>— bind to value types according to the following graphic:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*cG4lx-vnNiTC_iGh" /></figure><ul><li><strong><em>transfer of ownership</em></strong> — a concept where the responsibility of managing dynamically allocated data members (i.e. — lives on the heap) is transferred from one object to another</li><li><strong><em>lvalue copy constructor </em></strong>&amp;<strong><em> copy-assignment operators</em></strong> — special member functions to create deep copies of the <em>copied-from</em> object’s dynamically allocated resources</li><li><strong><em>rvalue copy constructor </em></strong>&amp;<strong><em> copy-assignment operators</em></strong> — special member functions to claim ownership (i.e. — facilitates the transfer of ownership) of the <em>copied-from</em> object’s dynamically allocated resources</li></ul><p>Now recall that the function stubs of these lvalue/rvalue special member function overloads differ only by the type category of the argument they accept. For an object, Object, the lvalue overloads may accept a const or non-const lvalue reference denoted by Object&amp;. The rvalue overloads will accept an rvalue reference of type Object&amp;&amp;.</p><h4><strong>A Strong Foundation</strong></h4><p>If any of the above topics seem unfamiliar to you, I’d <em>strongly</em> recommend checking out the previous two articles in this series to ensure a strong foundation:</p><ul><li><a href="https://jaredmil.medium.com/move-semantics-pt-1-lvalues-rvalues-a-case-for-shallow-copies-b66c3504c1c3?sk=aae4c7cf522f79a4e7b3ab36ba52c9ef">Move Semantics Pt. 1 — Lvalues, Rvalues &amp; A Case For Shallow Copies</a></li><li><a href="https://jaredmil.medium.com/move-semantics-pt-2-rvalue-references-the-rule-of-five-80a4fd5f64d6?sk=72545c60ef2dbb60647d7247d64ce4f8">Move Semantics Pt. 2 — Rvalue References &amp; The Rule of Five</a></li></ul><p>I myself have made the mistake, along with many of my peers, of trying to understand std::move without a strong foundation in the fundamentals. Everyone who does that will most definitely use std::move incorrectly, sometimes making code <em>less efficient</em>, and need to circle back to the basics anyways.</p><h3><strong>Finally Understanding Move Semantics</strong></h3><p>Congrats! If you’ve made it this far, I’m going to assume you have a solid foundation in the topics listed above. Now let’s finally get to understanding std::move.</p><p>The good news is it’s actually super simple.</p><blockquote>Most tutorials take a <em>long </em>time to comprehend because they strictly explain it in non-intuitive technical terms. I’ll do better than that.</blockquote><h4><strong>Explain It Simply, Please.</strong></h4><p>We know rvalue copy constructor &amp; copy-assignment operators have two unique properties compared to their lvalue counterparts. Rvalue overloads of these special member functions:</p><ol><li>facilitate the transfer of ownership of dynamically allocated resources from the argument (i.e. — the <em>copied-from</em> object); and</li><li>can only be invoked on rvalue arguments, as rvalue references may only bind to rvalues.</li></ol><p>This is great and all, but what happens when we want to transfer ownership of dynamically allocated resources <em>from an lvalue</em>?</p><p>This is the use case for std::move.</p><p>But wait! This is where a lot of people get tripped up… Before we get any further, I want you to disassociate the concept of lvalues and rvalues from their full technical meanings. Yep — for the rest of this subsection, just forget that lvalues can have their address taken, rvalues disappear after the expression they appear in, and all the other nuances.</p><p>Instead, internalize these (very much true, but narrower different) definitions:</p><ul><li><strong><em>lvalue</em></strong> — a type that when copied, has a deep copy performed</li><li><strong><em>rvalue</em></strong> — a type that when copied, has a transfer of ownership (of any dynamically allocated resources) performed</li></ul><p>Note that the text <em>“when copied”</em> above really just means <em>“when passed into the copy constructor or copy-assignment operator of another instance of the same object type.”</em></p><blockquote>Now, simply treat std::move as a black box function that unconditionally converts the argument into an rvalue (in the context of the above definitions) for the remainder of the expression it appears in.</blockquote><p>Let’s look at an example:</p><p>Assume we have a class, Object, that correctly implements the <a href="https://jaredmil.medium.com/move-semantics-pt-2-rvalue-references-the-rule-of-five-80a4fd5f64d6?sk=72545c60ef2dbb60647d7247d64ce4f8">Rule of Five</a> — meaning it definitely has lvalue and rvalue overloads of the copy constructor and copy-assignment operator.</p><p>If an lvalue, lvalue, of type Object is passed into the copy constructor or copy-assignment operator, it will invoke the <em>lvalue overload</em> causing its resources to be <em>deep copied</em> into the new object. In code, that would look something like this:</p><pre>Object new_object(lvalue);</pre><p>or this:</p><pre>Object some_object;<br>some_object = lvalue;</pre><p>If instead std::move(lvalue) is passed into the Object copy constructor or copy-assignment operator, it will invoke the <em>rvalue</em> <em>overload</em> causing lvalue’s resources to be <em>transferred</em> to the new object. This is because std::move(lvalue) casts lvalue to an rvalue for the remainder of the expression and per our narrow definitions above, rvalues indicate that we want resources to be transferred during copies. In code, that would look something like this:</p><pre>Object new_object(std::move(lvalue));</pre><p>or this:</p><pre>Object some_object;<br>some_object = std::move(lvalue);</pre><p>We should also clarify that invoking std::move on an rvalue is idempotent. It is valid code, but it doesn’t actually do anything, because it just casts an rvalue to an rvalue.</p><p>That wasn’t too hard, right?</p><h4><strong>A Bit More Technical</strong></h4><p>You can now remember the holistic definitions of <em>lvalue</em> and <em>rvalue</em> again, just don’t forget your non-technical understanding from above!</p><p><a href="https://jaredmil.medium.com/move-semantics-pt-1-lvalues-rvalues-a-case-for-shallow-copies-b66c3504c1c3?sk=aae4c7cf522f79a4e7b3ab36ba52c9ef">When introducing lvalues and rvalues</a>, we talked about how you can always take the address of an lvalue, but not an rvalue. I also mentioned the caveat that the whole idea of lvalues and rvalues are really just rules for how the compiler needs to interface with our code, but behind the scenes it can make (almost) any optimizations it wants. This means that sometimes rvalues are stored in memory too. You still can’t take the address of them from code, but it’s important to note that an rvalue can represent some object stored in memory.</p><p>std::move itself does not move anything, copy anything, or even produce any executable code. The magic happens at compile time.</p><p>The rvalue generated by some code std::move(lvalue) still refers to the original object stored in memory that lvalue represents. One side-effect (or really non-effect) of this, is that a const lvalue passed into std::move will maintain its constness and result in a new value type called <em>rvalue-to-const</em>. This type isn’t used very often and its binding rules are pretty intuitive, but it’s worth knowing and adding to our handy binding chart:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6xMLEYJGo1o3QD5x" /></figure><p>By (statically) casting an lvalue to an rvalue, std::move simply affects overload resolution. All it does is cause the compiler to prefer an rvalue overload of the aforementioned special member functions, if one is defined.</p><p>This is a crazy powerful feature that allows us to rid our code of unnecessary copies. But…</p><h4><strong>With Great Power, Comes Great Responsibility</strong></h4><p>There are three big red flags to keep in mind when using move semantics:</p><ol><li><a href="https://jaredmil.medium.com/move-semantics-pt-2-rvalue-references-the-rule-of-five-80a4fd5f64d6?sk=72545c60ef2dbb60647d7247d64ce4f8">When we first introduced the rvalue copy constructor and copy-assignment operator</a>, we noted that after invoking one of these functions (i.e. — moving from some object), the state of any dynamically allocated members in that <em>moved-from</em> object is <em>only</em> guaranteed to be “valid and destructible.” That definition is rather elusive, as it relies on the object’s implementation.<br> <br>Where does that leave us?<br> <br>Once you use std::move on an object, you shouldn’t make any assumptions regarding the state of moved-from data members based on properties observed prior to the move. In fact, a good rule of thumb is to either never access moved-from data members again or ensure they are reassigned with some expected state prior to being used again.</li><li>Never use std::move in a return statement from a function (eg. return std::move(lvalue)), as this prevents the compiler from employing <a href="https://en.wikipedia.org/wiki/Copy_elision#Return_value_optimization">return value optimization (RVO)</a>, which is even more efficient than move semantics.</li><li>Make sure any custom objects you use std::move on follow the <a href="https://jaredmil.medium.com/move-semantics-pt-2-rvalue-references-the-rule-of-five-80a4fd5f64d6?sk=72545c60ef2dbb60647d7247d64ce4f8">Rule of Five</a> and have rvalue overloads of the copy constructor and copy-assignment operator implemented. You definitely don’t want to assume you are using move semantics when you actually are just performing deep copies.</li></ol><h3><strong>What’s Next?</strong></h3><p>Congrats! You now have enough knowledge to use std::move correctly… and also to point out existing mistakes in your code base 😎</p><p>You might be wondering how std::move is implemented. Well, that requires an understanding of perfect forwarding, which we’ll go over next week.</p><p>If you liked this article, please consider <em>clapping</em> and sharing. To get notified when the next “C++ Beyond the Syllabus” article is posted, you can subscribe for free <a href="https://jaredmil.medium.com/subscribe">here</a>.</p><h3><strong>Sources &amp; More Info</strong></h3><ul><li><em>Effective Modern C++</em> by Scott Meyers, Items 23–25, 28</li><li><a href="http://thbecker.net/articles/rvalue_references/section_01.html">“C++ Rvalue References Explained” by Thomas Becker</a></li><li><a href="https://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c/">“Understanding lvalues and rvalues in C and C++” by Eli Bendersky</a></li></ul><p>A special thanks to Mason Wilie (<a href="https://www.linkedin.com/in/mason-wilie/">LinkedIn</a>) who took time to edit and review this post.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=337aa3061d0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Free links to all ‘C++ Beyond The Syllabus’ articles]]></title>
            <link>https://jaredmil.medium.com/free-links-to-all-c-beyond-the-syllabus-articles-259c26a682c8?source=rss-b54a8b7d894c------2</link>
            <guid isPermaLink="false">https://medium.com/p/259c26a682c8</guid>
            <category><![CDATA[c-plus-plus-language]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Jared Miller]]></dc:creator>
            <pubDate>Fri, 19 Jul 2024 19:49:47 GMT</pubDate>
            <atom:updated>2024-08-23T14:12:13.698Z</atom:updated>
            <content:encoded><![CDATA[<h4>Don’t have a paid Medium account? View the articles for free here...</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*F4JBiXoINfZc3oep5_2hrg.png" /></figure><p>Subscribe <a href="https://jaredmil.medium.com/subscribe">here</a> to receive weekly-ish articles directly in your inbox.</p><p>Below are “friend links” allowing unlimited access to view each article…</p><p><em>(Can’t even view this article? Try </em><a href="https://jaredmil.medium.com/259c26a682c8?source=friends_link&amp;sk=e203f087a2bc1f984456fc157db7976c"><em>this friend link</em></a><em>.)</em></p><ul><li><a href="https://jaredmil.medium.com/9cc98d1e5b15?source=friends_link&amp;sk=5be806016f6b51f0b7f3c722c69858c3">#11: Address Sanitizers</a></li><li><a href="https://jaredmil.medium.com/db82d7e9d4f8?sk=d6e36e4e26b4b7f4abedc582b9a56009">#10: C++20 </a><a href="https://jaredmil.medium.com/db82d7e9d4f8?sk=d6e36e4e26b4b7f4abedc582b9a56009">std::span</a></li><li><a href="https://jaredmil.medium.com/89df5ba6b25e?sk=adc91ab1a8832c497fec2f5dcef3d28e">#9: </a><a href="https://jaredmil.medium.com/89df5ba6b25e?sk=adc91ab1a8832c497fec2f5dcef3d28e">std::string_view</a></li><li><a href="https://jaredmil.medium.com/c-move-semantics-pt-4-forwarding-references-std-forward-e32e95c72a7e?source=friends_link&amp;sk=54c6f09a5a7860e8d9cbd66cfdc1eac1">#8: Move Semantics Pt. 4 — Forwarding References &amp; </a><a href="https://jaredmil.medium.com/c-move-semantics-pt-4-forwarding-references-std-forward-e32e95c72a7e?source=friends_link&amp;sk=54c6f09a5a7860e8d9cbd66cfdc1eac1">std::forward</a></li><li><a href="https://jaredmil.medium.com/move-semantics-pt-3-std-move-explained-simply-337aa3061d0d?source=friends_link&amp;sk=f14892e55be8090187f59d7c7e63eb94">#7: Move Semantics Pt. 3 — std::move Explained (Simply)</a></li><li><a href="https://jaredmil.medium.com/move-semantics-pt-2-rvalue-references-the-rule-of-five-80a4fd5f64d6?source=friends_link&amp;sk=72545c60ef2dbb60647d7247d64ce4f8">#6: Move Semantics Pt. 2 — Rvalue References &amp; The Rule of Five</a></li><li><a href="https://jaredmil.medium.com/move-semantics-pt-1-lvalues-rvalues-a-case-for-shallow-copies-b66c3504c1c3?source=friends_link&amp;sk=aae4c7cf522f79a4e7b3ab36ba52c9ef">#5: Move Semantics Pt. 1 — Lvalues, Rvalues &amp; A Case For Shallow Copies</a></li><li><a href="https://jaredmil.medium.com/c-beyond-the-syllabus-4-raii-smart-pointers-7c784134ace5?source=friends_link&amp;sk=15aa8222ac7c3a1e52101a466f59a172">#4: RAII &amp; C++ Smart Pointers</a></li><li><a href="https://jaredmil.medium.com/micro-benchmarking-c-with-quick-bench-8788b1edcf79?source=friends_link&amp;sk=64b00b1e6af159bbe25508f7bb682607">#3: Micro-Benchmarking with Quick Bench</a></li><li><a href="https://jaredmil.medium.com/c-beyond-the-syllabus-2-intro-to-benchmarking-and-macro-benchmarking-deep-dive-a465fc73a604?source=friends_link&amp;sk=cb14ece9c861a3bdf6b963693d550beb">#2: Macro-Benchmarking Deep Dive</a></li><li><a href="https://jaredmil.medium.com/c-beyond-the-syllabus-1-peeking-under-the-hood-with-compiler-explorer-aaa8d08b2876?source=friends_link&amp;sk=d694af4e787b6047efc5ed473973d6bf">#1: Compiler Explorer</a></li><li><a href="https://jaredmil.medium.com/cpp-beyond-the-syllabus-0-what-is-this-f8d2091786c6?source=friends_link&amp;sk=529252411b9fd49704fedb2b940cdeee">#0: Welcome! (a non-technical intro)</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=259c26a682c8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Move Semantics Pt. 2 — Rvalue References & The Rule of Five]]></title>
            <link>https://jaredmil.medium.com/move-semantics-pt-2-rvalue-references-the-rule-of-five-80a4fd5f64d6?source=rss-b54a8b7d894c------2</link>
            <guid isPermaLink="false">https://medium.com/p/80a4fd5f64d6</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[move-semantics]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[cplusplus]]></category>
            <category><![CDATA[c-plus-plus-language]]></category>
            <dc:creator><![CDATA[Jared Miller]]></dc:creator>
            <pubDate>Thu, 18 Jul 2024 14:31:06 GMT</pubDate>
            <atom:updated>2024-07-23T18:47:14.006Z</atom:updated>
            <content:encoded><![CDATA[<h3>C++ Beyond the Syllabus #6: Move Semantics Pt. 2 — Rvalue References &amp; The Rule of Five</h3><h4>An rvalue reference is like a magic glove letting you temporarily catch, hold, and modify a bubble without popping it.</h4><p><em>Not a Medium member? View this entire article </em><a href="https://jaredmil.medium.com/80a4fd5f64d6?source=friends_link&amp;sk=72545c60ef2dbb60647d7247d64ce4f8"><em>here</em></a><em>!</em></p><p><em>This article is a part of the </em><a href="https://jaredmil.medium.com/"><em>C++ Beyond the Syllabus</em></a><em> series. </em><a href="https://jaredmil.medium.com/subscribe"><em>Subscribe here</em></a><em> to receive each new issue directly in your inbox.</em></p><figure><img alt="Article Cover Photo: C++ Beyond the Syllabus #6: Move Semantics Pt 2 — Rvalue References &amp; The Rule of Five" src="https://cdn-images-1.medium.com/max/960/1*ju-3QhE1MCKgz_nsQyvYuA.png" /></figure><p>Move semantics is a powerful tool that can drastically increase the performance of your code by reducing unnecessary and expensive copies of data on the heap. Before you can get to move semantics though, you need a sound understanding of rvalue references.</p><p>In the last article, we covered the basics of <em>lvalues</em>, <em>rvalues</em>, and an argument for <em>shallow copies</em>. If any of those sound unfamiliar to you, I’d definitely recommend taking a quick look at that post before continuing:</p><p><a href="https://jaredmil.medium.com/move-semantics-pt-1-lvalues-rvalues-a-case-for-shallow-copies-b66c3504c1c3">Move Semantics Pt. 1 — Lvalues, Rvalues &amp; A Case For Shallow Copies</a></p><h3><strong>Lvalue &amp; Rvalue References</strong></h3><h4><strong>Back To The 5 Year Olds!</strong></h4><p>In the <a href="https://jaredmil.medium.com/move-semantics-pt-1-lvalues-rvalues-a-case-for-shallow-copies-b66c3504c1c3?source=friends_link&amp;sk=aae4c7cf522f79a4e7b3ab36ba52c9ef">previous article</a>, we related <em>lvalues</em> to a stuffed animal on the shelf and <em>rvalues</em> to a bubble….</p><p>Lvalue references:</p><ul><li>Recall your stuffed animal, named <em>lvalue,</em> who has a specific spot on the shelf</li><li>Let’s say your friend wants to borrow <em>lvalue</em>, so you let them with a couple ground rules… They can play with <em>lvalue</em>, take it on walks, and even dress it up or cut its hair, but at the end of play time, they must always put <em>lvalue</em> back in its spot on your shelf.</li><li>And your friend’s name? <em>lvalue reference</em>, of course.</li></ul><p>Rvalue references:</p><ul><li>Recall the bubble you can play with, but not hold because it will inevitably pop. That bubble was an <em>rvalue</em>.</li><li><em>Rvalue references</em> are like a magic glove. It lets you temporarily catch, hold, and modify the bubble without popping it.</li><li>The bubble is still going to pop as soon as you take the magic glove off; it just lets you use the bubble a little while longer.</li></ul><h4><strong>Let’s Get Technical With It</strong></h4><p>Lvalue references are the traditional reference type denoted by T&amp; for some type T. You are probably pretty familiar with these already, so I’ll skip right to rvalue references.</p><p>In code, an rvalue reference to some <em>non-template</em> type T is denoted as T&amp;&amp;. The most probable place you will have already seen this is as an argument to an object’s rvalue copy constructor and rvalue copy assignment operator.</p><p>Let’s work our way up to those…</p><h3><strong>The Use Case For Rvalue References</strong></h3><h4><strong>The Big Three</strong></h4><p>“The Big Three” is a concept taught to almost all C++ students. The main gist is that any class managing dynamically allocated resources (i.e. — stored on the heap), should also implement a…</p><ul><li><em>destructor</em> to clean up dynamically allocated data</li><li><em>copy constructor</em> to perform deep copies of dynamically allocated data</li><li><em>copy assignment operator</em> to clean up old data and deep copy new data</li></ul><p>Here’s a simple implementation of a class that implements The Big Three:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d0fb8daad8a675ed4f3dc588dcc7511/href">https://medium.com/media/7d0fb8daad8a675ed4f3dc588dcc7511/href</a></iframe><p>The concept of The Big Three was created prior to C++11, which introduced rvalues, rvalue references, and move semantics.</p><p>Since then, The Big Three has been augmented to the “Rule of Five,” which is also sometimes taught in school, but often not given the love it deserves.</p><h4><strong>The Rule of Five</strong></h4><p>The Rule of Five includes the three special member functions from The Big Three, along with two additional functions…</p><ul><li>rvalue/move copy constructor</li><li>rvalue/move copy assignment operator</li></ul><p><em>Note: The above are often referred to by “rvalue copy X” and “move copy X” interchangeably, for the remainder of this article, I will call them by “rvalue copy X.” As I explain in this series, the functions themselves don’t really move anything. You could theoretically swap all of the code from the lvalue and rvalue versions of these functions (along with some other code changes) and everything would behave the same.</em></p><p>Before getting into the syntax, know that the main point of these new function overloads is to facilitate the <em>transfer of ownership</em> of resources (i.e. — move semantics) as an alternative to performing a deep copy.</p><p>Recall the <a href="https://jaredmil.medium.com/move-semantics-pt-1-lvalues-rvalues-a-case-for-shallow-copies-b66c3504c1c3?source=friends_link&amp;sk=aae4c7cf522f79a4e7b3ab36ba52c9ef">value proposition of transferring ownership</a>:</p><blockquote>We can eliminate unneeded copies of dynamically allocated data when copying from an object that will not use the managed data again.</blockquote><p>Below, we modify our Object class for the Rule of Five…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7cc3201aea8559a48f76dfcaf2a20e6e/href">https://medium.com/media/7cc3201aea8559a48f76dfcaf2a20e6e/href</a></iframe><p>Notice the only difference in function stub of the two new functions and their Big Three counterparts is that the new functions accept their argument as an rvalue reference instead of an lvalue reference.</p><h4><strong>The Transfer of Ownership</strong></h4><p>This seems pretty intuitive, but it’s worth explicitly stating as they are prone to error. As a knowledgeable software engineer writing rvalue copy constructors and rvalue copy assignment operators on a regular basis, it’s good to keep these explicit steps in mind…</p><p>Responsibilities of the <em>rvalue copy constructor</em>:</p><ul><li>Initialize dynamically allocated member variables with shallow copies of other’s dynamically allocated members.</li><li>Reset other’s dynamically allocated members to some valid, destructible state. (Note, for simplicity, my example’s destructor accounts for nullptr.)</li></ul><p>Responsibilities of the <em>rvalue copy assignment operator</em>:</p><ul><li>Clean up the dynamically allocated resource of the current object.</li><li><em>+ all responsibilities of the rvalue copy constructor</em></li></ul><p>A common trick for the rvalue copy assignment operator, which takes care of all needed responsibilities is to simply swap the managed resources of the current object and other like below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/258e2105bd41458e0b8b8e32df3a343c/href">https://medium.com/media/258e2105bd41458e0b8b8e32df3a343c/href</a></iframe><p>The <a href="https://en.cppreference.com/w/cpp/algorithm/swap">std::swap syntax</a> actually just invokes the rvalue copy constructor and rvalue copy assignment operator on an object/resource. This works because move semantics (i.e. — the act of invoking these rvalue function overloads) makes no guarantees about the state of the <em>moved-from</em> object other than it must be valid and destructible. So, we can simply swap it with whatever current valid and destructible state our <em>moved-to</em> object had.</p><h4><strong>Your Burning Question</strong></h4><p>The rvalue references as arguments to the above rvalue copy constructor and rvalue copy assignment operator are denoted as Object&amp;&amp; other. That other thing… that’s a name…</p><p>Does this mean rvalue references are… lvalues?</p><p>YES!</p><p>That was a great question 😉. It’s pretty counterintuitive, but rvalue references actually satisfy all of the conditions for lvalues that we discussed in <a href="https://jaredmil.medium.com/move-semantics-pt-1-lvalues-rvalues-a-case-for-shallow-copies-b66c3504c1c3?source=friends_link&amp;sk=aae4c7cf522f79a4e7b3ab36ba52c9ef">our last article</a>. Recall: an lvalue…</p><ul><li>has a name</li><li>can appear on either side of an assignment operator (=)</li><li>can have its address taken with the address-of operator (&amp;)</li></ul><p>So, what happens when we need to access an rvalue from an rvalue reference? This is where std::move comes into play. It’s not very intuitive, so I’ll cover it in-depth later in this series.</p><h3><strong>Reference Binding</strong></h3><p>Now that we are on the same page for <em>where</em> rvalue references are used, you might have some questions about <em>how</em> these functions are invoked.</p><p>To get there, we have to talk about <em>reference binding</em>.</p><h4><strong>Lvalue Reference Binding</strong></h4><p>You’ve been binding lvalue references since your 100-level CS course. Here’s a simple example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e9acc15ff8190cb5e4bf609c39ac62ec/href">https://medium.com/media/e9acc15ff8190cb5e4bf609c39ac62ec/href</a></iframe><p>The above code compiles. As you see on lines two and three respectively,</p><blockquote>an lvalue reference can bind to an lvalue or another lvalue reference.</blockquote><p>Now consider this example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/844a1dfec3c58e25cc5beb8e77ef1768/href">https://medium.com/media/844a1dfec3c58e25cc5beb8e77ef1768/href</a></iframe><p>Let’s break this down…</p><ul><li>non-const lvalue reference to type ‘int’ — that must refer to int&amp; in our code</li><li>temporary type of ‘int’ — this is effectively another way of saying rvalue int</li><li>Thus, the error tells us we can’t bind an non-const lvalue reference to an rvalue</li></ul><p>Hmmm… but why does it specify non-const?</p><p>Well, if we make the lvalue reference const and throw it in Compiler Explorer, we see it compiles (<a href="https://godbolt.org/#g:!((g:!((g:!((g:!((h:codeEditor,i:(filename:&#39;1&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,selection:(endColumn:31,endLineNumber:3,positionColumn:31,positionLineNumber:3,selectionStartColumn:31,selectionStartLineNumber:3,startColumn:31,startLineNumber:3),source:&#39;int+main()%0A%7B%0A++++const+int%26+lvalue_ref+%3D+5%3B%0A%7D&#39;),l:&#39;5&#39;,n:&#39;1&#39;,o:&#39;C%2B%2B+source+%231&#39;,t:&#39;0&#39;)),k:28.696820966819853,l:&#39;4&#39;,m:49.631449631449634,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;),(g:!((h:executor,i:(argsPanelShown:&#39;1&#39;,compilationPanelShown:&#39;0&#39;,compiler:clang1400,compilerName:&#39;&#39;,compilerOutShown:&#39;0&#39;,execArgs:&#39;&#39;,execStdin:&#39;&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,libs:!(),options:&#39;-std%3Dc%2B%2B20+-O0&#39;,overrides:!(),runtimeTools:!(),source:1,stdinPanelShown:&#39;1&#39;,tree:0,wrap:&#39;0&#39;),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;Executor+x86-64+clang+14.0.0+(C%2B%2B,+Editor+%231)&#39;,t:&#39;0&#39;)),header:(),l:&#39;4&#39;,m:50.368550368550366,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),k:41.68694241686944,l:&#39;3&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;),(g:!((h:compiler,i:(compiler:clang1400,filters:(b:&#39;0&#39;,binary:&#39;1&#39;,binaryObject:&#39;1&#39;,commentOnly:&#39;0&#39;,debugCalls:&#39;1&#39;,demangle:&#39;0&#39;,directives:&#39;0&#39;,execute:&#39;1&#39;,intel:&#39;0&#39;,libraryCode:&#39;1&#39;,trim:&#39;1&#39;,verboseDemangling:&#39;0&#39;),flagsViewOpen:&#39;1&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,libs:!(),options:&#39;-std%3Dc%2B%2B20+-O0&#39;,overrides:!(),selection:(endColumn:12,endLineNumber:9,positionColumn:12,positionLineNumber:9,selectionStartColumn:12,selectionStartLineNumber:9,startColumn:12,startLineNumber:9),source:1),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;+x86-64+clang+14.0.0+(Editor+%231)&#39;,t:&#39;0&#39;)),k:58.313057583130586,l:&#39;4&#39;,m:100,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),l:&#39;2&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;)),version:4">link</a>):</p><figure><img alt="Compiler Explorer: What actually happens when binding a const lvalue reference to an rvalue?" src="https://cdn-images-1.medium.com/max/1024/0*LC4nqCkt1_hWPwuQ" /><figcaption>Compiler Explorer: What actually happens when binding a const lvalue reference to an rvalue?</figcaption></figure><p>From looking at the generated assembly with optimizations disabled, we can see the compiler writes the value 5 on the stack and stores the reference to that value in lvalue_ref.</p><p>But lvalue_ref doesn’t reference an rvalue then, right?</p><p>Right, not anymore.</p><p>C++ has a type of <a href="https://en.cppreference.com/w/cpp/language/implicit_conversion">implicit conversion</a> called temporary materialization allowing rvalues to be converted into lvalues when bound to a const lvalue reference. The lifetime of the once-temporary variable is then bound to the scope of the const lvalue reference.</p><h4><strong>Rvalue Reference Binding</strong></h4><p>Rvalue reference binding is actually quite simple. There is one rule:</p><blockquote>Rvalue references can only bind to rvalues.</blockquote><p>Remember the magic glove analogy — just like the glove, rvalue references allow you to modify the rvalue it is bound to.</p><p>Const qualifiers behave similarly to those on lvalue references, with the exception that there is no such thing as a const rvalue. There is, however, a thing referred to as <em>ravlue-to-const</em>, which isn’t used too often — I’ll touch on that more when discussing std::move in my next article.</p><p>Note that while const rvalue references exist, they really aren’t used that often. The main use case of rvalue references in practice is to enable move semantics (i.e. — transfer of ownership) and any const qualifiers would prevent that from happening.</p><h4><strong>Quick Recap</strong></h4><p>Now, you’re probably like <em>“woah — lots of information… what about that really matters?”</em></p><p>Here’s a chart to sum up the important takeaways.</p><figure><img alt="Chart created in Google Slides displaying reference types and the associated value types they can bind to." src="https://cdn-images-1.medium.com/max/946/0*PyP8Fv7MsllkCKjH" /><figcaption>Chart created in Google Slides displaying reference types and the associated value types they can bind to.</figcaption></figure><h4><strong>Overload Resolution</strong></h4><p>Sooo, that might leave you with another burning question:</p><p>How does overload resolution work?</p><p>Let’s start with what you’re most likely to be familiar with already. Consider a simple example with two function definitions accepting an lvalue reference as an argument, differing only by constness. What will the following code output?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74225dc5235c047d6338b39a3ad419e4/href">https://medium.com/media/74225dc5235c047d6338b39a3ad419e4/href</a></iframe><p>If you’re unsure, plug it into your IDE or Compiler Explorer (<a href="https://godbolt.org/#g:!((g:!((g:!((g:!((h:codeEditor,i:(filename:&#39;1&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,selection:(endColumn:2,endLineNumber:20,positionColumn:2,positionLineNumber:20,selectionStartColumn:2,selectionStartLineNumber:20,startColumn:2,startLineNumber:20),source:&#39;%23include+%3Ciostream%3E%0A%0Avoid+func(int%26+lvalue_ref)%0A%7B%0A++++std::cout+%3C%3C+%22lvalue_ref:+%22+%3C%3C+lvalue_ref+%3C%3C+%22%5Cn%22%3B%0A%7D%0A%0Avoid+func(const+int%26+const_lvalue_ref)%0A%7B%0A++++std::cout+%3C%3C+%22const_lvalue_ref:+%22+%3C%3C+const_lvalue_ref+%3C%3C+%22%5Cn%22%3B%0A%7D%0A%0Aint+main()%0A%7B%0A++++int+x+%3D+1%3B%0A++++func(x)%3B%0A%0A++++const+int+y+%3D+2%3B%0A++++func(y)%3B%0A%7D&#39;),l:&#39;5&#39;,n:&#39;1&#39;,o:&#39;C%2B%2B+source+%231&#39;,t:&#39;0&#39;)),k:28.696820966819853,l:&#39;4&#39;,m:70.03802800519851,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;),(g:!((h:executor,i:(argsPanelShown:&#39;1&#39;,compilationPanelShown:&#39;0&#39;,compiler:clang1400,compilerName:&#39;&#39;,compilerOutShown:&#39;0&#39;,execArgs:&#39;&#39;,execStdin:&#39;&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,libs:!(),options:&#39;-std%3Dc%2B%2B20+-O0&#39;,overrides:!(),runtimeTools:!(),source:1,stdinPanelShown:&#39;1&#39;,tree:0,wrap:&#39;0&#39;),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;Executor+x86-64+clang+14.0.0+(C%2B%2B,+Editor+%231)&#39;,t:&#39;0&#39;)),header:(),l:&#39;4&#39;,m:29.961971994801484,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),k:70.33434650455929,l:&#39;3&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;),(g:!((h:compiler,i:(compiler:clang1400,filters:(b:&#39;0&#39;,binary:&#39;1&#39;,binaryObject:&#39;1&#39;,commentOnly:&#39;0&#39;,debugCalls:&#39;1&#39;,demangle:&#39;0&#39;,directives:&#39;0&#39;,execute:&#39;1&#39;,intel:&#39;0&#39;,libraryCode:&#39;1&#39;,trim:&#39;1&#39;,verboseDemangling:&#39;0&#39;),flagsViewOpen:&#39;1&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,libs:!(),options:&#39;-std%3Dc%2B%2B20+-O0&#39;,overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;+x86-64+clang+14.0.0+(Editor+%231)&#39;,t:&#39;0&#39;)),k:29.665653495440733,l:&#39;4&#39;,m:100,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),l:&#39;2&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;)),version:4">link</a>). You’ll find that when dealing with the same value type (i.e. — lvalues), overload resolution favors functions with parameters of the same constness as the arguments.</p><p>But what about when passing an rvalue argument into an overloaded function?</p><p>Well, from our chart above, we know acceptable function overloads would accept parameters of type</p><ul><li>const lvalue reference,</li><li>rvalue reference, or</li><li>const rvalue reference</li></ul><p>Consider our running Object example. You may have noticed there’s really no reason the lvalue copy constructor or lvalue copy assignment operator need to expect non-const parameters — in fact, they probably should be const for the sake of clean code!</p><p>But if we make that change, we need to know how the compiler will resolve those overloads at compile time.</p><p>Let’s throw together a simplified example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d45d40037154f12a458cb16490bdb7b9/href">https://medium.com/media/d45d40037154f12a458cb16490bdb7b9/href</a></iframe><p>Copying this code snippet into Compiler Explorer (<a href="https://godbolt.org/#g:!((g:!((g:!((g:!((h:codeEditor,i:(filename:&#39;1&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,selection:(endColumn:2,endLineNumber:21,positionColumn:2,positionLineNumber:21,selectionStartColumn:2,selectionStartLineNumber:21,startColumn:2,startLineNumber:21),source:&#39;%23include+%3Ciostream%3E%0A%0Avoid+func(const+int%26+const_lvalue_ref)%0A%7B%0Astd::cout+%3C%3C+%22const_lvalue_ref:+%22+%3C%3C+const_lvalue_ref+%3C%3C+%22%5Cn%22%3B%0A%7D%0A%0Avoid+func(int%26%26+rvalue_ref)%0A%7B%0Astd::cout+%3C%3C+%22rvalue_ref:+%22+%3C%3C+rvalue_ref+%3C%3C+%22%5Cn%22%3B%0A%7D%0A%0Avoid+func(const+int%26%26+const_rvalue_ref)%0A%7B%0Astd::cout+%3C%3C+%22const_rvalue_ref:+%22+%3C%3C+const_rvalue_ref+%3C%3C+%22%5Cn%22%3B%0A%7D%0A%0Aint+main()%0A%7B%0A%09func(5)%3B%0A%7D&#39;),l:&#39;5&#39;,n:&#39;1&#39;,o:&#39;C%2B%2B+source+%231&#39;,t:&#39;0&#39;)),k:28.696820966819853,l:&#39;4&#39;,m:70.03802800519851,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;),(g:!((h:executor,i:(argsPanelShown:&#39;1&#39;,compilationPanelShown:&#39;0&#39;,compiler:clang1400,compilerName:&#39;&#39;,compilerOutShown:&#39;0&#39;,execArgs:&#39;&#39;,execStdin:&#39;&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,libs:!(),options:&#39;-std%3Dc%2B%2B20+-O0&#39;,overrides:!(),runtimeTools:!(),source:1,stdinPanelShown:&#39;1&#39;,tree:0,wrap:&#39;0&#39;),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;Executor+x86-64+clang+14.0.0+(C%2B%2B,+Editor+%231)&#39;,t:&#39;0&#39;)),header:(),l:&#39;4&#39;,m:29.961971994801484,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),k:70.33434650455929,l:&#39;3&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;),(g:!((h:compiler,i:(compiler:clang1400,filters:(b:&#39;0&#39;,binary:&#39;1&#39;,binaryObject:&#39;1&#39;,commentOnly:&#39;0&#39;,debugCalls:&#39;1&#39;,demangle:&#39;0&#39;,directives:&#39;0&#39;,execute:&#39;1&#39;,intel:&#39;0&#39;,libraryCode:&#39;1&#39;,trim:&#39;1&#39;,verboseDemangling:&#39;0&#39;),flagsViewOpen:&#39;1&#39;,fontScale:20,fontUsePx:&#39;0&#39;,j:1,lang:c%2B%2B,libs:!(),options:&#39;-std%3Dc%2B%2B20+-O0&#39;,overrides:!(),selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1),l:&#39;5&#39;,n:&#39;0&#39;,o:&#39;+x86-64+clang+14.0.0+(Editor+%231)&#39;,t:&#39;0&#39;)),k:29.665653495440733,l:&#39;4&#39;,m:100,n:&#39;0&#39;,o:&#39;&#39;,s:0,t:&#39;0&#39;)),l:&#39;2&#39;,n:&#39;0&#39;,o:&#39;&#39;,t:&#39;0&#39;)),version:4">link</a>), we find the output to be</p><pre>rvalue_ref: 5</pre><p>This makes sense. We should probably expect the general rule of thumb for overload resolution that of the potential valid argument-parameter matches:</p><ul><li>values of the same type and constness are most favored</li><li>followed by values of the same type, but different constness</li><li>followed by values of different type</li></ul><p>Simply commenting out the rvalue reference overload (func(int&amp;&amp; rvalue_ref)) in our example yields the output,</p><pre>const_rvalue_ref: 5</pre><p>which confirms the above rule of thumb.</p><p>For reference, here is the binding chart from above modified for overload resolution priority:</p><figure><img alt="Chart created in Google Slides displaying reference types and the associated value types they can bind to along with overload resolution priority." src="https://cdn-images-1.medium.com/max/974/1*V_KUUdUpdfKAAgn6x0uBKA.png" /><figcaption>Chart created in Google Slides displaying reference types and the associated value types they can bind to along with overload resolution priority.</figcaption></figure><h3><strong>What’s Next?</strong></h3><p>If you’re following along with this Move Semantics series, you might be like, <em>“okay, so we can pass an rvalue into the rvalue copy constructor or copy assignment operator to initiate a transfer resource ownership, but what if we want to transfer ownership of resource from an lvalue?”</em></p><p>That’s a good question, and quite frankly, transferring ownership from lvalues is usually the goal when using move semantics in practice.</p><p>Now that you’re a pro on lvalues, rvalues, lvalue references, rvalue references, and understand how the transfer of ownership of resources actually works, there’s a few more topics we need to address:</p><ul><li>Universal references (yep! there’s another important reference type 😅)</li><li>Perfect Forwarding (std::forward)</li><li>Forcing lvalues to transfer ownership (std::move)</li><li>When to use (and <em>not</em> use) move semantics</li></ul><p>In the next two articles of this series, I’ll demystify these topics so you know when, where, how, and why to use move semantics and related tools.</p><h3><strong>Sources &amp; More Info</strong></h3><ul><li><em>Effective Modern C++</em> by Scott Meyers, Items 23–25, 28</li><li><a href="http://thbecker.net/articles/rvalue_references/section_01.html">“C++ Rvalue References Explained” by Thomas Becker</a></li><li><a href="https://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c/">“Understanding lvalues and rvalues in C and C++” by Eli Bendersky</a></li></ul><h3><strong>Collaborators</strong></h3><p>A special thanks to a few friends who took time to edit and review this post:</p><ul><li>Mason Wilie (<a href="https://www.linkedin.com/in/mason-wilie/">LinkedIn</a>)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80a4fd5f64d6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Move Semantics Pt. 1 — Lvalues, Rvalues & A Case For Shallow Copies]]></title>
            <link>https://jaredmil.medium.com/move-semantics-pt-1-lvalues-rvalues-a-case-for-shallow-copies-b66c3504c1c3?source=rss-b54a8b7d894c------2</link>
            <guid isPermaLink="false">https://medium.com/p/b66c3504c1c3</guid>
            <category><![CDATA[c-plus-plus-language]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[move-semantics]]></category>
            <category><![CDATA[cplusplus]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Jared Miller]]></dc:creator>
            <pubDate>Mon, 15 Jul 2024 13:11:17 GMT</pubDate>
            <atom:updated>2024-07-15T13:11:17.073Z</atom:updated>
            <content:encoded><![CDATA[<h3>C++ Beyond the Syllabus #5: Move Semantics Pt. 1 — Lvalues, Rvalues &amp; A Case For Shallow Copies</h3><h4><em>If you cut corners learning move semantics, other engineers will know.</em></h4><p><em>This article is a part of the </em><a href="https://jaredmil.medium.com/"><em>C++ Beyond the Syllabus</em></a><em> series. </em><a href="https://jaredmil.medium.com/subscribe"><em>Subscribe here</em></a><em> to receive each new issue directly in your inbox.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*aA_ZvHQG133xuOUNZJucEg.png" /></figure><p>Understanding lvalues and rvalues is not generally something you <em>need</em> to know to write code that produces the correct output…</p><p>But if you’re using C++, you also care about performance, and being comfortable working with these value types is a necessary prerequisite to understanding advanced, performance-related, modern C++ topics like move semantics and perfect forwarding.</p><p>By the end of this article, you’ll understand what lvalues and rvalues really are and how to distinguish between them, along with a little sneak peak at why they matter in the context of move semantics.</p><h3><strong>Lvalues &amp; Rvalues</strong></h3><h4><strong>Explain It Like I’m a 5 Year Old</strong></h4><p>Lvalues:</p><ul><li>Imagine you have a stuffed animal that you keep on your shelf… you give it a really cute name, like Stuffy or Snuggles, or better yet: <em>lvalue</em></li><li>You know exactly where you keep <em>lvalue</em> on your shelf.</li><li>You can pick <em>lvalue</em> up and play with it, but you always put it back in its spot on the shelf.</li></ul><p>Rvalues:</p><ul><li>Unlike your stuffed animal <em>lvalue</em>, rvalues are more like a bubble</li><li>You can look at the bubble, but you can’t pick up the bubble or save it for later</li><li>The bubble is fun and temporary — it will pop soon and will disappear</li></ul><h4><strong>Let’s Get More Technical</strong></h4><p>While compiler implementations and optimizations may vary, the theoretical gist of lvalues and rvalues in C++ remains the same.</p><p>Every variable or literal is either an lvalue or an rvalue.</p><p>An lvalue (<em>locator value </em>or <em>left hand value</em>) is stored in some stable memory location. It can be accessed and modified throughout its lifetime via its memory address.</p><p>An rvalue (<em>read value</em> or <em>right hand value</em>) is temporary. It does not have a memory address and disappears after the end of the expression it appears in (i.e. — at the next semicolon).</p><h4><strong>In English, Please?</strong></h4><p>The two main ways to identify some value as an lvalue is if…</p><ul><li>the variable has a name, or</li><li>you can take the address of the value with the <a href="https://en.cppreference.com/w/cpp/language/operator_member_access">address-of operator</a> (&amp;)</li></ul><p>Rvalues are defined as the converse of the lvalue definition… if a value is not an lvalue, then it is an rvalue.</p><p>If it helps, here’s another distinction:</p><ul><li>an lvalue can appear on either side of an assignment operator (=)</li><li>an rvalue may only ever appear on the right hand side of an assignment operator</li></ul><h4><strong>Let’s See Some Examples</strong></h4><p>For starters…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75b3d6da099241c59c608b0e37623923/href">https://medium.com/media/75b3d6da099241c59c608b0e37623923/href</a></iframe><p>Here’s an example of an lvalue without a name…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d959a32462d2086bf1445f9efcaff2fd/href">https://medium.com/media/d959a32462d2086bf1445f9efcaff2fd/href</a></iframe><p>Here’s an example of a triple whammy: a value that is definitely an rvalue…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1896ab84e642e79b9e36a932ff78def4/href">https://medium.com/media/1896ab84e642e79b9e36a932ff78def4/href</a></iframe><p>At this point, you’re probably thinking “this isn’t that hard, what’s all the hype about?”</p><h4><strong>Why do these matter?</strong></h4><p>Like a lot of people, my intuition was that these value types play into the C++ memory model; something along the lines of:</p><ul><li>lvalues can have their address taken, which means they must be stored in memory</li><li>rvalues can’t, so maybe that means they live only in registers</li></ul><p>But, the C++ standard leaves almost all implementation details up to compiler developers, providing them freedom to reorganize and optimize your code. While the above intuition is conceptually correct, the standard allows compilers to store values wherever they’d like, as long as the interface of these value types does not change.</p><p>So if it’s not for the memory model, what is it?</p><p>In practice, the distinction between lvalues and rvalues enables functions to branch at compile time via overload resolution.</p><p>Before we get into that though, let’s take a look at the motivation for all of this.</p><h4><strong>The Motivation: An Argument For Shallow Copies</strong></h4><p>Remember “deep copies”?</p><p>As a refresher, this is when you have some type like a std::string or std::vector, which stores a pointer to allocated data on the heap. The implementation of this type then must make sure that both the copy constructor and copy assignment operator “deep copy” the data on the heap owned by the <em>copied-from</em> object and then set the <em>copied-to</em> object’s data pointer to the newly made copy of the data on the heap. This prevents a whole host of problems, like unintended updates to data, access after deletion, double deletion, etc.</p><p>Great, we’re all caught up.</p><p>But what if we <em>don’t</em> want a deep copy?</p><p>Well, when wouldn’t we want a deep copy — doesn’t that just protect us from all the terrible side effects listed above?</p><p>Consider this ultra-simple and rather hand-wavy example…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f834607c9f77a0adbf6f08c48ad240c/href">https://medium.com/media/4f834607c9f77a0adbf6f08c48ad240c/href</a></iframe><p>In the example above, the std::string copy constructor will initialize str2 with a deep copy of the ~incredibly long string~ owned by str1.</p><p>Being an ~incredibly long string~, you might imagine this would take a toll on performance. You also might realize this performance hit is entirely unneeded because str1 is never used again after line 6!</p><p>Instead of creating a deep copy, we can just transfer ownership of the ~incredibly long string~ from str1 to str2. In doing this, str2 can just <em>shallow copy</em> str1’s data pointer and we can set str1’s data pointer to nullptr, which would be a much smaller performance hit than allocating the entire ~incredibly long string~ again.</p><p>This actual transfer of ownership is called <em>move semantics</em>.</p><p>For now, think about this:</p><p>If we have one function overload to perform a deep copy and another to perform a transfer of ownership, how can we instruct the compiler which one we want to use at compile time?</p><p>The answer: <em>rvalue references</em>.</p><h3><strong>What’s Next?</strong></h3><p>Next up, I’ll cover rvalue references, the Rule of Five, universal references, and finally, move semantics!</p><p>Move semantics is an essential tool in the C++ programmer’s toolbox. It can reduce unnecessary copies and allocations in your code, which often are the largest latency contributors.</p><p>The catch:</p><p>Move semantics is tricky! It is hard to understand and even harder to get right. It’s one of those topics that if you take a shortcut while learning, you’ll quickly realize you need to go back to the drawing board.</p><p>Having learned the ropes of lvalues &amp; rvalues gives you a solid foundation to understand move semantics the right way, once and for all!</p><h3><strong>Sources &amp; More Info</strong></h3><ul><li><em>Effective Modern C++</em> by Scott Meyers, Items 23–25, 28</li><li><a href="http://thbecker.net/articles/rvalue_references/section_01.html">“C++ Rvalue References Explained” by Thomas Becker</a></li><li><a href="https://eli.thegreenplace.net/2011/12/15/understanding-lvalues-and-rvalues-in-c-and-c/">“Understanding lvalues and rvalues in C and C++” by Eli Bendersky</a></li></ul><h3><strong>Collaborators</strong></h3><p>A special thanks to a few friends who took time to edit and review this post:</p><ul><li>Janay Bengal (<a href="https://www.linkedin.com/in/janay-bengal/">LinkedIn</a>)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b66c3504c1c3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[RAII & C++ Smart Pointers]]></title>
            <link>https://jaredmil.medium.com/c-beyond-the-syllabus-4-raii-smart-pointers-7c784134ace5?source=rss-b54a8b7d894c------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c784134ace5</guid>
            <category><![CDATA[cplusplus]]></category>
            <category><![CDATA[memory-leak]]></category>
            <category><![CDATA[smart-pointer]]></category>
            <category><![CDATA[raii]]></category>
            <category><![CDATA[memory-management]]></category>
            <dc:creator><![CDATA[Jared Miller]]></dc:creator>
            <pubDate>Wed, 03 Jul 2024 17:20:39 GMT</pubDate>
            <atom:updated>2024-07-08T16:20:31.754Z</atom:updated>
            <content:encoded><![CDATA[<h3>C++ Beyond the Syllabus #4: RAII &amp; Smart Pointers</h3><h4>Say goodbye to memory leaks.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2rWUV8HqnYHBoXAw" /></figure><p>Say goodbye to memory leaks… and say hello to your new friends, RAII and smart pointers!</p><p>The new and delete keywords are taught in 100-level Intro to CS courses. They are fundamental building blocks of C++ programming, but are <em>rarely</em> ever used in industry.</p><p>When it comes to real world C++ development, RAII and smart pointers are essential for code cleanliness, readability, and maintainability. Not to mention, they will drastically decrease your chances of memory leaks.</p><p>Let’s jump into it.</p><h4><strong>Raw Pointers Aren’t Cutting It</strong></h4><p>As a reminder, the new and delete keywords help programmers interact with memory. Without these keywords objects are (generally) allocated on the stack. When paired with new an object will be allocated on the heap. This new operator returns a <em>raw</em> pointer to the created object:</p><pre>MyObject *object_ptr = new MyObject();</pre><p>Objects created on the heap do not automatically go out of scope and get destructed like objects on the stack do. You have to manually apply the delete keyword when you are done using the heap-allocated object:</p><pre>delete object_ptr;</pre><p>For arrays, you’ll need the bracket operator [] along with the new and delete keywords.</p><p>There are so many ways this can go wrong:</p><ul><li>If you forget the delete keyword, you’ll end up with memory leaks, which can make your program slower, crash with cryptic errors, and/or exhibit undefined behavior.</li><li>If you double-delete, you’ll end up with undefined behavior (usually your program crashing).</li><li>Conditional code paths can require a delete at the end of each path.</li><li>After deleting a pointer, you’re left with a dangling pointer unless you manually set it to nullptr.</li><li>If an exception is thrown between new and delete, the latter will not be reached.</li></ul><p>RAII is the solution.</p><h4><strong>What’s RAII?</strong></h4><p>RAII stands for <em>Resource Acquisition Is Initialization</em>. While that might seem a bit ambiguous at first, it’s actually quite a simple concept.</p><p>There are many instances when an object’s initialization and cleanup need to be performed upon the creation and destruction of the object.</p><p>If you’re thinking something like <em>“Wait, isn’t that what constructors and destructors are for?”</em>, then you’re catching on quickly.</p><p>RAII is simply a design idiom where resource management is tied to the lifetime of objects. In this approach, we create a wrapper object for some resource (usually memory, file handles, network connections, etc). This wrapper object’s…</p><ul><li>constructor acquires and initializes the resource, and</li><li>destructor cleans up and releases the resource.</li></ul><p>This makes the clean up and release of the resource automatic when the wrapper object goes out of scope.</p><p>Let’s build a super simple RAII wrapper to help manage heap-allocated objects by abstracting away the new and delete keywords…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14814fdd206b0d880d3cad591bd9a00b/href">https://medium.com/media/14814fdd206b0d880d3cad591bd9a00b/href</a></iframe><p>With this example, memory management is almost trivial.</p><p>When execution returns from the main function, SimpleResourceWrapper is destructed, which will invoke the delete operator on the MyObject pointer. Aside from the implementation of this wrapper, there should be no need to manually use the delete keyword in our code.</p><p>Something doesn’t quite seem right with the above implementation though… Why do we still have the new keyword in our usage of the RAII wrapper? Shouldn’t that be abstracted away as well?</p><p>We’ll address this in the <em>“Unique Pointer”</em> section below.</p><p>The good news is if you see the value of SimpleResourceWrapper, you pretty much already understand the value proposition of smart pointers.</p><h4><strong>Smart Pointers</strong></h4><p>There are three types of smart pointers: <em>unique</em>, <em>shared</em>, and <em>weak</em>.</p><p>Each type is a memory managing RAII wrapper around pointers that provides some assurances about how it will manage the memory and pointer lifetime.</p><p>The smart pointer library can be included via #include &lt;memory&gt;.</p><h4><strong>Unique Pointer</strong></h4><p>This is the most intuitive smart pointer. It is essentially a glorified version of our SimpleResourceWrapper discussed above.</p><p>When std::unique_ptr goes out of scope, its destructor applies the delete operator to the memory it manages.</p><p>Alluded to by its name, std::unique_ptr provides <em>unique</em> ownership of the memory. This means with correct usage, there will only ever be <em>one</em> smart pointer managing the underlying raw pointer. Because of that, unique pointers cannot be copied; however, ownership of a managed raw pointer can be transferred to another unique pointer.</p><p>So when should you use a unique pointer?</p><p>Unless you need multiple references to the same dynamically allocated object, you should use a unique pointer. This will enforce that you don’t accidentally make copies of the pointer &amp; ensure that delete is properly applied when the pointer goes out of scope.</p><p>Here are a few example use cases…</p><p>Let’s say you only need a managed object from within the scope it is created (and any helper functions called from that scope):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/164a505ecb3816965d6120634140d6bb/href">https://medium.com/media/164a505ecb3816965d6120634140d6bb/href</a></iframe><p>In the above example, HelperFunction takes in a reference to a unique pointer. This is important!</p><p>If the function parameter was not a reference, it would require the unique pointer copy constructor. As discussed above, though, copies are not allowed on unique pointers (the copy constructor is deleted!), so it would cause a compiler error.</p><p>I did mention above that ownership can be transferred, when might we want to do that? There are two ways that unique pointer ownership can be transferred. (Both of these are rather involved topics, which I’ll be writing posts on in the coming weeks. For now, I’ve linked external resources and provided a TLDR for each.)</p><ol><li>Return Value Optimization (RVO) (<a href="https://en.cppreference.com/w/cpp/language/copy_elision">a type of copy elision</a>)</li><li><a href="https://en.cppreference.com/w/cpp/utility/move">Move semantics</a></li></ol><p>RVO is a compiler optimization used throughout the C++ language, not just with unique pointers. Simply put, when a function creates and returns an object, the object will be constructed directly in the final location where the return value is used, rather than creating and copying a temporary object.</p><p>Move semantics (usage of std::move) instruct the compiler to permanently transfer ownership of some object stored on the heap. In the context of unique pointers, move semantics can be used like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4833b0455ac88ff33118a067f46c245c/href">https://medium.com/media/4833b0455ac88ff33118a067f46c245c/href</a></iframe><p>In the above example, DriverFunction transfers ownership of object_ptr1 to HelperFunction’s object_ptr2 <em>without</em> copying the underlying instance of MyObject. Effectively, this means that object_ptr1 will no longer be responsible for applying the delete operator on the underlying raw pointer when it goes out of scope; that responsibility now falls on object_ptr2.</p><p>Above, I mentioned we’d address how to get rid of the new keyword.</p><p>std::unique_ptr has a factory function, std::make_unique, which safely and efficiently constructs instances of std::unique_ptr without needing the new keyword. You can use this factory function in either of the ways below:</p><pre>std::unique_ptr&lt;MyObject&gt; object_ptr = std::make_unique&lt;MyObject&gt;();</pre><p>or</p><pre>std::unique_ptr&lt;MyObjectWithArgs&gt; object_ptr = <br>    std::make_unique&lt;MyObjectWithArgs&gt;(arg1, arg2);</pre><p>How std::make_unique works under the hood is beyond the scope of this article. I’ll be circling back to this in the coming weeks though, after deep dives on a handful of prerequisites like RVO, move semantics, <a href="https://en.cppreference.com/w/cpp/language/variadic_arguments">variadic arguments</a>, and <a href="https://en.cppreference.com/w/cpp/utility/forward">perfect forwarding</a>.</p><p>Now that you’re familiar with std::unique_ptr, shared and weak pointers will be a breeze.</p><h4><strong>Shared Pointer</strong></h4><p>Shared pointers behave very similarly to unique pointers, except without the assurance of exclusivity.</p><p>Multiple shared pointers can be created to refer to the <em>same</em> managed object (usually via the std::shared_ptr copy constructor or copy assignment operator). The delete operator is applied once <em>all</em> shared pointers referring to the same object go out of scope.</p><p>Let’s jump right into a code example…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/818588bd42931a84518eff65989ed7ad/href">https://medium.com/media/818588bd42931a84518eff65989ed7ad/href</a></iframe><p>Notice how we use the std::make_shared factory function for shared pointers.</p><p>This code will behave the same as a unique pointer in terms of the managed object’s lifetime. The difference is that shared pointers store a reference count along with the managed object. When we first create the shared pointer above, it will have a reference count of one. Our memory diagram would look something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tIOofKIVTyiZTiPP" /></figure><p>Let’s write a more complex code snippet…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c9a6ad3f159ab77cfb5349857a2c5e0/href">https://medium.com/media/7c9a6ad3f159ab77cfb5349857a2c5e0/href</a></iframe><p>The following chain of events occurs relating to shared pointers in the above example:</p><ul><li>node is created in SomeFunction on line 20.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LTVrpgDz7jIq--tYS4Zlkw.png" /></figure><ul><li>The ContainerObject::SetPtr call on line 22 invokes the shared pointer copy constructor on line 11. This increases the reference count of the underlying managed Node object to 2.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*U5RTk2gOOhU08DHq" /></figure><ul><li>std::move in ContainerObject::SetPtr will transfer ownership from arg_node to container_node_. arg_node is still within scope and is set to nullptr. This operation will not change the reference count.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FvSCsWqJVQd3otxf" /></figure><ul><li>When execution returns from Container::SetPtr to SomeFunction, arg_node goes out of scope. This does not affect the managed Node object, as the shared pointer was no longer managing Node.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*pPJMFXpIyIy5zSX3" /></figure><ul><li>When execution returns from SomeFunction to main, the original node shared pointer (created on line 20) goes out of scope. This decreases the reference count to 1.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*NAh5zh3rHNHeDAUD" /></figure><ul><li>Lastly, main returns, destructing its local variable container. This causes container_node_ to go out of scope. When container_node_ is destructed, the shared pointer’s reference count becomes 0, and the delete operator is applied to the managed Node object.</li></ul><p>At this point, you might be wondering <em>“why don’t we always use shared pointers, as they behave the same as unique pointers if we never make copies of it?”</em></p><p>Well, two reasons:</p><ol><li>std::shared_ptr has some performance overhead because of the reference count. Every time a shared pointer is constructed or destructed, code execution has to synchronize with any other threads in your program to atomically update the reference count. This synchronization cost can add up fast in a busy application.</li><li>Performance aside, if a resource should logically only have one owner (like a database or network connection, for example), then representing it with a shared pointer is misleading and can allow unknowing developers to introduce bugs to your application. Representing uniquely owned resources with unique pointers is more clear.</li></ol><h4><strong>Weak Pointer</strong></h4><p>Weak pointers are used in conjunction with shared pointers.</p><p>TLDR — Weak pointer can be thought of as a variation of shared pointer that does not contribute to the reference count.</p><p>The textbook example of weak pointers is to represent cycles in graphs, while avoiding memory leaks caused by circular dependencies.</p><p>Let’s consider a tree where each node holds child and parent pointers. Our minimal example only needs two nodes: nodeA is the root of our tree and the parent of nodeB.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/0*K-5GkpIAWN1t-Kq9" /></figure><p>To illustrate the need for a better solution, let’s see why an implementation using only shared pointers won’t suffice…</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c39fedf4cc17bf7e0707e41920de5636/href">https://medium.com/media/c39fedf4cc17bf7e0707e41920de5636/href</a></iframe><p>Do you notice anything that will go wrong here?</p><p>Let’s walk through what exactly is going on…</p><ul><li>We construct a shared pointer called nodeA along with an instance of Node, which lives on the heap. This new Node object will have a reference count of 1.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9gQn7qbtPowTAHhn" /></figure><ul><li>Same as above, but with nodeB.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6S9OszzctoDCI6bR" /></figure><ul><li>We set the shared pointer nodeA::child to point to the managed Node object represented by the shared pointer nodeB, increasing that object’s reference count to 2.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VtfuDWYFLdDbEJXd" /></figure><ul><li>Similar to above, we set the shared pointer nodeB::parent to point to the managed Node object represented by the shared pointer nodeA, increasing that object’s reference count to 2.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0RCLWy-Ofah4iVJc" /></figure><ul><li>Now, execution reaches the end of SomeFunction, destructing the shared pointers nodeA and nodeB. Upon destruction, these shared pointers decrement the reference count of their managed objects, but they do not clean up those objects, because the reference counts are still non-zero.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*4L-Y8fB_wmE9G8gz" /></figure><p>We have a memory leak 😰…</p><p>Isn’t that what smart pointers were supposed to help resolve?</p><p>Weak pointers to the rescue!</p><p>First, we need to recognize that the real issue above was the circular dependency between two shared pointers. To fix this, we just need to break the circular dependency by replacing the shared pointers with weak pointers in one direction of the graph.</p><p>Let’s say we (arbitrarily) want parent nodes to strongly own their children, and children to weakly own their parents. Our modified code will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d84fe8af2d8ccd8f558aa2b0cf6e266/href">https://medium.com/media/0d84fe8af2d8ccd8f558aa2b0cf6e266/href</a></iframe><p>Note how in the above implementation, both variables nodeA and nodeB in main are still shared pointers even though the object pointed to by nodeB is the child of the object pointed to by nodeA.</p><p>Let’s take a deeper look at this modified implementation…</p><ul><li>Our memory diagram below shows the state just before returning from main. Notice how the reference count of the managed object Node (A) is only 1 because the weak pointer referencing the object does not contribute to the reference count.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EXthM5CRGvUQp9-u" /></figure><ul><li>When we do return from main, the nodeA and nodeB variables will be destructed, reducing each of the managed Node objects’ reference counts by 1.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*JC50NrdJ-ZAFhs4i" /></figure><ul><li>Still in the process of returning from main, the Node object representing the parent node Node (A) now has a reference count of zero, causing the delete operator to be applied, which destructs and deallocates the object.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*b92K2_rHcWllSvY7" /></figure><ul><li>In deleting the parent node, the child node’s reference count decremented to zero, and thus Node (B) will also have the delete operator applied.</li></ul><p>The above chain of events all happens while returning from main in our implementation utilizing weak pointers.</p><p>Memory leak resolved!</p><h4><strong>Wrapping Up</strong></h4><p>(pun intended 😉)</p><p>Smart pointers are powerful tools that drastically increase engineer productivity by reducing the need to track every new and delete keyword.</p><p>There are many instances of RAII throughout C++ (and software development in general). Now that you’re familiar with the concept, you’ll begin to notice this popping up all over the place.</p><p>One of the most common use cases of RAII aside from smart pointers is std::lock_guard, which automatically acquires and releases a std::mutex when writing multi-threaded applications — more on that in a future post, though 🤓.</p><h4>What’s Next?</h4><p>As I mentioned earlier, there are quite a few building blocks required to fully understand std::make_unique. Those same building blocks happen to be the foundation of many advanced topics in real world C++.</p><p>Beginning next week, we’ll start to dive into each of those building blocks to achieve a more well-rounded understanding.</p><p>If you found this post useful, please <em>clap</em> &amp; subscribe!</p><p>When you subscribe, you’ll receive <em>C++ Beyond the Syllabus</em> for free, directly in your inbox.</p><h4><strong>Sources &amp; More Info</strong></h4><ul><li><a href="https://en.cppreference.com/w/cpp/memory/unique_ptr">C++ Docs — unique_ptr</a></li><li><a href="https://en.cppreference.com/w/cpp/memory/shared_ptr">C++ Docs — shared_ptr</a></li><li><a href="https://en.cppreference.com/w/cpp/memory/weak_ptr">C++ Docs — weak_ptr</a></li><li>University of Michigan — EECS 482 — Discussion of RAII</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c784134ace5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Micro-Benchmarking with Quick Bench]]></title>
            <link>https://jaredmil.medium.com/micro-benchmarking-c-with-quick-bench-8788b1edcf79?source=rss-b54a8b7d894c------2</link>
            <guid isPermaLink="false">https://medium.com/p/8788b1edcf79</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[cpp]]></category>
            <category><![CDATA[code-performance]]></category>
            <category><![CDATA[benchmark]]></category>
            <dc:creator><![CDATA[Jared Miller]]></dc:creator>
            <pubDate>Thu, 27 Jun 2024 18:36:59 GMT</pubDate>
            <atom:updated>2024-07-08T13:58:51.945Z</atom:updated>
            <content:encoded><![CDATA[<h3>C++ Beyond the Syllabus #3: Micro-Benchmarking with Quick Bench</h3><h4>You’ve identified a section of code you’d like to speed up… now what?</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vz0emVxfkyfVjWce" /></figure><p>You’ve identified a section of code you’d like to speed up… now what?</p><p>Familiarity with micro-benchmarking will significantly enhance your efficacy.</p><p>Micro-benchmarking allows you to do two main things:</p><ol><li>Compare performance of different code implementations</li><li>Identify what functions/lines use the most CPU time</li></ol><p>In this post, we’ll explore Quick Bench, a web-based tool designed to compare different C++ code implementations.</p><blockquote>In school, we are taught to compare algorithms based on their Big-O complexity. In industry, code performance is far more nuanced.</blockquote><p>Quick Bench helps you get a feel for the constant factor between two or more implementations with the same time complexity.</p><p>The action described by objective #2 is called <em>profiling</em>, which is super interesting, but is outside the scope of this article. Here are a few tools to use for profiling:</p><ul><li>perf (<a href="https://perf.wiki.kernel.org/index.php/Main_Page">linux</a>)</li><li>gprof (<a href="https://ftp.gnu.org/old-gnu/Manuals/gprof-2.9.1/html_mono/gprof.html">GNU</a>)</li><li>TCMalloc and gperftools (<a href="https://google.github.io/tcmalloc/gperftools.html">linux, windows, older macOS versions</a>)</li><li>Valgrind (<a href="https://google.github.io/tcmalloc/gperftools.html">GNU</a>)</li><li>Intel VTune Profiler (<a href="https://www.intel.com/content/www/us/en/docs/vtune-profiler/get-started-guide/2024-0/overview.html">linux, windows</a>, <a href="https://www.intel.com/content/www/us/en/docs/vtune-profiler/cookbook/2024-2/viewing-analysis-results-on-web-browser.html">online tool</a>)</li><li>magic-trace (<a href="https://github.com/janestreet/magic-trace">linux</a>)</li></ul><p>Before we dive into Quick Bench, let’s clarify a few things micro-benchmarking <em>is not</em> good for…</p><ul><li>absolute runtime estimates for code snippets</li><li><em>always</em> claims like “implementation A is <em>always</em> faster than implementation B”</li><li>insights on entire application performance or large code segments</li></ul><p>If you haven’t already, check out last week’s post, <a href="https://jaredmil.medium.com/c-beyond-the-syllabus-2-intro-to-benchmarking-and-macro-benchmarking-deep-dive-a465fc73a604">Intro to Benchmarking &amp; Macro-Benchmarking Deep Dive</a>, to see how macro-benchmarking can help you narrow down on smaller code segments to micro-benchmark.</p><p><a href="https://jaredmil.medium.com/c-beyond-the-syllabus-2-intro-to-benchmarking-and-macro-benchmarking-deep-dive-a465fc73a604">C++ Beyond the Syllabus #2: Macro-Benchmarking Deep Dive</a></p><p>One of the best ways to learn the thing, is to do the thing! You can follow along at <a href="http://quick-bench.com/">quick-bench.com</a>.</p><h4><strong>Setting Your Configuration</strong></h4><p>When you first open up Quick Bench, you’ll see something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*icOjelBfZMGVjR0g" /></figure><p>You will want to configure your Quick Bench environment to match your production environment. Instructions on how to find all of this information for your setup can be found in the article linked below.</p><p><a href="https://jaredmil.medium.com/c-beyond-the-syllabus-1-peeking-under-the-hood-with-compiler-explorer-aaa8d08b2876">C++ Beyond the Syllabus #1: Compiler Explorer</a></p><p>A few additional configurations:</p><ul><li>STL = libc++(LLVM) - I use MacOS</li><li>Disassembly = None - We won’t be looking into assembly code today.</li><li>optim = O3 - We want our benchmarking environment to have the same compiler optimizations as our production environment (usually -O3 or -Ofast )… the one exception to this is that we don’t want the compiler optimizing away the code we’re trying to benchmark 😅 — we’ll address this throughout the post</li></ul><p>You can check out the article linked above for a refresher on (dis)assembly and compiler optimizations.</p><h4><strong>Library Syntax</strong></h4><p>Let’s start by breaking down the default example code as seen below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*aAhqW3RNojkinzqI" /></figure><p>This example consists of four main components:</p><p><strong><em>(A)</em></strong> — Function Definition</p><ul><li>All code to be benchmarked must live in some function that is later registered (see <strong><em>(D)</em></strong>).</li><li>The state argument here contains any inputs you may pass into the function. As discussed in the <em>Arguments</em> section below, you can instruct Quick Bench to use certain inputs to aid your testing.</li></ul><p><strong><em>(B)</em></strong>— State Loop</p><ul><li>The state loop runs many times, repeatedly measuring the contained code.</li><li>You can imagine implicit start and end timestamps being taken at the start and end of each loop iteration.</li><li>Anything outside of the loop is not measured.</li></ul><p><strong><em>(C)</em></strong>— Do Not Optimize Specifier</p><ul><li>Recall that compiler optimizations may completely remove unused code while generating assembly instructions. This line tricks the compiler into thinking the variable created_string is actually used somewhere.</li><li>I’ll go into more detail on this in the <em>Optimizations</em> section below.</li></ul><p><strong><em>(D) </em></strong>— Benchmark Registration</p><ul><li>You can include helper functions and develop alternate code implementations within the code editor.</li><li>Benchmarking will only be performed on a function once it is registered via this syntax.</li></ul><h4><strong>Interpreting Results</strong></h4><p>We can run a benchmark of the default example code by clicking the large green Run Benchmark button. From there, we see the following results.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SDrY9nmVsNs0c9iL" /></figure><p>These results show the StringCreation benchmark ran faster than the StringCopy benchmark. But what actually happened when we ran the benchmark? And how should we correctly interpret the results?</p><p>When we clicked Run Benchmark, the Quick Bench backend got to work. It sent our code to a cluster of AWS servers and ran a few things…</p><p>For each registered function, Quick Bench ran the state loop many times and discarded the first few iterations so the cache could “warm up”. The information we see is an average of each iteration.</p><p>Remember how I mentioned benchmarking isn’t great for absolute time estimates?</p><p>One reason for this is that our production environment usually isn’t the same as the benchmarking environment. In the case of Quick Bench, there could be hundreds of other users running their benchmarks at the same time as you, causing the AWS cluster to be slower than your production environment would be.</p><p>Alternatively, there could be no other users running benchmarks, and any absolute time estimates could yield faster results than your production app, which might have a lot of moving components that aren’t taken into account when micro-benchmarking.</p><p>Quick Bench accounts for this by only showing relative comparisons. Alongside your benchmarks, it will run a Noop (no operation) benchmark, that looks something like this (you can run this yourself to fact check! 🤓):</p><pre>static void ExampleNoop(benchmark::State&amp; state) {<br>  for (auto _ : state) {<br>    benchmark::DoNotOptimize(0);<br>  }<br>}<br>BENCHMARK(ExampleNoop);</pre><p>The expected absolute time of a Noop is the time we’d expect the CPU to complete a single operation. Quick Bench uses this Noop time as the common denominator when measuring benchmarks. Go ahead and click Show Noop bar at the bottom of the results and then hover over the blue bar representing StringCreation.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/874/0*Ls8i3Q-4EBjJWvE4" /></figure><p>You’ll see that our StringCreation benchmark was about 2x faster than StringCopy and about 4x slower than Noop.</p><p>If you’re like me, you might have a few questions:</p><ol><li>Why don’t we need benchmark::DoNotOptimize(copy); included in our StringCopy function?</li><li>With full optimizations enabled (our optim=O3 setting), is the string literal constructor (StringCreation) really over 2x faster than the string copy constructor (StringCopy)?</li></ol><p>These are some good questions… let’s dive into optimizations and compiler configurations a bit more.</p><h4><strong>Optimizations &amp; Compiler Configurations</strong></h4><p>Recall how I set the compiler configurations to use LLVM, Clang 14.0, C++20, and O3 to match my local development environment. With these configurations, we saw StringCreation perform 2.2x faster than StringCopy.</p><p>If we try out Clang 17.0, the most recent supported version, we get different results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/0*ixLxiXOAo2S8Ko9Q" /></figure><p>What?!? How can StringCopy now take effectively <em>no</em> CPU time?</p><p>Well, it seems likely that somewhere between Clang versions 14.0 and 17.0 the compiler developers added support to optimize away unused strings created by the copy constructor.</p><p>If we simply add in the missing benchmark::DoNotOptimize(copy); line mentioned above, we see much more believable results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1HNeO0JGnvsal8fU" /></figure><p>This is why it’s so important to…</p><ol><li>Instruct the compiler not to optimize anything that might remove the core of what you aim to benchmark, and</li><li>Make sure your benchmark environment configuration matches your production environment.</li></ol><p>Now that we’ve discussed this benchmark::DoNotOptimize function a few times. You’re probably wondering what it actually does.</p><p>If we had to guess, we might say this function performs some read operation on the argument. Then, the compiler wouldn’t optimize away the creation of the variable, because it is accessed later on.</p><p>The problem here is that we don’t want there to be an unnecessary read of the argument from within the state loop, because it would skew the measurements! As an extreme example, imagine passing some very large object that doesn’t fit in the cache as an argument to that implementation of benchmark::DoNotOptimize.</p><p>Quick Bench gets around this by just <em>tricking</em> the compiler into thinking the variable has some side effects, which the compiler itself might not know about. The actual nuances of the function are beyond the scope of this article, but it is super interesting, so I’d definitely recommend checking out benchmark::DoNotOptimize<a href="https://github.com/google/benchmark/blob/d8254bb9eb5f6deeddee639d0b27347e186e0a84/include/benchmark/benchmark.h#L308-L325">’s source code</a> and <a href="https://www.youtube.com/watch?v=nXaxk27zwlk&amp;t=2441s">Chandler Carruth’s explanation of it from CppCon 2015</a>.</p><h4><strong>Arguments &amp; Randomization</strong></h4><p>So we’ve covered most of the basics, but what we haven’t touched on so far are the arguments to our benchmark functions and randomization.</p><p>Let’s get to it…</p><p>There are a handful of ways to pass integer arguments to your functions in Quick Bench. Let’s start with a new code example measuring the benefits of std::vector::reserve (<a href="https://quick-bench.com/q/jUJ6QAeD6m-bQWLIrf2th0Ul1qs">Quick Bench link</a>):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lCDJWy6XWLtS743q" /></figure><p>Let’s go over a few important notes about this example…</p><ul><li>At point <strong><em>(0)</em></strong> on line 20 is the only difference between VecPushBack and VecReserve. The latter calls std::vector::reserve with the end-size of the vector, which avoids any resizing while inserting elements.</li><li>At points labeled <strong><em>(A)</em></strong>, we pass the value 4 into our benchmark functions.</li><li>Points labeled <strong><em>(B)</em></strong> access the argument via the state.range(0) syntax.</li></ul><blockquote>In Quick Bench, an argument passed into a registered benchmark function results in a single call to the function in which the state loop is run many times with the same argument.</blockquote><p>When we run this benchmark…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EUtmWEQRurl3mgjR" /></figure><p>We see VecReserve is faster than VecPushBack, which matches what we’d expect.</p><p>You can also see the argument passed into the benchmarks in each of the column labels (format RegisteredFunctionName/Arg).</p><p>This was a pretty simple example that only compared implementations for a single vector size.</p><p>If we were truly benchmarking this functionality, we’d want to test a variety of different inputs. To do this, we can chain arguments together on each benchmark call.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*hAU2iZ7DXaX5QwO8" /></figure><p>On the left of the above figure, you can see the chained argument syntax. Keep in mind that each individual argument passed in with this syntax results in a separate call to the registered function. Within each of those independent calls, the argument will be the same for all iterations of the state loop.</p><p>On the right, we now see a graph of the inputs to each benchmark. If you would rather view these results as a bar chart with information like <em>“variation X is Y times faster than variation Z”</em>, you can toggle that where it says Line on the bottom right.</p><p>The chained argument syntax shown above is pretty repetitive, though. We can replace it with an equivalent and more succinct version like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*9rUlFEEc7NZoDJQu" /></figure><p>This syntax says that beginning from 4, the lower bound of our range, let’s multiply each argument by 2, the range multiplier, to generate the next argument. We will continue this up through 32, the (inclusive) range upper bound.</p><p>This is all great, but how can we randomize inputs?</p><p>Let’s consider a new example more suitable for this use case.</p><p>Here’s one implementation for a benchmark comparing the square root computation of an argument with std::sqrt versus a custom made std::pow solution. For randomization, we’re simply using the STL rand() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*G_6Om86XoThheIWW" /></figure><p>This does indeed randomize the “argument” used for each of the registered functions, but as we mentioned earlier, these functions are only called once, exposing a few problems with this solution:</p><ul><li>We have no guarantee the random arguments will be the same for each registered function.</li><li>The same input is used for all iterations of the state loop.</li></ul><p>For all we know, Sqrt might be calculating the square root of some large prime number like 100,000,007 while Pow is simply calculating the square root of 4.</p><p>Below is an alternate solution using some new syntax designed for complex arguments (<a href="https://quick-bench.com/q/7Ta-njvUUV28sq3jlB24-QW-7c0">Quick Bench link</a>).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zE0xaKCV7_EIhziW" /></figure><p>With this new syntax…</p><ul><li>Box <strong><em>(A)</em></strong> contains code to generate 20 random numbers.</li><li>Box <strong><em>(B)</em></strong> has a <em>generator function</em>, which takes the registered benchmark function as an argument and then adds the 20 random numbers as arguments to that benchmark.</li><li>Boxes labeled <strong><em>(C)</em></strong> register each benchmark function and apply the generator function from box <strong><em>(B)</em></strong>.</li></ul><p>Based on our results above, we can conclude the std::sqrt solution is indeed faster than our custom std::pow solution.</p><h4><strong>What else?</strong></h4><p>We could go on for hours talking about other cool features, tips &amp; tricks for using Quick Bench. Stay tuned for more articles discussing some of these features in-depth! 🤓</p><p>In the meantime, you can check out the <a href="https://github.com/google/benchmark/blob/main/docs/user_guide.md">Google Benchmark documentation</a>, which Quick Bench is built upon. This has tips and tricks for custom statistics, multi-threaded benchmarking, CPU timers, asymptotic complexity, and so much more!</p><h4>What’s next?</h4><p>Did you find this article useful? Give it a <em>clap</em>!</p><p>Want to learn about other C++ features and tools that aren’t always taught in school?</p><p>Subscribe to have <strong><em>C++ Beyond the Syllabus</em></strong> delivered directly to your inbox every week :) 🤓</p><h4><strong>Sources &amp; More Info</strong></h4><ul><li><a href="https://www.youtube.com/watch?v=nXaxk27zwlk">CppCon 2015: Chandler Carruth “Tuning C++: Benchmarks, and CPUs, and Compilers! Oh My!”</a></li><li><a href="https://github.com/google/benchmark/tree/main">GitHub — google/benchmark</a></li><li><a href="https://gist.github.com/mathieumallet/22c8fa248b9d260b79f92090ffcd42f3">A cool list of Quick Bench links to interesting benchmarks (GitHub — mathieumallet)</a></li></ul><h4><strong>Collaborators</strong></h4><p>A special thanks to a few friends who took time to edit and review this post:</p><ul><li>Janay Bengal (<a href="https://www.linkedin.com/in/janay-bengal/">LinkedIn</a>)</li><li>Sam Maksimovich (<a href="https://www.linkedin.com/in/maksimovich-sam/">LinkedIn</a>)</li><li>Vanita Sharma (<a href="https://www.linkedin.com/in/vanita-s/">LinkedIn</a>, <a href="https://medium.com/@svanita">Medium</a>)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8788b1edcf79" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>