<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Shahar Mike&#39;s Web Spot</title>
    <link>https://shaharmike.com/index.xml</link>
    <description>Recent content on Shahar Mike&#39;s Web Spot</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Tue, 21 Nov 2023 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://shaharmike.com/index.xml" rel="self" type="application/rss+xml" />
    
    <item>
      <title>How We Optimized Dragonfly to Get 30x Throughput with BullMQ</title>
      <link>https://shaharmike.com/cpp/dragonfly-bullmq/</link>
      <pubDate>Tue, 21 Nov 2023 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/dragonfly-bullmq/</guid>
      <description>&lt;p&gt;It&amp;rsquo;s been a while since I added any new content here. Sorry about that!&lt;/p&gt;

&lt;p&gt;However, in case you&amp;rsquo;ve been following along, I just posted a new blog post on my (new) company&amp;rsquo;s
blog. I hope you&amp;rsquo;ll find it interesting!&lt;/p&gt;

&lt;p&gt;It&amp;rsquo;s title is
&lt;a href=&#34;https://www.dragonflydb.io/blog/running-bullmq-with-dragonfly-part-2-optimization&#34;&gt;How We Optimized Dragonfly to Get 30x Throughput with BullMQ&lt;/a&gt;,
and it tells the story of how we went about optimizing our service to get &amp;gt;30x throughput.&lt;/p&gt;

&lt;p&gt;While our service is indeed written in C++, the blog post does not go into C++ code unfortunately.
Instead, it describes the design decisions that we took in order to be more performant.&lt;/p&gt;

&lt;p&gt;I hope you like it!&lt;/p&gt;

&lt;p&gt;P.S: 30x is arbitrary, because I tested this on an 8 core machine. I could have added more cores to
write 100x, but such beefy machines aren&amp;rsquo;t likely to be used by our users I guess :)&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Longest C&#43;&#43; Variable Declaration</title>
      <link>https://shaharmike.com/cpp/longest-var-decl/</link>
      <pubDate>Sat, 26 Oct 2019 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/longest-var-decl/</guid>
      <description>&lt;p&gt;Here&amp;rsquo;s a challenge: what&amp;rsquo;s the longest variable declaration you can come up
with?&lt;/p&gt;

&lt;p&gt;That&amp;rsquo;s not useful in any way. Quite the opposite - the result is something you&amp;rsquo;d
&lt;em&gt;never&lt;/em&gt; want to see in your codebase. But it&amp;rsquo;s an interesting mental challenge,
or at least so I think.&lt;/p&gt;

&lt;p&gt;Ground rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Each keyword used gets you 1 point, repetitions allowed;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Must be global variable;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Must be valid C++17;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Must not declare new structs / classes / unions / functions / methods;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Variable must be initialized with empty braces, i.e &lt;code&gt;{}&lt;/code&gt;;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;No comments of any type are allowed, nor usage of preprocessor;&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Each &amp;lsquo;trick&amp;rsquo; may only be used once.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Take a moment to think about it! Maybe you&amp;rsquo;ll come up with ideas I haven&amp;rsquo;t.&lt;/p&gt;

&lt;h2 id=&#34;my-solution&#34;&gt;My Solution&lt;/h2&gt;

&lt;p&gt;Here&amp;rsquo;s roughly how my solution evolved:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;// Most basic declaration - 1 point.
int a{};

// Some types are longer than others - 4 points
unsigned long long int a{};

// Adding constness - 6 points.
constexpr const unsigned long long int a{};

// With pointers you can add an additional const - 7 points.
constexpr const unsigned long long int* const a{};

// static - 8 points
static constexpr const unsigned long long int* const a{};

// Can you use thread_local with static? Apparently! - 9 points.
static thread_local constexpr const unsigned long long int* const a{};

// How &#39;bout static thread_local inline? Seems to work... - 10 points.
static thread_local inline constexpr const unsigned long long int* const a{};

// Here&#39;s a feature rarely used - volatile! - 11 points.
// (volatile constexpr - such a weird statement...)
static thread_local inline constexpr volatile const unsigned long long int* const a{};
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;cheating-aka-stack-overflow&#34;&gt;Cheating, aka Stack Overflow&lt;/h2&gt;

&lt;p&gt;At this point, curious if I&amp;rsquo;m the only very bored C++ developer who came up with
this challenge I decided to Google it.&lt;/p&gt;

&lt;p&gt;I found
&lt;a href=&#34;https://stackoverflow.com/questions/23861535/most-keywords-possible-in-declaration&#34;&gt;this&lt;/a&gt;
Stack Overflow thread with some interesting ideas. Someone came up with an easy
way to get infinite points:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;int const* const* const* const* const* const* const* const* /* ... */ a{};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is the reason I added that last rule. It&amp;rsquo;s not well formulated I guess,
but you get the spirit.&lt;/p&gt;

&lt;p&gt;Additional idea is to use &lt;code&gt;decltype&lt;/code&gt; with &lt;code&gt;typeid&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;// Adding decltype with typeid buys us additional 4 points, with a total of 15 points.
static thread_local inline constexpr volatile const decltype(typeid(const volatile unsigned long long int).name())* const a{};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And the last idea I read was to geniusly use &lt;code&gt;alignas&lt;/code&gt; with &lt;code&gt;sizeof&lt;/code&gt;, although I
personally prefer to use &lt;code&gt;alignof&lt;/code&gt; instead:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;// Adding alignas with alignof / sizeof gets us to 25 points!
alignas(alignof(decltype(typeid(const volatile unsigned long long int)))) static thread_local inline constexpr volatile const decltype(typeid(const volatile unsigned long long int).name())* const a{};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is as far as I got. I&amp;rsquo;m &lt;em&gt;certain&lt;/em&gt; there are many more tricks I hadn&amp;rsquo;t
thought of, or unaware of. Do you have any? Let me know in a comment!&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Compiling Clang from Scratch</title>
      <link>https://shaharmike.com/cpp/build-clang/</link>
      <pubDate>Fri, 07 Dec 2018 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/build-clang/</guid>
      <description>&lt;p&gt;In this post I&amp;rsquo;ll show how easy it is to build clang from scratch on Linux, and
how to use it both directly and with CMake.&lt;/p&gt;

&lt;h2 id=&#34;but-why&#34;&gt;But Why?&lt;/h2&gt;

&lt;p&gt;Clang is changing rapidly, and new features are added frequently. On the other
hand, Linux distros have ancient versions shipped with them. That&amp;rsquo;s just
frustrating.&lt;/p&gt;

&lt;p&gt;Furthermore, building Clang is so easy that it really shouldn&amp;rsquo;t be a blocker
for you to use the newest and bestest. Cloning the repositories takes a few
minutes, then the actual build step will take between 7 minutes to 1 hour,
depending on your hardware.&lt;/p&gt;

&lt;h2 id=&#34;step-1-clone&#34;&gt;Step 1 - Clone&lt;/h2&gt;

&lt;p&gt;For this step you&amp;rsquo;ll need &lt;code&gt;git&lt;/code&gt; installed on your system. Simply paste the
following into a shell, while inside an empty directory you&amp;rsquo;d like to clone the
sources into (I used &lt;code&gt;~/clang&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;git clone -q  https://github.com/llvm-mirror/llvm llvm
git clone -q  https://github.com/llvm-mirror/clang llvm/tools/clang
git clone -q  https://github.com/llvm-mirror/clang-tools-extra llvm/tools/clang/tools/extra
git clone -q  https://github.com/llvm-mirror/compiler-rt llvm/projects/compiler-rt
git clone -q  https://github.com/llvm-mirror/libcxx llvm/projects/libcxx
git clone -q  https://github.com/llvm-mirror/libcxxabi llvm/projects/libcxxabi
git clone -q  https://github.com/llvm-mirror/lld llvm/tools/lld
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This will create an &lt;code&gt;llvm&lt;/code&gt; directory, inside which all the needed sources are
found.  Note that we&amp;rsquo;re building from the head of &lt;code&gt;master&lt;/code&gt; branch, where all
the goodies are. If you&amp;rsquo;re using this for something serious consider using one
of the release branches instead.&lt;/p&gt;

&lt;h2 id=&#34;step-2-run-cmake&#34;&gt;Step 2 - Run CMake&lt;/h2&gt;

&lt;p&gt;Now we need to invoke &lt;code&gt;cmake&lt;/code&gt; to generate build environment for us. It&amp;rsquo;s
recommended to use the &lt;a href=&#34;https://ninja-build.org/&#34;&gt;Ninja&lt;/a&gt; build system as it&amp;rsquo;s
much faster, but you can also use good old Unix make: just drop the &lt;code&gt;-GNinja&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You may also wish to build Clang in Release mode rather than the default Debug
mode (pass &lt;code&gt;-DCMAKE_BUILD_TYPE=Release&lt;/code&gt; to &lt;code&gt;cmake&lt;/code&gt;). The reasons are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Everything (sources + built objects, libs &amp;amp; executables) take 61gb with
Debug, and only 4.6gb in Release&lt;/li&gt;
&lt;li&gt;At least on my machine it takes more time (7m vs 9m) to build the Debug
version (maybe due to I/O?). YMMV.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Paste the following into a &lt;code&gt;bash&lt;/code&gt; shell to get going:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;mkdir build
cd build
cmake -GNinja ../llvm
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;step-3-build&#34;&gt;Step 3 - Build&lt;/h2&gt;

&lt;p&gt;This is fairly easy. Simply run &lt;code&gt;ninja&lt;/code&gt; (or &lt;code&gt;make&lt;/code&gt; if that&amp;rsquo;s how you configured
&lt;code&gt;cmake&lt;/code&gt;). Now go make a sandwich - your PC will be rather busy.&lt;/p&gt;

&lt;h2 id=&#34;step-4-profit&#34;&gt;Step 4 - Profit&lt;/h2&gt;

&lt;p&gt;You now have LLVM, Clang, libc++ and other goodies built and ready to use.&lt;/p&gt;

&lt;p&gt;Simple compilation:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ cat main.cpp
#include &amp;lt;iostream&amp;gt;
using namespace std;

int main() {
  cout &amp;lt;&amp;lt; &amp;quot;Hello, World!&amp;quot; &amp;lt;&amp;lt; endl;
}

$ ~/clang/build/bin/clang++ main.cpp
$ ./a.out
Hello, World!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Use libc++ which we built above:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ ~/clang/build/bin/clang++ main.cpp -nostdinc++ -I$HOME/clang/build/include/c++/v1 -L$HOME/clang/build/lib -Wl,-rpath,$HOME/clang/build/lib -L$HOME/clang/build/lib -lc++ -Wl,-rpath,$HOME/clang/build/lib
$ ./a.out
Hello, World!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Compile with C++20 (as of writing this - initializer inside ranged-based for is
not in any released clang version):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ cat main.cpp
#include &amp;lt;iostream&amp;gt;
using namespace std;

int main() {
  for (int i = 1; int j : {1, 2, 3, 4}) {
    cout &amp;lt;&amp;lt; &amp;quot;i: &amp;quot; &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;, j: &amp;quot; &amp;lt;&amp;lt; j &amp;lt;&amp;lt; endl;
  }
}

$ ~/clang/build/bin/clang++ main.cpp -std=c++2a
$ ./a.out
i: 1, j: 1
i: 1, j: 2
i: 1, j: 3
i: 1, j: 4
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Compile with
&lt;a href=&#34;https://clang.llvm.org/docs/AddressSanitizer.html&#34;&gt;AddressSanitizer&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ cat main.cpp
int main() {
  int* a = new int(123);
  delete a;
  delete a;
}

$ ~/clang/build/bin/clang++ -O0 -g -fno-omit-frame-pointer -fsanitize=address main.cpp
$ ./a.out
=================================================================
==239623==ERROR: AddressSanitizer: attempting double-free on 0x602000000010 in thread T0:
    #0 0x4f9d12  (/tmp/cpp_vmizcK/a.out+0x4f9d12)
    #1 0x4fc0f1  (/tmp/cpp_vmizcK/a.out+0x4fc0f1)
    #2 0x7feaefe3c2b0  (/lib/x86_64-linux-gnu/libc.so.6+0x202b0)
    #3 0x41d989  (/tmp/cpp_vmizcK/a.out+0x41d989)

0x602000000010 is located 0 bytes inside of 4-byte region [0x602000000010,0x602000000014)
freed by thread T0 here:
    #0 0x4f9d12  (/tmp/cpp_vmizcK/a.out+0x4f9d12)
    #1 0x4fc0d3  (/tmp/cpp_vmizcK/a.out+0x4fc0d3)
    #2 0x7feaefe3c2b0  (/lib/x86_64-linux-gnu/libc.so.6+0x202b0)

previously allocated by thread T0 here:
    #0 0x4f9092  (/tmp/cpp_vmizcK/a.out+0x4f9092)
    #1 0x4fc068  (/tmp/cpp_vmizcK/a.out+0x4fc068)
    #2 0x7feaefe3c2b0  (/lib/x86_64-linux-gnu/libc.so.6+0x202b0)

SUMMARY: AddressSanitizer: double-free (/tmp/cpp_vmizcK/a.out+0x4f9d12)
==239623==ABORTING
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Show proper symbols:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ env ASAN_SYMBOLIZER_PATH=$HOME/clang/build/bin/llvm-symbolizer ./a.out
=================================================================
==239574==ERROR: AddressSanitizer: attempting double-free on 0x602000000010 in thread T0:
    #0 0x4f9d12 in operator delete(void*) /home/shmike/clang/llvm/projects/compiler-rt/lib/asan/asan_new_delete.cc:167:3
    #1 0x4fc0f1 in main /tmp/cpp_vmizcK/main.cpp:4:3
    #2 0x7fb8a6c792b0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202b0)
    #3 0x41d989 in _start (/tmp/cpp_vmizcK/a.out+0x41d989)

0x602000000010 is located 0 bytes inside of 4-byte region [0x602000000010,0x602000000014)
freed by thread T0 here:
    #0 0x4f9d12 in operator delete(void*) /home/shmike/clang/llvm/projects/compiler-rt/lib/asan/asan_new_delete.cc:167:3
    #1 0x4fc0d3 in main /tmp/cpp_vmizcK/main.cpp:3:3
    #2 0x7fb8a6c792b0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202b0)

previously allocated by thread T0 here:
    #0 0x4f9092 in operator new(unsigned long) /home/shmike/clang/llvm/projects/compiler-rt/lib/asan/asan_new_delete.cc:106:3
    #1 0x4fc068 in main /tmp/cpp_vmizcK/main.cpp:2:12
    #2 0x7fb8a6c792b0 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x202b0)

SUMMARY: AddressSanitizer: double-free /home/shmike/clang/llvm/projects/compiler-rt/lib/asan/asan_new_delete.cc:167:3 in operator delete(void*)
==239574==ABORTING
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Compile with
&lt;a href=&#34;https://clang.llvm.org/docs/MemorySanitizer.html&#34;&gt;MemorySanitizer&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ cat main.cpp
int main() {
  int a;  // uninitialized
  return a;
}

$ ~/clang/build/bin/clang++ -O0 -g -fno-omit-frame-pointer -fsanitize=memory main.cpp
$ ./a.out
==243759==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x496dac  (/tmp/cpp_vmizcK/a.out+0x496dac)
    #1 0x7f6f9c9cc2b0  (/lib/x86_64-linux-gnu/libc.so.6+0x202b0)
    #2 0x41e379  (/tmp/cpp_vmizcK/a.out+0x41e379)

SUMMARY: MemorySanitizer: use-of-uninitialized-value (/tmp/cpp_vmizcK/a.out+0x496dac)
Exiting
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Compile for WebAssembly (wasm):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ ~/clang/build/bin/clang++ --target=wasm32 -Os main.cpp -c -o out.wasm
$ file out.wasm
out.wasm: WebAssembly (wasm) binary module version 0x1 (MVP)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(Note that it&amp;rsquo;s not trivial to use libc or STL natively, and thus it&amp;rsquo;s much
easier to use &lt;a href=&#34;http://kripken.github.io/emscripten-site/&#34;&gt;Emscripten&lt;/a&gt;).&lt;/p&gt;

&lt;h2 id=&#34;integrate-with-cmake&#34;&gt;Integrate with CMake&lt;/h2&gt;

&lt;p&gt;While using the compiler manually is nice, it&amp;rsquo;s only rarely a good idea. For
real projects we all use some sort of build system, and many of us use CMake.&lt;/p&gt;

&lt;p&gt;Integrating our freshly built Clang with CMake is fairly simple, assuming you
know how to paste :)&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;cmake -DCMAKE_CXX_COMPILER=$HOME/clang/build/bin/clang++ -DCMAKE_LINKER=$HOME/clang/build/bin/clang++
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And if you&amp;rsquo;d like to also use the locally built libc++:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;cmake -DCMAKE_CXX_FLAGS=&amp;quot;-nostdinc++ -I$HOME/clang/build/include/c++/v1&amp;quot; -DCMAKE_EXE_LINKER_FLAGS=&amp;quot;-L$HOME/clang/build/lib -lc++ -nostdinc++ -Wl,-rpath,$HOME/clang/build/lib&amp;quot; -DCMAKE_CXX_COMPILER=$HOME/clang/build/bin/clang++ -DCMAKE_LINKER=$HOME/clang/build/bin/clang++
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;rsquo;s it. I hope you liked this mini tutorial, and that you found it useful, or
at least interesting!&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Move Semantics</title>
      <link>https://shaharmike.com/cpp/move-semantics/</link>
      <pubDate>Wed, 19 Sep 2018 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/move-semantics/</guid>
      <description>&lt;p&gt;&lt;em&gt;Move Semantics&lt;/em&gt; are a C++11 feature which complements &lt;a href=&#34;https://shaharmike.com/cpp/rvo/&#34;&gt;C++98&amp;rsquo;s RVO&lt;/a&gt;; Think of them as &lt;strong&gt;user-defined RVO-like optimization&lt;/strong&gt;. While
originally designed to only allow optimizations, one can also utilize move
semantics to limit APIs. This is how &lt;code&gt;std::unique_ptr&lt;/code&gt; is able to be a
&lt;em&gt;move-only&lt;/em&gt; type, allowing it to enforce single ownership (more
about &lt;code&gt;std::unique_ptr&lt;/code&gt; &lt;a href=&#34;https://shaharmike.com/cpp/unique-ptr/&#34;&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;h2 id=&#34;motivation&#34;&gt;Motivation&lt;/h2&gt;

&lt;p&gt;As we saw previously, &lt;a href=&#34;https://shaharmike.com/cpp/rvo/&#34;&gt;RVO&lt;/a&gt; does not &lt;em&gt;always&lt;/em&gt; take place.
When it doesn&amp;rsquo;t, C++98 forced users to create expensive copies.&lt;/p&gt;

&lt;p&gt;As an example, let&amp;rsquo;s see what the following program does when compiled with
different flags:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;string&amp;gt;

// replace operator new and delete to log allocations
void* operator new(std::size_t n) throw(std::bad_alloc) {
  std::cout &amp;lt;&amp;lt; &amp;quot;[Allocating &amp;quot; &amp;lt;&amp;lt; n &amp;lt;&amp;lt; &amp;quot; bytes]\n&amp;quot;;
  return malloc(n);
}
void operator delete(void* p) throw() { free(p); }

std::string BuildLongString() {
  return &amp;quot;This string is so long it can&#39;t possibly be inline (SSO)&amp;quot;;
}

int main() {
  BuildLongString();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(1) C++98 with RVO support: Single copy - RVO FTW!&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++-libc++ -std=c++98 main.cpp &amp;amp;&amp;amp; ./a.out
[Allocating 64 bytes]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(2) C++98 without RVO support: 2 copies - makes sense. Sad but true.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++-libc++ -std=c++98 -fno-elide-constructors main.cpp &amp;amp;&amp;amp; ./a.out
[Allocating 64 bytes]
[Allocating 64 bytes]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(3) C++11 with RVO support: Single copy - no news.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++-libc++ -std=c++11 main.cpp &amp;amp;&amp;amp; ./a.out
[Allocating 64 bytes]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;(4) C++11 without RVO support: Single copy - that&amp;rsquo;s new!&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++-libc++ -std=c++11 -fno-elide-constructors main.cpp &amp;amp;&amp;amp; ./a.out
[Allocating 64 bytes]
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Using move-semantics we are able to avoid allocating data even when RVO is
disabled.&lt;/p&gt;

&lt;h2 id=&#34;more-practical-example&#34;&gt;More Practical Example&lt;/h2&gt;

&lt;p&gt;Let&amp;rsquo;s consider the following example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;std::string s = BuildLongString();  // Same BuildLongString() from above
// Do something with s.
s = BuildLongString();  // Copy assignment - no RVO ever!
// Do something else with s.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In C++98 the above code had to create a copy of the long string returned by
&lt;code&gt;BuildLongString()&lt;/code&gt;&lt;sup class=&#34;footnote-ref&#34; id=&#34;fnref:1&#34;&gt;&lt;a rel=&#34;footnote&#34; href=&#34;#fn:1&#34;&gt;1&lt;/a&gt;&lt;/sup&gt; because RVO is not allowed on assignment. That&amp;rsquo;s
unfortunate, because we can immediately see that the previous value of &lt;code&gt;s&lt;/code&gt; will
be lost as part of that assignment.&lt;/p&gt;

&lt;p&gt;C++11&amp;rsquo;s Move Semantics allow us to avoid this copy by &amp;lsquo;stealing&amp;rsquo; the pointer of
the temporary object returned by &lt;code&gt;BuildLongString()&lt;/code&gt; and directing that
temporary object to not own that pointer anymore (so that it won&amp;rsquo;t attempt to
&lt;code&gt;delete&lt;/code&gt; it).&lt;/p&gt;

&lt;p&gt;In other words, the compiler now provides us with a way to &lt;em&gt;know&lt;/em&gt; that an
object passed to us is temporary and will soon be destroyed. With this
knowledge we can write smarter and better performing code. These temporary,
soon-to-be-destroyed objects are annotated with &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; - a new C++ syntax.&lt;/p&gt;

&lt;h2 id=&#34;move-constructor-move-assignment&#34;&gt;Move Constructor / Move Assignment&lt;/h2&gt;

&lt;p&gt;Most commonly, move-semantics are used for creating a special type of
constructor called a &lt;em&gt;move constructor&lt;/em&gt;. Move constructors are similar to &lt;em&gt;copy
constructors&lt;/em&gt; both syntactically and logically. They can be implemented in
addition to, or instead of, a copy constructor. Similarly one can implement a
&lt;em&gt;move assignment&lt;/em&gt; - in addition to, or instead of a copy assignment (like in &lt;code&gt;a
= b;&lt;/code&gt;).&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;class MyClass {
 public:
  MyClass();                             // Constructor

  MyClass(const MyClass&amp;amp; o);             // Copy constructor
  MyClass(MyClass&amp;amp;&amp;amp; o);                  // Move constructor

  MyClass&amp;amp; operator=(const MyClass&amp;amp; o);  // Copy assignment
  MyClass&amp;amp; operator=(MyClass&amp;amp;&amp;amp; o);       // Move assignment
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As we saw above, &lt;code&gt;MyClass&amp;amp;&amp;amp;&lt;/code&gt; is the syntax for a special reference to &lt;code&gt;MyClass&lt;/code&gt;
that is an rvalue, aka &lt;em&gt;rvalue reference&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Move constructors / assignment operations will be invoked automatically by the
compiler only if the parameter passed to them (&lt;code&gt;o&lt;/code&gt; in the above example) are
rvalues. Otherwise the compiler will invoke the safe-but-slow copy constructor
/ assignment.&lt;/p&gt;

&lt;p&gt;Consider this: you&amp;rsquo;re tasked with implementing &lt;code&gt;std::string&lt;/code&gt;&amp;rsquo;s assignment
operator. Let&amp;rsquo;s assume &lt;code&gt;std::string&lt;/code&gt; has 3 members: &lt;code&gt;data_&lt;/code&gt;, &lt;code&gt;size_&lt;/code&gt; and
&lt;code&gt;capacity_&lt;/code&gt;. When implementing the assignment function you obviously want
&lt;code&gt;this&lt;/code&gt; to be like another &lt;code&gt;std::string o&lt;/code&gt; (that&amp;rsquo;s the meaning of an
assignment), but you also &lt;em&gt;know&lt;/em&gt; that &lt;code&gt;o&lt;/code&gt; will very soon need to be destroyed.
With this knowledge you can implement that assignment operation in a very
optimized fashion.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;std::string&amp;amp; operator=(std::string&amp;amp;&amp;amp; o) {  // `o` is a temporary
  // Steal &amp;amp; copy data
  data_ = o.data_;  // data_ is a char*
  size_ = o.size_;  // size_ is a size_t
  capacity_ = o.capacity_;  // capacity_ is a size_t

  // Make sure `o` can be destroyed safely
  o.data_ = nullptr;
  // We can also do o.size_ = o.capacity_ = 0;

  return *this;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;No memory allocation, no copying of buffer, O(1) operation. That&amp;rsquo;s much better
than copy assignment!&lt;/p&gt;

&lt;h2 id=&#34;interim-summary&#34;&gt;Interim Summary&lt;/h2&gt;

&lt;p&gt;This special syntax, &lt;code&gt;std::string&amp;amp;&amp;amp; o&lt;/code&gt;, is our entry point to using move
semantics. Furthermore, this assignment operation is not a &lt;em&gt;copy assignment&lt;/em&gt;
but rather a &lt;em&gt;move assignment&lt;/em&gt;. And &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; means that we have a special reference
in our hands - a reference to a &amp;ldquo;temporary object&amp;rdquo;.&lt;/p&gt;

&lt;h2 id=&#34;temporary-objects-intuition&#34;&gt;Temporary Objects - Intuition&lt;/h2&gt;

&lt;p&gt;What exactly is considered to be temporary?&lt;/p&gt;

&lt;p&gt;You might have seen the term rhs (right-hand side) or rvalue (right-hand value)
in some compiler errors in the past. For example, when attempting to compile
code such as this:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;int foo() { return 42; }

// ...
foo() = 5;  // Error: expression is not assignable
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It doesn&amp;rsquo;t make sense to assign &lt;em&gt;to&lt;/em&gt; the value returned from &lt;code&gt;foo()&lt;/code&gt;.  It might
have made sense if &lt;code&gt;foo()&lt;/code&gt; were to return a &lt;em&gt;reference&lt;/em&gt;, but that&amp;rsquo;s not the
case here. Since it doesn&amp;rsquo;t make sense for this variable to be assigned to, the
compiler forbids us to do so.&lt;/p&gt;

&lt;p&gt;This is the first rule of thumb in deciding whether an object is a temporary:
can it be used in the left-hand side of an assignment equation? That&amp;rsquo;s exactly
what we tried to do with &lt;code&gt;foo() = 5;&lt;/code&gt; above. If we can&amp;rsquo;t - the object is a
temporary.&lt;/p&gt;

&lt;p&gt;Unfortunately this rule doesn&amp;rsquo;t always work. The compiler &lt;em&gt;will&lt;/em&gt; allow us to
invoke assignment operations on a custom class (unless they used
ref-qualifiers, which are uncommon and beyond the scope of this post).&lt;/p&gt;

&lt;p&gt;Another rule of thumb, and one I like better, is to consider whether it is
possible to take the address of an object. For example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;int foo() { return 42; }

// ...
int i = foo();
int* p = &amp;amp;i;  // OK: `i` is an lvalue

p = &amp;amp;foo();  // Error: cannot take the address of an rvalue of type &#39;int&#39;.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that it&amp;rsquo;s fine to take the address of the &lt;em&gt;function&lt;/em&gt;. We can&amp;rsquo;t take the
address of the value &lt;em&gt;returned from&lt;/em&gt; a function. I.e:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;auto t = &amp;amp;foo;    // OK - taking the address of function foo
auto v = &amp;amp;foo();  // Error - can&#39;t take the address of the value returned from foo
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In a future post I plan to explain better what is the definition of rvalues,
but for now we will consider &lt;em&gt;an object which will be destroyed by the end of
the statement&lt;/em&gt; as rvalue. They are usually temporary objects, as in the above.
You can find a more accurate definition of what&amp;rsquo;s an rvalue in
&lt;a href=&#34;https://en.cppreference.com/w/cpp/language/value_category&#34;&gt;cppreference&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&#34;std-move&#34;&gt;&lt;code&gt;std::move()&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Any function (such as move constructor, move assignment, or just a global
function) which accepts rvalue references (&lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt;) can only be called with an
rvalue object. This is where the true power of move semantics comes into play.
The compiler &lt;em&gt;knows&lt;/em&gt; when it&amp;rsquo;s safe to pass an object as an rvalue reference.
If it&amp;rsquo;s not, we&amp;rsquo;ll get a compile error:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;void foo(std::string&amp;amp;&amp;amp; s) { /* ... */ }

// ...

foo(&amp;quot;hello&amp;quot;);  // Temporary objects are always rvalues.

// Return values are rvalues as well (except when the function returns a
// reference)
foo(BuildLongString());

std::string s;
foo(s);  // Compile error - `s` isn&#39;t an rvalue
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is good, and by design. However there are cases where we do want to
convert an object to an rvalue - where we know it won&amp;rsquo;t be used in the future.
What then? This is where &lt;code&gt;std::move()&lt;/code&gt; comes into play:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;// std::move() converts an object to rvalue.
foo(std::move(s));
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;rule-of-3-becomes-rule-of-5&#34;&gt;Rule of 3 becomes Rule of 5&lt;/h2&gt;

&lt;p&gt;One last thing before I wrap up: If you ever heard of the &lt;a href=&#34;http://en.cppreference.com/w/cpp/language/rule_of_three&#34;&gt;rule of
three&lt;/a&gt; - with move
semantics we now have a complementary rule - the rule of 5.&lt;/p&gt;

&lt;h2 id=&#34;that-s-it&#34;&gt;That&amp;rsquo;s it&lt;/h2&gt;

&lt;p&gt;In the next post I plan to look into what rvalue categories are and how they
differ from rvalue references. See you next time!&lt;/p&gt;
&lt;div class=&#34;footnotes&#34;&gt;

&lt;hr /&gt;

&lt;ol&gt;
&lt;li id=&#34;fn:1&#34;&gt;In C++98 days it was popular to implement &lt;code&gt;std::string&lt;/code&gt; as copy-on-write.  Technically speaking a copy of the string would be created, but it would not allocate the buffer again but would instead atomically increment a counter.  This was abandoned in recent years (and is also forbidden by the C++11 Standard) because for the most part it&amp;rsquo;s faster on multi-core computers to create copies rather than use atomics. I&amp;rsquo;ve written a bit on the subject &lt;a href=&#34;https://shaharmike.com/cpp/std-string/&#34;&gt;here&lt;/a&gt;
 &lt;a class=&#34;footnote-return&#34; href=&#34;#fnref:1&#34;&gt;&lt;sup&gt;[return]&lt;/sup&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description>
    </item>
    
    <item>
      <title>Dollhouse</title>
      <link>https://shaharmike.com/assorted/dollhouse/</link>
      <pubDate>Wed, 15 Aug 2018 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/assorted/dollhouse/</guid>
      <description>&lt;p&gt;As our oldest daughter was nearing her 4th birthday, my wife and I considered
to give her a dollhouse. Very soon after we decided to build her one instead of
purchasing.&lt;/p&gt;

&lt;h2 id=&#34;tools-used&#34;&gt;Tools Used&lt;/h2&gt;

&lt;p&gt;We have a wood workshop at work. It&amp;rsquo;s sort of an employee benefit, rather than
something that&amp;rsquo;s really work-related. Awesome, I know. It also has a great view
:)&lt;/p&gt;

&lt;p&gt;So most of the tools I used aren&amp;rsquo;t mine, and are mostly professional:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;I&amp;rsquo;m too scared to use a table saw, so instead I mostly used a &lt;strong&gt;circular
saw&lt;/strong&gt; with tracks.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Jointer&lt;/strong&gt; and &lt;strong&gt;Planer&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Router&lt;/strong&gt; for corners.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scroll saw&lt;/strong&gt; for cutting the windows and doors. I could have created a
frame and use the router for straighter lines, but I&amp;rsquo;m OK with the result.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Lots of &lt;strong&gt;clamps&lt;/strong&gt; for applying pressure while gluing.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&#34;from-design-to-product&#34;&gt;From Design to Product&lt;/h2&gt;

&lt;p&gt;Being the pedantic engineer I am, I started with a SketchUp model:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://shaharmike.com/img/dollhouse/sketch.png&#34; alt=&#34;Dollhouse Sketch&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Next, time to purchase lumber. I couldn&amp;rsquo;t get this huge piece through the
stairs, nor through the elevator, so I had to cut it outside:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://shaharmike.com/img/dollhouse/lumber.jpg&#34; alt=&#34;Raw lumber&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Next I cut it to length according to plan:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://shaharmike.com/img/dollhouse/pieces.jpg&#34; alt=&#34;Pieces&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Here I made my first mistake, see below.&lt;/p&gt;

&lt;p&gt;I worked with a router for round corners, cut the windows / door manually, and
then it was time to glue it all together:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://shaharmike.com/img/dollhouse/gluing.jpg&#34; alt=&#34;Gluing&#34; /&gt;&lt;/p&gt;

&lt;p&gt;Add the roof, some color and voila!:&lt;/p&gt;

&lt;p&gt;&lt;img src=&#34;https://shaharmike.com/img/dollhouse/final.jpg&#34; alt=&#34;Final&#34; /&gt;&lt;/p&gt;

&lt;p&gt;With some purchased furniture and dolls on Ali Express we&amp;rsquo;re done :)&lt;/p&gt;

&lt;h2 id=&#34;what-i-learned&#34;&gt;What I Learned&lt;/h2&gt;

&lt;p&gt;I made quite a few mistakes, most of which I contribute to this being my first
&amp;ldquo;serious&amp;rdquo; wood project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;I trusted the seller&amp;rsquo;s dimensions instead of verifying. Too late did I
realize that instead of the listed 20cm depth by 2.5cm width it was in fact
closer to 19cm by 2cm. My design was not affected by changed depth (20cm or
19cm), but having a different width (and discovering it too late) made a few
angles quirky and glued less strongly. Conclusion is simple: &lt;strong&gt;always
measure, never trust&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;I used the jointer and planer after cutting the wood to pieces. This caused a
lot of unnecessary work for no good reason.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;I thought I good skip using a planer, and only use a jointer instead. I got
trapezoid which I had fix with a planer.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Lastly, I made a few mistakes with the roof, which changed it significantly.
To test my gluing abilities I glued it before cutting the 45&amp;deg; angels at
the bottom. Huge mistake, as now I couldn&amp;rsquo;t use the circular saw&amp;rsquo;s tracks as
it simply did not fit in width anymore. The roof is not symmetric, but it
still looks OK I think&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hope you like it! My daughter sure does ;)&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Free Cloud VM &amp; HTTPS</title>
      <link>https://shaharmike.com/assorted/free-vm-https/</link>
      <pubDate>Sun, 15 Oct 2017 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/assorted/free-vm-https/</guid>
      <description>&lt;p&gt;If your knowledge of what it takes to have an SSL encrypted website (aka
&lt;em&gt;https&lt;/em&gt;) is as outdated as mine was a couple of weeks ago - you will find this
quick post useful.&lt;/p&gt;

&lt;p&gt;A colleague of mine recently mentioned the fact that you can get SSL certificate
for any website for free. I know that issuing certificates was every CA&amp;rsquo;s
business model for ages, and so I was very surprised to learn that. Well, I was
in for a pleasant surprise.&lt;/p&gt;

&lt;p&gt;So I looked around (well, first result of &amp;lsquo;free https&amp;rsquo; on Google) and it turns
out a non-profit named &lt;a href=&#34;https://letsencrypt.org&#34;&gt;&lt;em&gt;letsencrypt&lt;/em&gt;&lt;/a&gt; gives free
certificates. &lt;strong&gt;2017 baby&lt;/strong&gt;. So I immediately went to the instructions, which to
my disappointment said that if one is using a shared-hosting he/she can either
manually create and renew certificates periodically (unless of course the
shared-hosting company supports &lt;em&gt;letsencrypt&lt;/em&gt;, but why would they if they make
money selling the same product?).&lt;/p&gt;

&lt;p&gt;Disappointed, I searched for an alternative solution. Luckily, Google recently
announced that they started a Cloud program called &amp;lsquo;Always Free&amp;rsquo; which gives
an &lt;code&gt;f1-micro&lt;/code&gt; instance for free without expiration (more details
&lt;a href=&#34;https://cloud.google.com/free/docs/frequently-asked-questions#always-free&#34;&gt;here&lt;/a&gt;).
A free always-on, cloud-based Linux machine? That&amp;rsquo;s even better than https for
my blog!&lt;/p&gt;

&lt;p&gt;If you&amp;rsquo;re still here you must love freebies, like myself; keep on reading. I
already have a dot-com domain. But my friend also mentioned
&lt;a href=&#34;http://dot.tk&#34;&gt;dot.tk&lt;/a&gt;, which is a free .tk domain anyone can register. So if
you&amp;rsquo;re starting fresh you could get free domain, hosting and https. I love
freebies!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Return Value Optimization</title>
      <link>https://shaharmike.com/cpp/rvo/</link>
      <pubDate>Fri, 18 Aug 2017 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/rvo/</guid>
      <description>&lt;p&gt;&lt;em&gt;Return Value Optimization&lt;/em&gt; (RVO), &lt;em&gt;Named RVO&lt;/em&gt; (NRVO) and &lt;em&gt;Copy-Elision&lt;/em&gt; are in
C++ since C++98. In this post I will explain what these concepts mean and how
they help improve runtime performance.&lt;/p&gt;

&lt;p&gt;I will use our old friend &lt;code&gt;Snitch&lt;/code&gt; - a class dedicated to printing at key
events:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;struct Snitch {   // Note: All methods have side effects
  Snitch() { cout &amp;lt;&amp;lt; &amp;quot;c&#39;tor&amp;quot; &amp;lt;&amp;lt; endl; }
  ~Snitch() { cout &amp;lt;&amp;lt; &amp;quot;d&#39;tor&amp;quot; &amp;lt;&amp;lt; endl; }

  Snitch(const Snitch&amp;amp;) { cout &amp;lt;&amp;lt; &amp;quot;copy c&#39;tor&amp;quot; &amp;lt;&amp;lt; endl; }
  Snitch(Snitch&amp;amp;&amp;amp;) { cout &amp;lt;&amp;lt; &amp;quot;move c&#39;tor&amp;quot; &amp;lt;&amp;lt; endl; }

  Snitch&amp;amp; operator=(const Snitch&amp;amp;) {
    cout &amp;lt;&amp;lt; &amp;quot;copy assignment&amp;quot; &amp;lt;&amp;lt; endl;
    return *this;
  }

  Snitch&amp;amp; operator=(Snitch&amp;amp;&amp;amp;) {
    cout &amp;lt;&amp;lt; &amp;quot;move assignment&amp;quot; &amp;lt;&amp;lt; endl;
    return *this;
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let&amp;rsquo;s get goin&amp;rsquo;.&lt;/p&gt;

&lt;h2 id=&#34;return-value-optimization&#34;&gt;Return Value Optimization&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;RVO&lt;/em&gt; basically means the compiler is allowed to avoid creating temporary
objects for return values, &lt;strong&gt;even if they have side effects&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s a simple example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;Snitch ExampleRVO() {
  return Snitch();
}

int main() {
  Snitch snitch = ExampleRVO();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output (note that &lt;code&gt;-fno-elide-constructors&lt;/code&gt; disables RVO in clang):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++ -std=c++11 main.cpp &amp;amp;&amp;amp; ./a.out
c&#39;tor
d&#39;tor

$ clang++ -std=c++11 -fno-elide-constructors main.cpp &amp;amp;&amp;amp; ./a.out
c&#39;tor
move c&#39;tor
d&#39;tor
move c&#39;tor
d&#39;tor
d&#39;tor
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the first run (without &lt;code&gt;-fno-elide-constructors&lt;/code&gt;) the compiler refrained from
calling user code despite it having a clear side effect (being printing to
console). This is also the &lt;strong&gt;default&lt;/strong&gt; behavior, meaning practically all C++
programs utilize RVO.&lt;/p&gt;

&lt;p&gt;Without RVO the compiler creates 3 &lt;code&gt;Snitch&lt;/code&gt; objects instead of 1:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A temporary object inside &lt;code&gt;ExampleRVO()&lt;/code&gt; (when printing &lt;code&gt;c&#39;tor&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;A temporary object for the returned object inside &lt;code&gt;main()&lt;/code&gt; (when printing the
first &lt;code&gt;move c&#39;tor&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;The named object &lt;code&gt;snitch&lt;/code&gt; (when printing the second &lt;code&gt;move c&#39;tor&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&#34;performance&#34;&gt;Performance&lt;/h2&gt;

&lt;p&gt;The neat thing about RVO is that it makes returning objects &lt;strong&gt;free&lt;/strong&gt;. It works
via allocating memory for the to-be-returned object in the caller&amp;rsquo;s stack frame.
The returning function then uses that memory as if it was in its own frame
without the programmer knowing / caring.&lt;/p&gt;

&lt;p&gt;In C++98 days this was significant:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;vector&amp;gt;
using namespace std;

vector&amp;lt;int&amp;gt; ReturnVector() {
  return vector&amp;lt;int&amp;gt;(1, 1);
}

int main() {
  for (int i = 0; i &amp;lt; 1000000000; ++i) {
    ReturnVector();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++ -fno-elide-constructors -std=c++98 -stdlib=libc++ -O3 main.cpp &amp;amp;&amp;amp; time ./a.out
real	0m37.235s
user	0m37.168s
sys	0m0.024s

$ clang++ -std=c++98 -stdlib=libc++ -O3 main.cpp &amp;amp;&amp;amp; time ./a.out
real	0m17.681s
user	0m17.668s
sys	0m0.000s
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;217% difference on my machine by simply avoiding the copy of the &lt;code&gt;vector&lt;/code&gt;. In
C++11 (or newer) environments it is even marginally faster to disable RVO:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++ -fno-elide-constructors -std=c++11 -stdlib=libc++ -O3 main.cpp &amp;amp;&amp;amp; time ./a.out
real	0m18.195s
user	0m18.188s
sys	0m0.000s

$ clang++ -std=c++11 -stdlib=libc++ -O3 main.cpp &amp;amp;&amp;amp; time ./a.out
real	0m18.356s
user	0m18.340s
sys	0m0.000s
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is due to Move Semantics, which is the subject of the next post.&lt;/p&gt;

&lt;p&gt;In trying to come up with an example where RVO is faster on modern C++ using STL
containers I hit a wall, mostly because of move-semantics but also because on
x86_84 RVO is in the ABI so disabling it is harder. &lt;strong&gt;Please post such examples
if you have any!&lt;/strong&gt;&lt;/p&gt;

&lt;h2 id=&#34;named-return-value-optimization-nrvo&#34;&gt;Named Return Value Optimization (NRVO)&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Named&lt;/em&gt; RVO is when an object with a name is returned but is nevertheless not
copied. A simple example is:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;Snitch ExampleNRVO() {
  Snitch snitch;
  return snitch;
}

int main() {
  ExampleNRVO();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which has a similar output to &lt;code&gt;ExampleRVO()&lt;/code&gt; above:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++ -std=c++11 main.cpp &amp;amp;&amp;amp; ./a.out
c&#39;tor
d&#39;tor
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;While RVO is almost always going to happen, NRVO is more restricted, as we will
see below. I personally don&amp;rsquo;t think NRVO deserves its own acronym.&lt;/p&gt;

&lt;h2 id=&#34;copy-elision&#34;&gt;Copy Elision&lt;/h2&gt;

&lt;p&gt;RVO is part of a larger group of optimizations called &lt;em&gt;copy-elision&lt;/em&gt;. Essentials
are the same, except copy-elision is not required to happen as part of return
statements, for example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;void foo(Snitch s) {
}

int main() {
  foo(Snitch());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;c&#39;tor
d&#39;tor
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In my experience, RVO is more frequent (and thus useful) than other copy-elision
practices, but your mileage may vary.&lt;/p&gt;

&lt;h2 id=&#34;when-rvo-doesn-t-can-t-happen&#34;&gt;When RVO doesn&amp;rsquo;t / can&amp;rsquo;t happen&lt;/h2&gt;

&lt;p&gt;RVO is an optimization the compiler is &lt;em&gt;allowed&lt;/em&gt; to apply (starting C++17 it is
in fact required to in certain cases). However, even in C++17 it is not always
guaranteed. Let&amp;rsquo;s look at a few examples.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The following examples are cases where, on my environment, RVO doesn&amp;rsquo;t happen.
Some of them may change with other compiler / versions.&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&#34;deciding-on-instance-at-runtime&#34;&gt;Deciding on Instance at Runtime&lt;/h3&gt;

&lt;p&gt;When the compiler can&amp;rsquo;t know from within the function which instance will be
returned it must disable RVO:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;Snitch CreateSnitch(bool runtime_condition) {
  Snitch a, b;
  if (runtime_condition) {
    return a;
  } else {
    return b;
  }
}

int main() {
  Snitch snitch = CreateSnitch(true);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++ -std=c++11 -stdlib=libc++ main.cpp &amp;amp;&amp;amp; ./a.out
c&#39;tor
c&#39;tor
move c&#39;tor
d&#39;tor
d&#39;tor
d&#39;tor
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;returning-a-parameter-global&#34;&gt;Returning a Parameter / Global&lt;/h3&gt;

&lt;p&gt;When returning an object that is not created in the scope of the function there
is no way to do RVO:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;Snitch global_snitch;

Snitch ReturnParameter(Snitch snitch) {
  return snitch;
}

Snitch ReturnGlobal() {
  return global_snitch;
}

int main() {
  Snitch snitch = ReturnParameter(global_snitch);
  Snitch snitch2 = ReturnGlobal();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++ -std=c++11 -stdlib=libc++ main.cpp &amp;amp;&amp;amp; ./a.out
c&#39;tor
copy c&#39;tor
move c&#39;tor
d&#39;tor
copy c&#39;tor
d&#39;tor
d&#39;tor
d&#39;tor
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;returning-by-std-move&#34;&gt;Returning by &lt;code&gt;std::move()&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;Returning by calling &lt;code&gt;std::move()&lt;/code&gt; on the return value is an anti-pattern. It is
wrong most of the times. It will indeed attempt to force move-constructor, but
in doing so it will disable RVO. It is also redundant, as move will happen if it
can even without explicitly calling &lt;code&gt;std::move()&lt;/code&gt; (see &lt;a href=&#34;https://stackoverflow.com/questions/14856344/when-should-stdmove-be-used-on-a-function-return-value&#34;&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;Snitch CreateSnitch() {
  Snitch snitch;
  return std::move(snitch);
}

int main() {
  Snitch snitch = CreateSnitch();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++ -std=c++11 -stdlib=libc++ main.cpp &amp;amp;&amp;amp; ./a.out
c&#39;tor
move c&#39;tor
d&#39;tor
d&#39;tor
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;assignment&#34;&gt;Assignment&lt;/h3&gt;

&lt;p&gt;RVO can only happen when an object is &lt;strong&gt;created&lt;/strong&gt; from a returned value. Using
&lt;code&gt;operator=&lt;/code&gt; on an existing object rather than copy/move &lt;em&gt;constructor&lt;/em&gt; might be
mistakenly thought of as RVO, but it isn&amp;rsquo;t:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;Snitch CreateSnitch() {
  return Snitch();
}

int main() {
  Snitch s = CreateSnitch();
  s = CreateSnitch();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++ -std=c++11 -stdlib=libc++ main.cpp &amp;amp;&amp;amp; ./a.out
c&#39;tor
c&#39;tor
move assignment
d&#39;tor
d&#39;tor
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;returning-member&#34;&gt;Returning Member&lt;/h3&gt;

&lt;p&gt;In some cases even an unnamed variable can&amp;rsquo;t RVO:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;struct Wrapper {
  Snitch snitch;
};

Snitch foo() {
  return Wrapper().snitch;
}

int main() {
  Snitch s = foo();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++ -std=c++11 -stdlib=libc++ main.cpp &amp;amp;&amp;amp; ./a.out
c&#39;tor
move c&#39;tor
d&#39;tor
d&#39;tor
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;While we can&amp;rsquo;t count on RVO to always take place, it will in most cases. For
those cases where it doesn&amp;rsquo;t we always have Move Semantics, which is the topic
of the &lt;a href=&#34;https://shaharmike.com/cpp/move-semantics/&#34;&gt;next post&lt;/a&gt;. As always, optimize for
readability rather than performance when writing code, unless you have a
quantifiable reason.&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Using libclang to Parse C&#43;&#43; (aka libclang 101)</title>
      <link>https://shaharmike.com/cpp/libclang/</link>
      <pubDate>Tue, 03 Jan 2017 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/libclang/</guid>
      <description>&lt;p&gt;In this post I&amp;rsquo;ll provide a quick tutorial for using libclang. I started playing
around with libclang while implementing &lt;a href=&#34;https://github.com/chakaz/reflang&#34;&gt;Reflang&lt;/a&gt;
&amp;ndash; an open source reflection framework for C++. Then I came to appreciate the
amazing work done by its developers.&lt;/p&gt;

&lt;p&gt;Please note that we will start with a program and will gradually add code.
Scroll to the end of the post to view the complete solution.&lt;/p&gt;

&lt;h2 id=&#34;libclang&#34;&gt;libclang?&lt;/h2&gt;

&lt;p&gt;Clang, if you haven&amp;rsquo;t heard of yet, is a wonderful C++ (and other C language
family) compiler. Well, not exactly a compiler, but a frontend to the LLVM
compiler.&lt;/p&gt;

&lt;p&gt;You see, compilers have a very tough problem to solve, and so most of them split
it into 2 easier problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Translating a programming language (C++ in our case) to some intermediate code
&amp;ndash; this is called the frontend, and is exactly what Clang does.&lt;/li&gt;
&lt;li&gt;Translate the above intermediate code to machine code &amp;ndash; this is called the
back-end. Clang uses LLVM for that.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The neat thing about Clang is that it is designed to be also used as a library.
There are many types of applications that must truly understand code &amp;ndash; IDEs,
documentation-generators, static-analysis tools, etc. Instead of each of them
having to implement C++ parsing (which is an extremely difficult task!),
libclang can be used to correctly handle all language features and edge-cases.&lt;/p&gt;

&lt;h2 id=&#34;libclang-1&#34;&gt;libclang!&lt;/h2&gt;

&lt;p&gt;And it&amp;rsquo;s so darn easy. Really. Those Clang folks really did an awesome work. In
the rest of this post we will use its C-API to explore the following code:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;// header.hpp

class MyClass
{
public:
  int field;
  virtual void method() const = 0;

  static const int static_field;
  static int static_method();
};
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;basic-example&#34;&gt;Basic example&lt;/h2&gt;

&lt;p&gt;Let&amp;rsquo;s look at the simplest of examples. The following program parses the above
file and immediately exists:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;clang-c/Index.h&amp;gt;  // This is libclang.
using namespace std;

int main()
{
  CXIndex index = clang_createIndex(0, 0);
  CXTranslationUnit unit = clang_parseTranslationUnit(
    index,
    &amp;quot;header.hpp&amp;quot;, nullptr, 0,
    nullptr, 0,
    CXTranslationUnit_None);
  if (unit == nullptr)
  {
    cerr &amp;lt;&amp;lt; &amp;quot;Unable to parse translation unit. Quitting.&amp;quot; &amp;lt;&amp;lt; endl;
    exit(-1);
  }

  clang_disposeTranslationUnit(unit);
  clang_disposeIndex(index);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There are many &lt;code&gt;0&lt;/code&gt;s and &lt;code&gt;nullptr&lt;/code&gt;s - these allow us to do some more advanced
stuff (like pass argv &amp;amp; argc, use in-memory files, etc). Let&amp;rsquo;s not get into
these.&lt;/p&gt;

&lt;p&gt;So what do we have after &lt;code&gt;clang_parseTranslationUnit()&lt;/code&gt; has finished
successfully? We have a parsed Abstract Syntax Tree (AST) which we can traverse
and inspect. Which is exactly what we&amp;rsquo;ll do.&lt;/p&gt;

&lt;h2 id=&#34;cursors&#34;&gt;Cursors&lt;/h2&gt;

&lt;p&gt;Pointers to the AST are called Cursors in libclang lingo. A Cursor can have a
parent and children. It can also have related cursors (like a default value for
a parameter, an explicit value to an enum entry, etc).&lt;/p&gt;

&lt;p&gt;The &amp;lsquo;entry point&amp;rsquo; cursor we will use is the cursor representing the Translation
Unit (TU), which is a C++ term meaning a single file including all &lt;code&gt;#include&lt;/code&gt;d
code. To get the TU&amp;rsquo;s cursor we will use the very descriptive
&lt;code&gt;clang_getTranslationUnitCursor()&lt;/code&gt;. Now that we have a cursor we can investigate
it or iterate using it.&lt;/p&gt;

&lt;h2 id=&#34;visit-children&#34;&gt;Visit children&lt;/h2&gt;

&lt;p&gt;Any cursor has a kind, which represents the essence of the cursor. Kind can be
one of many, many options, as can be seen &lt;a href=&#34;http://clang.llvm.org/doxygen/Index_8h_source.html#l01562&#34;&gt;here&lt;/a&gt;.
A few examples are:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;  /** \brief A C or C++ struct. */
  CXCursor_StructDecl                    = 2,
  /** \brief A C or C++ union. */
  CXCursor_UnionDecl                     = 3,
  /** \brief A C++ class. */
  CXCursor_ClassDecl                     = 4,
  /** \brief An enumeration. */
  CXCursor_EnumDecl                      = 5,
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We can get the kind from a cursor using &lt;code&gt;clang_getCursorKind()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For now lets visit all children of the TU:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;  CXCursor cursor = clang_getTranslationUnitCursor(unit);
  clang_visitChildren(
    cursor,
    [](CXCursor c, CXCursor parent, CXClientData client_data)
    {
      cout &amp;lt;&amp;lt; &amp;quot;Cursor kind: &amp;quot; &amp;lt;&amp;lt; clang_getCursorKind(c) &amp;lt;&amp;lt; endl;
      return CXChildVisit_Recurse;
    },
    nullptr);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The second-parameter lambda is a function called for every cursor visited.
Inside we always return &lt;code&gt;CXChildVisit_Recurse&lt;/code&gt; (although other options exist),
because we want to explore everything in our file.&lt;/p&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;Cursor kind: 4
Cursor kind: 39
Cursor kind: 6
Cursor kind: 21
Cursor kind: 9
Cursor kind: 21
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;rsquo;s a bit cryptic, and requires us to skip back and forth to &lt;code&gt;Index.h&lt;/code&gt;.
Fortunately, there&amp;rsquo;s a built-in function to convert cursor kind to a string, but
first we need to discuss libclang&amp;rsquo;s strings.&lt;/p&gt;

&lt;h2 id=&#34;cxstring&#34;&gt;CXString&lt;/h2&gt;

&lt;p&gt;CXString is a type representing a pointer to the AST. To retrieve an actually
useful string (&lt;code&gt;const char *&lt;/code&gt; for example), one must call &lt;code&gt;clang_getCString()&lt;/code&gt;
which internally increments a ref-count, and then &lt;code&gt;clang_disposeString()&lt;/code&gt; when
done.&lt;/p&gt;

&lt;p&gt;Since we&amp;rsquo;re going to do this a lot, let&amp;rsquo;s create a helper function:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream&amp;amp; stream, const CXString&amp;amp; str)
{
  stream &amp;lt;&amp;lt; clang_getCString(str);
  clang_disposeString(str);
  return stream;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;print-meaningful-output&#34;&gt;Print meaningful output&lt;/h2&gt;

&lt;p&gt;Now that we can extract strings, let&amp;rsquo;s modify our lambda to print something that
is actually useful:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;  CXCursor cursor = clang_getTranslationUnitCursor(unit);
  clang_visitChildren(
    cursor,
    [](CXCursor c, CXCursor parent, CXClientData client_data)
    {
      cout &amp;lt;&amp;lt; &amp;quot;Cursor &#39;&amp;quot; &amp;lt;&amp;lt; clang_getCursorSpelling(c) &amp;lt;&amp;lt; &amp;quot;&#39; of kind &#39;&amp;quot;
        &amp;lt;&amp;lt; clang_getCursorKindSpelling(clang_getCursorKind(c)) &amp;lt;&amp;lt; &amp;quot;&#39;\n&amp;quot;;
      return CXChildVisit_Recurse;
    },
    nullptr);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;Cursor &#39;MyClass&#39; of kind &#39;ClassDecl&#39;
Cursor &#39;&#39; of kind &#39;CXXAccessSpecifier&#39;
Cursor &#39;field&#39; of kind &#39;FieldDecl&#39;
Cursor &#39;method&#39; of kind &#39;CXXMethod&#39;
Cursor &#39;static_field&#39; of kind &#39;VarDecl&#39;
Cursor &#39;static_method&#39; of kind &#39;CXXMethod&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, that&amp;rsquo;s friggin&amp;rsquo; neat.&lt;/p&gt;

&lt;h2 id=&#34;a-more-complicated-example&#34;&gt;A more complicated example&lt;/h2&gt;

&lt;p&gt;I was very careful not to &lt;code&gt;#include&lt;/code&gt; &lt;em&gt;any&lt;/em&gt; header in &lt;code&gt;header.hpp&lt;/code&gt;. Why? Well, by
merely adding &lt;code&gt;#include &amp;lt;string&amp;gt;&lt;/code&gt; to &lt;code&gt;header.hpp&lt;/code&gt; the output size is 1.51MB.
Ever got pissed at the compiler for taking so long? That&amp;rsquo;s why. It&amp;rsquo;s very
educating to read such a file, but for everyone&amp;rsquo;s sake I won&amp;rsquo;t post it here.&lt;/p&gt;

&lt;p&gt;Instead, let&amp;rsquo;s parse the following file:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;enum class Cpp11Enum
{
  RED = 10,
  BLUE = 20
};

struct Wowza
{
  virtual ~Wowza() = default;
  virtual void foo(int i = 0) = 0;
};

struct Badabang : Wowza
{
  void foo(int) override;

  bool operator==(const Badabang&amp;amp; o) const;
};

template &amp;lt;typename T&amp;gt;
void bar(T&amp;amp;&amp;amp; t);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Same program&amp;rsquo;s output for this file:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;Cursor &#39;Cpp11Enum&#39; of kind &#39;EnumDecl&#39;
Cursor &#39;RED&#39; of kind &#39;EnumConstantDecl&#39;
Cursor &#39;&#39; of kind &#39;IntegerLiteral&#39;
Cursor &#39;BLUE&#39; of kind &#39;EnumConstantDecl&#39;
Cursor &#39;&#39; of kind &#39;IntegerLiteral&#39;
Cursor &#39;Wowza&#39; of kind &#39;StructDecl&#39;
Cursor &#39;~Wowza&#39; of kind &#39;CXXDestructor&#39;
Cursor &#39;foo&#39; of kind &#39;CXXMethod&#39;
Cursor &#39;i&#39; of kind &#39;ParmDecl&#39;
Cursor &#39;&#39; of kind &#39;IntegerLiteral&#39;
Cursor &#39;Badabang&#39; of kind &#39;StructDecl&#39;
Cursor &#39;struct Wowza&#39; of kind &#39;C++ base class specifier&#39;
Cursor &#39;struct Wowza&#39; of kind &#39;TypeRef&#39;
Cursor &#39;foo&#39; of kind &#39;CXXMethod&#39;
Cursor &#39;&#39; of kind &#39;attribute(override)&#39;
Cursor &#39;&#39; of kind &#39;ParmDecl&#39;
Cursor &#39;operator==&#39; of kind &#39;CXXMethod&#39;
Cursor &#39;o&#39; of kind &#39;ParmDecl&#39;
Cursor &#39;struct Badabang&#39; of kind &#39;TypeRef&#39;
Cursor &#39;bar&#39; of kind &#39;FunctionTemplate&#39;
Cursor &#39;T&#39; of kind &#39;TemplateTypeParameter&#39;
Cursor &#39;t&#39; of kind &#39;ParmDecl&#39;
Cursor &#39;T&#39; of kind &#39;TypeRef&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;libclang is awesome:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It allows checking whether code has been expanded from a macro, and to jump
there;&lt;/li&gt;
&lt;li&gt;It allows checking the location (file+line+column) for each cursor;&lt;/li&gt;
&lt;li&gt;It allows getting function&amp;rsquo;s parameter names, types and return type;&lt;/li&gt;
&lt;li&gt;It understands templates, autos, lambdas, and, well, everything in C++.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope this short post made you curious, and that you&amp;rsquo;ll also try exploring what
this amazing API provides. Please do write a comment below if you have anything
you want to add or ask!&lt;/p&gt;

&lt;h2 id=&#34;complete-code&#34;&gt;Complete Code&lt;/h2&gt;

&lt;p&gt;For your convenience, here&amp;rsquo;s the complete code we implemented today:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;clang-c/Index.h&amp;gt;
using namespace std;

ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream&amp;amp; stream, const CXString&amp;amp; str)
{
  stream &amp;lt;&amp;lt; clang_getCString(str);
  clang_disposeString(str);
  return stream;
}

int main()
{
  CXIndex index = clang_createIndex(0, 0);
  CXTranslationUnit unit = clang_parseTranslationUnit(
    index,
    &amp;quot;header.hpp&amp;quot;, nullptr, 0,
    nullptr, 0,
    CXTranslationUnit_None);
  if (unit == nullptr)
  {
    cerr &amp;lt;&amp;lt; &amp;quot;Unable to parse translation unit. Quitting.&amp;quot; &amp;lt;&amp;lt; endl;
    exit(-1);
  }

  CXCursor cursor = clang_getTranslationUnitCursor(unit);
  clang_visitChildren(
    cursor,
    [](CXCursor c, CXCursor parent, CXClientData client_data)
    {
      cout &amp;lt;&amp;lt; &amp;quot;Cursor &#39;&amp;quot; &amp;lt;&amp;lt; clang_getCursorSpelling(c) &amp;lt;&amp;lt; &amp;quot;&#39; of kind &#39;&amp;quot;
        &amp;lt;&amp;lt; clang_getCursorKindSpelling(clang_getCursorKind(c)) &amp;lt;&amp;lt; &amp;quot;&#39;\n&amp;quot;;
      return CXChildVisit_Recurse;
    },
    nullptr);

  clang_disposeTranslationUnit(unit);
  clang_disposeIndex(index);
}
&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    
    <item>
      <title>Exploring std::shared_ptr</title>
      <link>https://shaharmike.com/cpp/shared-ptr/</link>
      <pubDate>Sun, 13 Nov 2016 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/shared-ptr/</guid>
      <description>&lt;p&gt;Today we&amp;rsquo;ll talk about C++&amp;rsquo;s built-in smart pointer &lt;code&gt;std::shared_ptr&lt;/code&gt;. If you
have not yet read my previous post about &lt;a href=&#34;https://shaharmike.com/cpp/unique-ptr/&#34;&gt;&lt;code&gt;std::unique_ptr&lt;/code&gt;&lt;/a&gt;
I would highly recommend doing so before continuing.&lt;/p&gt;

&lt;h2 id=&#34;std-shared-ptr&#34;&gt;&lt;code&gt;std::shared_ptr&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;shared_ptr&lt;/code&gt; is another C++11 managed pointer. Like &lt;code&gt;unique_ptr&lt;/code&gt;, it also saves
you the need to call &lt;code&gt;new&lt;/code&gt; and &lt;code&gt;delete&lt;/code&gt; (and to generally worry about forgetting
to release etc).&lt;/p&gt;

&lt;p&gt;Unlike &lt;code&gt;unique_ptr&lt;/code&gt;, &lt;code&gt;shared_ptr&lt;/code&gt; can be shared. This means that multiple
instances of &lt;code&gt;shared_ptr&amp;lt;T&amp;gt;&lt;/code&gt; pointing to the same instance of &lt;code&gt;T&lt;/code&gt; can co-exist.
This is achieved via reference counting in a control block that&amp;rsquo;s shared by all
&lt;code&gt;shared_ptr&lt;/code&gt;s pointing to the same object. When the last &lt;code&gt;shared_ptr&lt;/code&gt; pointing
to an instance of &lt;code&gt;T&lt;/code&gt; is released, &lt;code&gt;T&lt;/code&gt; is released as well.&lt;/p&gt;

&lt;p&gt;Releasing a &lt;code&gt;shared_ptr&lt;/code&gt; can be done in the following ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Most commonly: via going out of scope, meaning calling the destructor
automatically;&lt;/li&gt;
&lt;li&gt;Through assignment of another &lt;code&gt;shared_ptr&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;Through calling &lt;code&gt;reset()&lt;/code&gt; (more on this later).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let&amp;rsquo;s look at an example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;memory&amp;gt;

struct Snitch {
public:
  Snitch() { std::cout &amp;lt;&amp;lt; &amp;quot;c&#39;tor&amp;quot; &amp;lt;&amp;lt; std::endl; }
  ~Snitch() { std::cout &amp;lt;&amp;lt; &amp;quot;d&#39;tor&amp;quot; &amp;lt;&amp;lt; std::endl; }
  Snitch(Snitch const&amp;amp;) { std::cout &amp;lt;&amp;lt; &amp;quot;copy c&#39;tor&amp;quot; &amp;lt;&amp;lt; std::endl; }
  Snitch(Snitch&amp;amp;&amp;amp;) { std::cout &amp;lt;&amp;lt; &amp;quot;move c&#39;tor&amp;quot; &amp;lt;&amp;lt; std::endl; }
};

int main() {
  auto snitch = std::make_shared&amp;lt;Snitch&amp;gt;();
  auto another_snitch = snitch;
  std::cout &amp;lt;&amp;lt; &amp;quot;Equal?: &amp;quot; &amp;lt;&amp;lt; (snitch == another_snitch) &amp;lt;&amp;lt; std::endl;

  {
    auto scoped_snitch = snitch;
    auto another_scoped_snitch = scoped_snitch;
  }  // destroy &#39;another_scoped_snitch&#39; and &#39;scoped_snitch&#39;
}  // destroy &#39;snother_snitch&#39; and &#39;snitch&#39;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;c&#39;tor
Equal?: 1
d&#39;tor
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that only 1 instance of &lt;code&gt;Snitch&lt;/code&gt; is ever created, and that no copy/move
constructors are used. All of &lt;code&gt;snitch&lt;/code&gt;, &lt;code&gt;another_snitch&lt;/code&gt;, &lt;code&gt;scoped_snitch&lt;/code&gt; and
&lt;code&gt;another_scoped_snitch&lt;/code&gt; are equal. Also note that &lt;code&gt;snitch&lt;/code&gt; has the type
&lt;code&gt;shared_ptr&amp;lt;Snitch&amp;gt;&lt;/code&gt;, as this is &lt;code&gt;make_shared()&lt;/code&gt;&amp;rsquo;s return type:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;std::is_same&amp;lt;decltype(snitch), std::shared_ptr&amp;lt;Snitch&amp;gt;&amp;gt;::value == true
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We&amp;rsquo;ll go into &lt;code&gt;make_shared&lt;/code&gt; soon.&lt;/p&gt;

&lt;h2 id=&#34;performance-thread-safety&#34;&gt;Performance &amp;amp; thread safety&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;unique_ptr&lt;/code&gt; has performance similar to a raw pointer (with compiler
optimizations), and also the size of a raw pointer. This is not the case for
&lt;code&gt;shared_ptr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;shared_ptr&lt;/code&gt; must have at least 2 pointers, so it&amp;rsquo;s bigger than a raw pointer.
It also guarantees thread-safety for all methods &lt;strong&gt;as long as each thread has
its own copy&lt;/strong&gt;. Thread safety includes assigment, reference increment / decrement and
all other operations. However, it does &lt;strong&gt;not&lt;/strong&gt; mean that locks are acquired
prior to calling any of &lt;code&gt;T&lt;/code&gt;&amp;rsquo;s methods - only &lt;code&gt;shared_ptr&lt;/code&gt;&amp;rsquo;s own methods are
guaranteed to be thread-safe. In other words, if you want &lt;code&gt;T&lt;/code&gt; to be used from
multiple thread concurrently you will have to implement thread-safety yourself.&lt;/p&gt;

&lt;p&gt;As always, thread safety comes at a price &amp;ndash; performance. For most cases it
would probably be minimal, by utilizing atomic operations, but it&amp;rsquo;s not
guaranteed to be atomic and even atomics are not free.&lt;/p&gt;

&lt;p&gt;If you&amp;rsquo;d like to read more on performance of C++ smart pointers vs raw pointers
check out &lt;a href=&#34;http://blog.davidecoppola.com/2016/10/performance-of-raw-pointers-vs-smart-pointers-in-cpp/&#34;&gt;this cool post by Davide Coppola&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&#34;std-make-shared&#34;&gt;&lt;code&gt;std::make_shared()&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Much like &lt;code&gt;make_unique()&lt;/code&gt;, &lt;code&gt;make_shared()&lt;/code&gt; saves us from using &lt;code&gt;new&lt;/code&gt; directly,
arguably prodoces cleaner code, and is exception safe. In addition to all of
these, and unlike &lt;code&gt;make_unique()&lt;/code&gt;, it also brings a performance advantage.&lt;/p&gt;

&lt;p&gt;Performance? Yes, &lt;em&gt;performance&lt;/em&gt;. &lt;code&gt;shared_ptr&amp;lt;T&amp;gt;&lt;/code&gt; manages a reference count to
know when to release &lt;code&gt;T&lt;/code&gt;. This is done via a shared &lt;em&gt;control block&lt;/em&gt; to which all
&lt;code&gt;shared_ptr&lt;/code&gt;s point. Therefore, it must be dynamically allocated. And of course
there&amp;rsquo;s also the object itself, &lt;code&gt;T&lt;/code&gt;, which needs to be dynamically allocated as
well.&lt;/p&gt;

&lt;p&gt;So creating a new &lt;code&gt;shared_ptr&lt;/code&gt; would create 2 objects, thus call &lt;code&gt;new&lt;/code&gt; twice.
However, &lt;code&gt;make_shared()&lt;/code&gt; can make a single allocation for both, and thus save
some load. Cool, right?&lt;/p&gt;

&lt;p&gt;It&amp;rsquo;s good to know that:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;This can&amp;rsquo;t possibly be done through &lt;code&gt;shared_ptr&lt;/code&gt;&amp;rsquo;s constructor, as the
constructor is called on an already allocated memory block and there&amp;rsquo;s no
&lt;code&gt;realloc()&lt;/code&gt; equivalent in C++.&lt;/li&gt;
&lt;li&gt;You may use &lt;code&gt;std::allocate_shared()&lt;/code&gt; if you need a custom allocator.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&#34;construct-from-unique-ptr&#34;&gt;Construct from &lt;code&gt;unique_ptr&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;shared_ptr&lt;/code&gt; has a special constructor that accepts a &lt;code&gt;unique_ptr&amp;amp;&amp;amp;&lt;/code&gt;. This is
useful when working with factories that return a &lt;code&gt;unique_ptr&lt;/code&gt;, but you want to
assign the value to a &lt;code&gt;shared_ptr&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;std::unique_ptr&amp;lt;MyObject&amp;gt; CreateMyObject() {
  return std::make_unique&amp;lt;MyObject&amp;gt;();
}

int main() {
  std::shared_ptr&amp;lt;MyObject&amp;gt; shared_object = CreateMyObject();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you&amp;rsquo;re implementing a factory and don&amp;rsquo;t know if your callers will assign
the value to a &lt;code&gt;unique_ptr&lt;/code&gt; or a &lt;code&gt;shared_ptr&lt;/code&gt; - always return &lt;code&gt;unique_ptr&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&#34;no-release-method-reset-doesn-t-necessarily-release&#34;&gt;No &lt;code&gt;release()&lt;/code&gt; method, &lt;code&gt;reset()&lt;/code&gt; doesn&amp;rsquo;t necessarily release&lt;/h2&gt;

&lt;p&gt;Unlike &lt;code&gt;unique_ptr&lt;/code&gt;, &lt;code&gt;shared_ptr&lt;/code&gt; does not have a &lt;code&gt;release()&lt;/code&gt; method. It
wouldn&amp;rsquo;t make sense to implement such a method since there&amp;rsquo;s oftentimes no way
to determine &lt;em&gt;at compile time&lt;/em&gt; how many &lt;code&gt;shared_ptr&lt;/code&gt;s point to the same
instance.&lt;/p&gt;

&lt;p&gt;On the other hand, &lt;code&gt;reset()&lt;/code&gt; exists, but it does not necessarily delete the
underlying object. Here&amp;rsquo;s an example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;type_traits&amp;gt;
#include &amp;lt;memory&amp;gt;

struct Snitch {  // Same as above, no changes
public:
  Snitch() { std::cout &amp;lt;&amp;lt; &amp;quot;c&#39;tor&amp;quot; &amp;lt;&amp;lt; std::endl; }
  ~Snitch() { std::cout &amp;lt;&amp;lt; &amp;quot;d&#39;tor&amp;quot; &amp;lt;&amp;lt; std::endl; }
  Snitch(Snitch const&amp;amp;) { std::cout &amp;lt;&amp;lt; &amp;quot;copy c&#39;tor&amp;quot; &amp;lt;&amp;lt; std::endl; }
  Snitch(Snitch&amp;amp;&amp;amp;) { std::cout &amp;lt;&amp;lt; &amp;quot;move c&#39;tor&amp;quot; &amp;lt;&amp;lt; std::endl; }
};

int main() {
  std::cout &amp;lt;&amp;lt; &amp;quot;Creating 1st Snitch&amp;quot; &amp;lt;&amp;lt; std::endl;
  auto snitch1 = std::make_shared&amp;lt;Snitch&amp;gt;();
  auto snitch2 = snitch1;

  std::cout &amp;lt;&amp;lt; &amp;quot;Calling reset&amp;quot; &amp;lt;&amp;lt; std::endl;
  snitch1.reset();  // object will *not* be released

  std::cout &amp;lt;&amp;lt; &amp;quot;Moving out of scope&amp;quot; &amp;lt;&amp;lt; std::endl;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;Creating 1st Snitch
c&#39;tor
Calling reset
Moving out of scope
d&#39;tor
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;cyclic-references-std-weak-ptr&#34;&gt;Cyclic references &amp;amp; &lt;code&gt;std::weak_ptr&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;shared_ptr&lt;/code&gt;s are almost perfect. Their one imperfection is that they don&amp;rsquo;t
support cycles. Example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;type_traits&amp;gt;
#include &amp;lt;memory&amp;gt;

struct Node {  // Binary tree
  Node() { std::cout &amp;lt;&amp;lt; &amp;quot;c&#39;tor&amp;quot; &amp;lt;&amp;lt; std::endl; }
  ~Node() { std::cout &amp;lt;&amp;lt; &amp;quot;d&#39;tor&amp;quot; &amp;lt;&amp;lt; std::endl; }

  std::shared_ptr&amp;lt;Node&amp;gt; parent;
  std::shared_ptr&amp;lt;Node&amp;gt; left;
  std::shared_ptr&amp;lt;Node&amp;gt; right;
};

int main() {
  auto root = std::make_shared&amp;lt;Node&amp;gt;();
  root-&amp;gt;left = std::make_shared&amp;lt;Node&amp;gt;();
  root-&amp;gt;left-&amp;gt;parent = root;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;c&#39;tor
c&#39;tor
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, no destructor has been called due to the vicious cycle I
introduced, thus a memory leak occurred.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;weak_ptr&lt;/code&gt; was created to allow us to have cycles that won&amp;rsquo;t leak. A &lt;code&gt;weak_ptr&lt;/code&gt;
holds a non-owning pointer. Essentially it means that &lt;code&gt;weak_ptr&lt;/code&gt; won&amp;rsquo;t prevent
its pointee from being released.&lt;/p&gt;

&lt;p&gt;In the above example, simply modifying the parent declaration from&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;  std::shared_ptr&amp;lt;Node&amp;gt; parent;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;  std::weak_ptr&amp;lt;Node&amp;gt; parent;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Without changing anything else in the code, and we get the following output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;c&#39;tor
c&#39;tor
d&#39;tor
d&#39;tor
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Magic, right? That&amp;rsquo;s cool, however there are a few things we should know. The
most important is that the object pointed by the &lt;code&gt;weak_ptr&lt;/code&gt; can be released
while the &lt;code&gt;weak_ptr&lt;/code&gt; is still alive. That&amp;rsquo;s pretty much the definition of a weak
pointer.&lt;/p&gt;

&lt;p&gt;Another thing is that &lt;code&gt;weak_ptr&lt;/code&gt; has a very basic API, which &lt;strong&gt;does not even
include a &lt;code&gt;get()&lt;/code&gt; method&lt;/strong&gt;. WAT? Yes. But, of course, it&amp;rsquo;s not useless. In order
to use the object pointed to by a &lt;code&gt;weak_ptr&lt;/code&gt; one must &lt;em&gt;upgrade&lt;/em&gt; it to a
&lt;code&gt;shared_ptr&lt;/code&gt; by calling &lt;code&gt;lock()&lt;/code&gt; and checking if the returned &lt;code&gt;shared_ptr&lt;/code&gt; is
empty:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;  std::weak_ptr&amp;lt;std::string&amp;gt; weak = // ...
  std::shared_ptr&amp;lt;std::string&amp;gt; shared = weak.lock();
  if (shared) {
    // object exists
  } else {
    // object has been released
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once we have a &lt;code&gt;shared_ptr&lt;/code&gt; in our hands the object no longer can be released,
so that&amp;rsquo;s a pretty smart and cool design decision.&lt;/p&gt;

&lt;p&gt;One caveat of using &lt;code&gt;weak_ptr&lt;/code&gt; is that while the &lt;em&gt;object&lt;/em&gt; is released after the
last &lt;code&gt;shared_ptr&lt;/code&gt; is released, the &lt;em&gt;control block&lt;/em&gt; remains alive until the last
&lt;code&gt;weak_ptr&lt;/code&gt; is released. If the control block and object are allocated together
(see &lt;code&gt;make_shared&lt;/code&gt; above) &amp;ndash; this would mean that &lt;code&gt;weak_ptr&lt;/code&gt; will cause the
memory to remain alive (even though the object will be destroyed).&lt;/p&gt;

&lt;h2 id=&#34;control-block&#34;&gt;Control block&lt;/h2&gt;

&lt;p&gt;As previously mentioned, &lt;code&gt;shared_ptr&lt;/code&gt;s are managed via a &lt;em&gt;control block&lt;/em&gt;. These
control blocks are up to the implementations to define, however they generally
contain the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The managed object (either a pointer or the object itself if created via
&lt;code&gt;make_unique()&lt;/code&gt;);&lt;/li&gt;
&lt;li&gt;Reference count (for both other &lt;code&gt;shared_ptr&lt;/code&gt;s and &lt;code&gt;weak_ptr&lt;/code&gt;s);&lt;/li&gt;
&lt;li&gt;Deletion function.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The control block is always accessed in a thread-safe way, either via atomics or
a mutex.&lt;/p&gt;

&lt;p&gt;Earlier I wrote that a &lt;code&gt;shared_ptr&lt;/code&gt; has the size of 2 pointers, while here I
descrive the control block as pointing to (or containing) the object. So what
does &lt;code&gt;shared_ptr&lt;/code&gt; need to point to, other than the control block? Read the next
section to find out :)&lt;/p&gt;

&lt;h2 id=&#34;point-to-a-manage-b&#34;&gt;Point to &lt;code&gt;A&lt;/code&gt;, manage &lt;code&gt;B&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;This may sound somewhat bizarre at first, so bear with me. Say you have an
internal object, &lt;code&gt;B&lt;/code&gt;. This &lt;code&gt;B&lt;/code&gt; has a few fields, but one of them, &lt;code&gt;A&lt;/code&gt;, is
exposed externally. One possible such scenario is where &lt;code&gt;B&lt;/code&gt; is a channel to a
database including the internal socket etc, and &lt;code&gt;A&lt;/code&gt; is the API object on which a
user acts. Usually you would have these as private members, or hide them behind
an interface. But, again, bear with me &amp;ndash; suppose you have a good reason.&lt;/p&gt;

&lt;p&gt;Now, you want to manage &lt;code&gt;B&lt;/code&gt; in a shared way, but you only want to give users &lt;code&gt;A&lt;/code&gt;
  what do you do? It turns out &lt;code&gt;shared_ptr&lt;/code&gt; supports this via a feature called
&lt;em&gt;aliasing&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;With aliasing one can create a &lt;code&gt;shared_ptr&lt;/code&gt; from another &lt;code&gt;shared_ptr&lt;/code&gt;, so that
their control blocks are the same, but have the &lt;code&gt;get()&lt;/code&gt; method return any
arbitrary pointer, even one that has nothing to do with them.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s an example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;struct DatabaseConnection {}; // exposed to the user

struct InternalDatabaseConnection {
  // socket
  // authentication information
  DatabaseConnection connection;
};

std::shared_ptr&amp;lt;DatabaseConnection&amp;gt; CreateDatabaseConnection() {
  auto tmp = std::make_shared&amp;lt;InternalDatabaseConnection&amp;gt;();
  return std::shared_ptr&amp;lt;DatabaseConnection&amp;gt;(tmp, &amp;amp;tmp-&amp;gt;connection);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that &lt;code&gt;delete&lt;/code&gt; will never be called on &lt;code&gt;&amp;amp;tmp-&amp;gt;connection&lt;/code&gt; which is a
&lt;code&gt;DatabaseConnection&lt;/code&gt;, but rather only on &lt;code&gt;InternalDatabaseConnection&lt;/code&gt; allocated
by &lt;code&gt;make_shared&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&#34;casting&#34;&gt;Casting&lt;/h2&gt;

&lt;p&gt;Like &lt;code&gt;unique_ptr&lt;/code&gt;, &lt;code&gt;shared_ptr&lt;/code&gt; also supports automatic cast from
&lt;code&gt;shared_ptr&amp;lt;T&amp;gt;&lt;/code&gt; to &lt;code&gt;shared_ptr&amp;lt;U&amp;gt;&lt;/code&gt; if &lt;code&gt;T*&lt;/code&gt; is convertible to &lt;code&gt;U*&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Unlike &lt;code&gt;unique_ptr&lt;/code&gt;, &lt;code&gt;shared_ptr&lt;/code&gt; will always call the destructor it was
constructed with, even when casting to a parent with no virtual destructor.
Example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;memory&amp;gt;

struct Base { ~Base() { std::cout &amp;lt;&amp;lt; &amp;quot;non-virtual ~Base()&amp;quot; &amp;lt;&amp;lt; std::endl; } };
struct Derived : Base { ~Derived() { std::cout &amp;lt;&amp;lt; &amp;quot;~Derived()&amp;quot; &amp;lt;&amp;lt; std::endl; } };

int main() {
	std::shared_ptr&amp;lt;Base&amp;gt; base = std::make_shared&amp;lt;Derived&amp;gt;();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;~Derived()
non-virtual ~Base()
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If we were to replace &lt;code&gt;shared_ptr&lt;/code&gt; with &lt;code&gt;unique_ptr&lt;/code&gt; (and &lt;code&gt;make_shared&lt;/code&gt; with
&lt;code&gt;make_unique&lt;/code&gt;) the program would not call &lt;code&gt;~Derived&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In addition to that, there are 4 utility functions to allow creating a
&lt;code&gt;shared_ptr&lt;/code&gt; when implicit conversion doesn&amp;rsquo;t happen:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;std::static_pointer_cast&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;std::dynamic_pointer_cast&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;std::const_pointer_cast&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;std::reinterpret_pointer_cast&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let&amp;rsquo;s look at an example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;auto derived = std::make_shared&amp;lt;Derived&amp;gt;();
std::shared_ptr&amp;lt;Base&amp;gt; base = derived;  // OK.
//std::shared_ptr&amp;lt;Derived&amp;gt; derived2 = base;  // ERROR: no implicit down-cast.
std::shared_ptr&amp;lt;Derived&amp;gt; derived2 = std::static_pointer_cast&amp;lt;Derived&amp;gt;(base);
std::shared_ptr&amp;lt;Derived&amp;gt; derived3 = std::dynamic_pointer_cast&amp;lt;Derived&amp;gt;(base);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that &lt;code&gt;T*&lt;/code&gt; needs to be convertible to &lt;code&gt;U*&lt;/code&gt;, which is different from &lt;code&gt;T&lt;/code&gt;
being convertible to &lt;code&gt;U&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;auto shared_short = std::make_shared&amp;lt;short&amp;gt;(123);
//std::shared_ptr&amp;lt;int&amp;gt; shared_int = shared_short;  // ERROR: no cast from short* to int*.
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;std-enable-shared-from-this&#34;&gt;&lt;code&gt;std::enable_shared_from_this&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;This is somewhat odd and very specific, so one last time I need you to bear with
me;&lt;/p&gt;

&lt;p&gt;Suppose you&amp;rsquo;re implementing a class &lt;code&gt;WeirdClass&lt;/code&gt;. And suppose you know that this
class will be managed by &lt;code&gt;shared_ptr&lt;/code&gt;. And suppose that for some reason you
would like to return a &lt;code&gt;shared_ptr&lt;/code&gt; &lt;strong&gt;to yourself&lt;/strong&gt; (yourself being an instance
of &lt;code&gt;WeirdClass&lt;/code&gt;). How would you do that? Let&amp;rsquo;s consider the following:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;memory&amp;gt;

struct WeirdClass {
  std::shared_ptr&amp;lt;WeirdClass&amp;gt; CreateSharedPtrToThis() {
    return std::shared_ptr&amp;lt;WeirdClass&amp;gt;(this);  // DON&#39;T DO THIS.
  }
};

int main() {
  auto weird_class = std::make_shared&amp;lt;WeirdClass&amp;gt;();
  auto tmp = weird_class-&amp;gt;CreateSharedPtrToThis();
}  // ERROR: double delete
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This kind of error can generally be avoided by not calling &lt;code&gt;shared_ptr&lt;/code&gt;&amp;rsquo;s
constructor directly, but &lt;code&gt;std::make_shared&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;However, in this specific case the object is already allocated - we merely want
to copy a &lt;code&gt;shared_ptr&lt;/code&gt; that already exists, but is unknown in the context of
&lt;code&gt;WeirdClass&lt;/code&gt;. What do we do? This is exactly why &lt;code&gt;std::enable_shared_from_this&lt;/code&gt;
was invented:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;memory&amp;gt;

struct WeirdClass : std::enable_shared_from_this&amp;lt;WeirdClass&amp;gt; {
  std::shared_ptr&amp;lt;WeirdClass&amp;gt; CreateSharedPtrToThis() {
    return shared_from_this();
  }
};

int main() {
  auto weird_class = std::make_shared&amp;lt;WeirdClass&amp;gt;();
  auto tmp = weird_class-&amp;gt;CreateSharedPtrToThis();
}  // no problem!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But please, don&amp;rsquo;t take this as a reason to use &lt;code&gt;enable_shared_from_this&lt;/code&gt;. Some
features are best not used :)&lt;/p&gt;

&lt;h2 id=&#34;that-s-it-for-today&#34;&gt;That&amp;rsquo;s it for today&lt;/h2&gt;

&lt;p&gt;I hope you found this post useful. Please let me know if I missed anything, have
an error somewhere, or if you have any question!&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Exploring std::unique_ptr</title>
      <link>https://shaharmike.com/cpp/unique-ptr/</link>
      <pubDate>Sat, 12 Nov 2016 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/unique-ptr/</guid>
      <description>&lt;p&gt;Today we&amp;rsquo;ll talk about C++&amp;rsquo;s built-in smart pointer &lt;code&gt;std::unique_ptr&lt;/code&gt;, which is
an extremely powerful, simple &amp;amp; common tool.&lt;/p&gt;

&lt;h2 id=&#34;std-unique-ptr&#34;&gt;&lt;code&gt;std::unique_ptr&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;C++98 had &lt;code&gt;std::auto_ptr&lt;/code&gt;. It&amp;rsquo;s problematic in areas I do not wish to discuss
here, and so it was deprecated and replaced by the awesome &lt;code&gt;unique_ptr&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;unique_ptr&lt;/code&gt; is a simple &amp;lsquo;smart&amp;rsquo; pointer: it holds an instance of an object and
deletes it when it goes out of scope. In terms of lifetime behvior it&amp;rsquo;s much
like any regular C++ object with a constructor and destructor, only with dynamic
memory allocation. No reference counting, no fancy tricks.&lt;/p&gt;

&lt;p&gt;And that&amp;rsquo;s the beauty. Simple, elegant &amp;amp; efficient, yet extremely powerful. With
&lt;code&gt;unique_ptr&lt;/code&gt; you no longer have to worry about &lt;code&gt;new&lt;/code&gt; and &lt;code&gt;delete&lt;/code&gt;. Simply call
&lt;code&gt;make_unique()&lt;/code&gt; (instead of &lt;code&gt;new&lt;/code&gt;), and the destructor will call &lt;code&gt;delete&lt;/code&gt;
automatically. It&amp;rsquo;s as simple as that, and covers 90% of use-cases that require
dynamic memory.&lt;/p&gt;

&lt;h2 id=&#34;std-make-unique&#34;&gt;&lt;code&gt;std::make_unique()&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;Above I mentioned &lt;code&gt;make_unique()&lt;/code&gt;. This is a new standard function that came in
C++14. You may think that it&amp;rsquo;s supposed to save us the need to typing the type
we&amp;rsquo;re interested in, similar to &lt;code&gt;make_pair&lt;/code&gt;. Well, not quite. Let&amp;rsquo;s look at
&lt;code&gt;make_unique()&lt;/code&gt;&amp;rsquo;s signature:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;// inside namespace std
template &amp;lt;typename T, typename ... Args&amp;gt;
std::unique_ptr&amp;lt;T&amp;gt; make_unique(Args&amp;amp;&amp;amp;... args);
// &#39;&amp;amp;&amp;amp;&#39; here means forwarding references, not necessarily rvalues. I hope to
// explain what these are in a future post.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that &lt;code&gt;T&lt;/code&gt; is not an argument to the function, thus it can&amp;rsquo;t possible be
deduced by the compiler. So to use &lt;code&gt;make_unique()&lt;/code&gt; one must &lt;em&gt;always&lt;/em&gt; provide T
explicitly, like so:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;auto u_int = std::make_unique&amp;lt;int&amp;gt;(123);
cout &amp;lt;&amp;lt; (*u_int == 123) &amp;lt;&amp;lt; endl;
auto u_string = std::make_unique&amp;lt;std::string&amp;gt;(3, &#39;#&#39;);
cout &amp;lt;&amp;lt; (*u_string == &amp;quot;###&amp;quot;) &amp;lt;&amp;lt; endl;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;1
1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So essentially all &lt;code&gt;make_unique()&lt;/code&gt; does is call &lt;code&gt;new&lt;/code&gt; and pass &lt;code&gt;args&lt;/code&gt; as
arguments to &lt;code&gt;T&lt;/code&gt;&amp;rsquo;s constructor. As a matter of fact, here is a feature-complete
implementation:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;template &amp;lt;typename T, typename ... Args&amp;gt;
std::unique_ptr&amp;lt;T&amp;gt; make_unique(Args&amp;amp;&amp;amp; ... args) {
  return std::unique_ptr&amp;lt;T&amp;gt;(new T(std::forward&amp;lt;Args&amp;gt;(args)...));
  // If you don&#39;t know what std::forward it simply read it as:
  // return std::unique_ptr&amp;lt;T&amp;gt;(new T(args...));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;rsquo;s it. If so, why do we even need it? What&amp;rsquo;s the difference between the
following?:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;auto a = std::make_unique&amp;lt;MyClass&amp;gt;();
auto b = std::unique_ptr&amp;lt;MyClass&amp;gt;(new MyClass());
std::unique_ptr&amp;lt;MyClass&amp;gt; c(new MyClass());
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;First difference is exception safety. In C++ &amp;lt; 17 calling the following &lt;em&gt;can&lt;/em&gt;
lead to a memory leak:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;MyFunction(std::unique_ptr&amp;lt;MyClass&amp;gt;(new MyClass()),
           std::unique_ptr&amp;lt;MyClass&amp;gt;(new MyClass()));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;How? Well, C++ doesn&amp;rsquo;t define the order of evaluation even between
sub-expressions, so in theory it could evaluate the first &lt;code&gt;new MyClass()&lt;/code&gt;, then
the second &lt;code&gt;new MyClass()&lt;/code&gt; and only then &lt;code&gt;unique_ptr&lt;/code&gt;&amp;rsquo;s constructors. Now, if
the first call to &lt;code&gt;new&lt;/code&gt; succeeds and the second call to &lt;code&gt;new&lt;/code&gt; throws an
exception (like from &lt;code&gt;MyClass&lt;/code&gt;&amp;rsquo;s constructor) it would leak memory as no class
owns the newly created object yet.&lt;/p&gt;

&lt;p&gt;But it&amp;rsquo;s not just exception safety. Look at the lines above, comparing the
initialization of &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt; and &lt;code&gt;c&lt;/code&gt;. I think that &lt;code&gt;a&lt;/code&gt; is the cleanest and least
verbose. Furthermore, it&amp;rsquo;s the only one who doesn&amp;rsquo;t repeat &lt;code&gt;MyClass&lt;/code&gt; twice.&lt;/p&gt;

&lt;p&gt;And one last thing - I also prefer using &lt;code&gt;make_unique()&lt;/code&gt; for assignment, not
only for construction:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;auto a = std::make_unique&amp;lt;int&amp;gt;(123);
a = std::make_unique&amp;lt;int&amp;gt;(456);  // Cool.
a.reset(new int(789));  // Works, but not as nice.
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;how-to-use-unique-ptr&#34;&gt;How to use &lt;code&gt;unique_ptr&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Use &lt;code&gt;unique_ptr&lt;/code&gt; to represent any owned object that&amp;rsquo;s not shared&lt;/strong&gt;. Here are
some common examples:&lt;/p&gt;

&lt;h3 id=&#34;return-a-dynamically-allocated-object-from-a-function&#34;&gt;Return a dynamically allocated object from a function&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;std::unique_ptr&amp;lt;MyObject&amp;gt; CreateMyObject();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This way whoever &lt;em&gt;calls&lt;/em&gt; your function won&amp;rsquo;t leak. Even if they don&amp;rsquo;t assign the
returned value to a variable:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;SomeFunction();
CreateMyObject();  // no assignment, yet no leak
SomeOtherFunction();
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&#34;take-ownership-of-a-dynamically-allocated-object&#34;&gt;Take ownership of a dynamically-allocated object&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;void TakeOwnership(std::unique_ptr&amp;lt;AnObject&amp;gt; obj);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With such signature callers can&amp;rsquo;t ignore the fact that you&amp;rsquo;re taking ownership
of obj:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;TakeOwnership(3);  // ERROR: no conversion from &#39;int&#39; to &#39;std::unique_ptr&amp;lt;int&amp;gt;&#39;

int* p = new int(3);
TakeOwnership(p);  // ERROR: no conversion from &#39;int*&#39; to &#39;std::unique_ptr&amp;lt;int&amp;gt;&#39;
                   // This is because unique_ptr&#39;s constructor is explicit.

auto u = std::make_unique&amp;lt;int&amp;gt;(3);
TakeOwnership(u);  // ERROR: no copy constructor

TakeOwnership(std::move(u));  // OK
TakeOwnership(std::make_unique&amp;lt;int&amp;gt;(3));  // OK
TakeOwnership(nullptr);  // OK -- due to constructor accepting nullptr_t
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you may have noticed, &lt;code&gt;unique_ptr&lt;/code&gt; supports C++11&amp;rsquo;s move semantics, but does
not allow copying. This makes sense &amp;ndash; as the name suggests, each instance is
supposed to only track a unique instance.&lt;/p&gt;

&lt;h3 id=&#34;dynamically-allocated-class-members&#34;&gt;Dynamically-allocated class members&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;unique_ptr&lt;/code&gt; to automatically release class members when a class is
released:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;class SomeObject {
  // No destructor needed

private:
  std::unique_ptr&amp;lt;SomethingElse&amp;gt; m_SomethingElse;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;casting&#34;&gt;Casting&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;unique_ptr&lt;/code&gt; supports construction of &lt;code&gt;unique_ptr&amp;lt;T&amp;gt;&lt;/code&gt; from &lt;code&gt;unique_ptr&amp;lt;U&amp;gt;&lt;/code&gt; if
&lt;code&gt;T*&lt;/code&gt; is convertible to &lt;code&gt;U*&lt;/code&gt; (which usually means up-casting). Example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;struct Base { virtual ~Base() = default; };
struct Derived : Base {};

// ...
std::unique_ptr&amp;lt;Derived&amp;gt; derived = std::make_unique&amp;lt;Derived&amp;gt;();
std::unique_ptr&amp;lt;Base&amp;gt; base(std::move(derived));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Note that we must call &lt;code&gt;std::move()&lt;/code&gt; on &lt;code&gt;derived&lt;/code&gt; &amp;ndash; that&amp;rsquo;s because there can&amp;rsquo;t
be 2 instances of &lt;code&gt;unique_ptr&lt;/code&gt; pointing to the same object, even if they are of
different types.&lt;/p&gt;

&lt;h2 id=&#34;custom-deleter&#34;&gt;Custom deleter&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;unique_ptr&lt;/code&gt; allows to specify a custom object that will be used for releasing
the object. To use this, however, one must provide a second template argument.
For example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;FILE* file = fopen(&amp;quot;...&amp;quot;, &amp;quot;r&amp;quot;);
auto FILE_releaser = [](FILE* f) { fclose(f); };
std::unique_ptr&amp;lt;FILE, decltype(FILE_releaser)&amp;gt; file_ptr(file, FILE_releaser);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As demonstrated above, this can be very useful when working with C APIs, or APIs
which have custom release logic.&lt;/p&gt;

&lt;p&gt;Notes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;file_ptr&lt;/code&gt; above is not compatible with &lt;code&gt;std::unique_ptr&amp;lt;FILE&amp;gt;&lt;/code&gt; as their 2nd
template argument is different. Move-assignment, for example, will fail.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;file_ptr&lt;/code&gt; still have the size of a single pointer. However, if we created
&lt;code&gt;FILE_releaser&lt;/code&gt; such that it captured variables &amp;ndash; then &lt;code&gt;file_ptr&lt;/code&gt;&amp;rsquo;s size
would have increased as well.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;misusing-unique-ptr&#34;&gt;Misusing &lt;code&gt;unique_ptr&lt;/code&gt;&lt;/h2&gt;

&lt;p&gt;If you try hard, you &lt;em&gt;could&lt;/em&gt; do some nasty things with &lt;code&gt;unique_ptr&lt;/code&gt;. But you
have to put some effort to do so. Here are a few examples:&lt;/p&gt;

&lt;h3 id=&#34;assigning-the-same-pointer-to-multiple-unique-ptr-s&#34;&gt;Assigning the same pointer to multiple &lt;code&gt;unique_ptr&lt;/code&gt;s&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;int* p = new int(123);
std::unique_ptr&amp;lt;int&amp;gt; a(p);
std::unique_ptr&amp;lt;int&amp;gt; b(p);  // Oops - b&#39;s destructor will double delete p
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The above example can be avoided by always using &lt;code&gt;make_unique()&lt;/code&gt; instead of
calling &lt;code&gt;new&lt;/code&gt; directly.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s a similar example, but done without calling &lt;code&gt;new&lt;/code&gt; directly:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;std::unique_ptr&amp;lt;int&amp;gt; a = std::make_unique&amp;lt;int&amp;gt;(123);
std::unique_ptr&amp;lt;int&amp;gt; b(a.get());
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Using &lt;code&gt;unique_ptr&lt;/code&gt;&amp;rsquo;s constructor directly is not recommended, and passing the
pointer returned by &lt;code&gt;.get()&lt;/code&gt; to a function that is taking ownership is an error.&lt;/p&gt;

&lt;h3 id=&#34;deleting-memory-managed-by-unique-ptr-s&#34;&gt;Deleting memory managed by &lt;code&gt;unique_ptr&lt;/code&gt;s&lt;/h3&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;auto u = std::make_unique&amp;lt;int&amp;gt;(123);
delete u.get();
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is an example of why you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Don&amp;rsquo;t want to call &lt;code&gt;delete&lt;/code&gt; directly;&lt;/li&gt;
&lt;li&gt;Are not supposed to mess with &lt;code&gt;unique_ptr&lt;/code&gt;&amp;rsquo;s memory&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&#34;a-word-about-arrays&#34;&gt;A word about arrays&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;unique_ptr&lt;/code&gt; also have partial specializaion to handle arrays. Specifically it
calls &lt;code&gt;delete[]&lt;/code&gt; rather than &lt;code&gt;delete&lt;/code&gt;. However, using C-style arrays is
something that should generally be avoided. Prefer &lt;code&gt;std::array&lt;/code&gt; or &lt;code&gt;std::vector&lt;/code&gt;
where possible.&lt;/p&gt;

&lt;h2 id=&#34;that-s-it-for-today&#34;&gt;That&amp;rsquo;s it for today&lt;/h2&gt;

&lt;p&gt;In the next post we&amp;rsquo;ll talk about &lt;a href=&#34;https://shaharmike.com/cpp/shared-ptr/&#34;&gt;&lt;code&gt;std::shared_ptr&lt;/code&gt;&lt;/a&gt;
&amp;ndash; &lt;code&gt;unique_ptr&lt;/code&gt;&amp;rsquo;s brother which is very interesting, however less frequently
used.&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Exploring std::string</title>
      <link>https://shaharmike.com/cpp/std-string/</link>
      <pubDate>Mon, 12 Sep 2016 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/std-string/</guid>
      <description>&lt;p&gt;Every C++ developer knows that &lt;code&gt;std::string&lt;/code&gt; represents a sequence of characters
in memory. It manages its own memory, and is very intuitive to use.
Today we&amp;rsquo;ll explore &lt;code&gt;std::string&lt;/code&gt; as defined by the C++ Standard, and also by
looking at 4 major implementations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Quick note&lt;/strong&gt;: in this post I use the notation of &lt;em&gt;compilers&lt;/em&gt; to inspect
implementations. This is technically incorrect, as a Standard library is not
necessarily tied to a specific compiler. So when I say, for example, &amp;lsquo;GCC&amp;rsquo; I
really mean &amp;lsquo;GCC with its default Standard library&amp;rsquo;, which is libstdc++. For
clang the library is libc++. For Microsoft Visual Studio its the library that
ships with it, which is implemented by Dinkumware (I think).&lt;/p&gt;

&lt;h2 id=&#34;size&#34;&gt;Size&lt;/h2&gt;

&lt;p&gt;We&amp;rsquo;ll start with the most basic thing - what&amp;rsquo;s the &lt;code&gt;sizeof&lt;/code&gt; of a &lt;code&gt;std::string&lt;/code&gt;
(not including any dynamic allocation)?&lt;/p&gt;

&lt;p&gt;A naive implementation would require 3 fields, each the size of a pointer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pointer to the allocated memory;&lt;/li&gt;
&lt;li&gt;Logical size of string;&lt;/li&gt;
&lt;li&gt;Size of allocated memory (which must be bigger than or equal to logical size).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On a 64-bit machine the above would consume 24 bytes. Let&amp;rsquo;s examine the output
of the following code on different compilers, all using 64-bit architecture:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;string&amp;gt;
#include &amp;lt;iostream&amp;gt;

int main() {
    std::cout &amp;lt;&amp;lt; sizeof(std::string) &amp;lt;&amp;lt; std::endl;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Visual Studio 14&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;40
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;GCC &amp;lt; 5&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ g++ -std=c++11 main.cpp &amp;amp;&amp;amp; ./a.out
8
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;GCC &amp;gt;= 5&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ g++-5 -std=c++11 main.cpp &amp;amp;&amp;amp; ./a.out
32
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;clang&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++ -std=c++11 -stdlib=libc++ main.cpp &amp;amp;&amp;amp; ./a.out
24
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Keep these very different numbers in mind, and let&amp;rsquo;s continue exploring.&lt;/p&gt;

&lt;h2 id=&#34;small-object-optimization&#34;&gt;Small Object Optimization&lt;/h2&gt;

&lt;p&gt;One particular optimization found its way to pretty much all implementations:
&lt;em&gt;small objects optimization&lt;/em&gt; (aka &lt;em&gt;small buffer optimization&lt;/em&gt;). Simply put,
Small Object Optimization means that the &lt;code&gt;std::string&lt;/code&gt; object has a small
buffer for small strings, which saves dynamic allocations.&lt;/p&gt;

&lt;p&gt;One might add a buffer on top of existing fields of &lt;code&gt;std::string&lt;/code&gt;. However,
there are some smart tricks to better use existing size.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;cstdlib&amp;gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;string&amp;gt;

// replace operator new and delete to log allocations
void* operator new(std::size_t n) {
    std::cout &amp;lt;&amp;lt; &amp;quot;[Allocating &amp;quot; &amp;lt;&amp;lt; n &amp;lt;&amp;lt; &amp;quot; bytes]&amp;quot;;
    return malloc(n);
}
void operator delete(void* p) throw() {
    free(p);
}

int main() {
    for (size_t i = 0; i &amp;lt; 24; ++i) {
        std::cout &amp;lt;&amp;lt; i &amp;lt;&amp;lt; &amp;quot;: &amp;quot; &amp;lt;&amp;lt; std::string(i, &#39;=&#39;) &amp;lt;&amp;lt; std::endl;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Visual Studio 14&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;0:
1: =
2: ==
3: ===
4: ====
5: =====
6: ======
7: =======
8: ========
9: =========
10: ==========
11: ===========
12: ============
13: =============
14: ==============
15: ===============
[Allocating 32 bytes]16: ================
[Allocating 32 bytes]17: =================
[Allocating 32 bytes]18: ==================
[Allocating 32 bytes]19: ===================
[Allocating 32 bytes]20: ====================
[Allocating 32 bytes]21: =====================
[Allocating 32 bytes]22: ======================
[Allocating 32 bytes]23: =======================
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With a size of 40 bytes (5 pointers), it appears as if there are 16 bytes
dedicated to hold small strings. As we will see below, this is a somewhat
wasteful implementation, and more juice can be squeezed from even smaller
structs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GCC &amp;lt; 5&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;0: 
1: [Allocating 26 bytes]=
2: [Allocating 27 bytes]==
3: [Allocating 28 bytes]===
4: [Allocating 29 bytes]====
5: [Allocating 30 bytes]=====
6: [Allocating 31 bytes]======
7: [Allocating 32 bytes]=======
8: [Allocating 33 bytes]========
9: [Allocating 34 bytes]=========
10: [Allocating 35 bytes]==========
11: [Allocating 36 bytes]===========
12: [Allocating 37 bytes]============
13: [Allocating 38 bytes]=============
14: [Allocating 39 bytes]==============
15: [Allocating 40 bytes]===============
16: [Allocating 41 bytes]================
17: [Allocating 42 bytes]=================
18: [Allocating 43 bytes]==================
19: [Allocating 44 bytes]===================
20: [Allocating 45 bytes]====================
21: [Allocating 46 bytes]=====================
22: [Allocating 47 bytes]======================
23: [Allocating 48 bytes]=======================
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As we will see soon, older libstdc++ implements copy-on-write, and so it makes
sense for them to not utilize small objects optimization.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GCC &amp;gt;= 5&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ g++-5 -std=c++11 main.cpp &amp;amp;&amp;amp; ./a.out
0:
1: =
2: ==
3: ===
4: ====
5: =====
6: ======
7: =======
8: ========
9: =========
10: ==========
11: ===========
12: ============
13: =============
14: ==============
15: ===============
[Allocating 17 bytes]16: ================
[Allocating 18 bytes]17: =================
[Allocating 19 bytes]18: ==================
[Allocating 20 bytes]19: ===================
[Allocating 21 bytes]20: ====================
[Allocating 22 bytes]21: =====================
[Allocating 23 bytes]22: ======================
[Allocating 24 bytes]23: =======================
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Recent GCC versions use a &lt;code&gt;union&lt;/code&gt; of buffer (16 bytes) and capacity (8 bytes) to
store small strings. Since &lt;code&gt;reserve()&lt;/code&gt; is mandatory (more on this later), the
internal pointer to the beginning of the string either points to this &lt;code&gt;union&lt;/code&gt;
or to the dynamically allocated string.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;clang&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;0: 
1: =
2: ==
3: ===
4: ====
5: =====
6: ======
7: =======
8: ========
9: =========
10: ==========
11: ===========
12: ============
13: =============
14: ==============
15: ===============
16: ================
17: =================
18: ==================
19: ===================
20: ====================
21: =====================
22: ======================
23: [Allocating 32 bytes]=======================
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;clang is by-far the smartest and coolest&lt;/strong&gt;. While &lt;code&gt;std::string&lt;/code&gt; has the size
of 24 bytes, it allows strings up to 22 bytes(!!) with no allocation. To achieve
this libc++ uses a neat trick: the size of the string is not saved as-is but
rather in a special way: if the string is short (&amp;lt; 23 bytes) then it stores
&lt;code&gt;size() * 2&lt;/code&gt;. This way the least significant bit is always 0. The long form
always bitwise-ors the LSB with 1, which in theory might have meant
unnecessarily larger allocations, but this implementation always rounds
allocations to be of form &lt;code&gt;16*n - 1&lt;/code&gt; (where &lt;code&gt;n&lt;/code&gt; is an integer). By the way, the
allocated string is actually of form &lt;code&gt;16*n&lt;/code&gt;, the last character being &lt;code&gt;&#39;\0&#39;&lt;/code&gt;. So
freaking cool :).&lt;/p&gt;

&lt;h2 id=&#34;copy-on-write&#34;&gt;Copy on Write&lt;/h2&gt;

&lt;p&gt;In the past it used to be valid (and some might even say encouraged) to
implement &lt;code&gt;std::string&lt;/code&gt; as a copy-on-write (using a reference-count) object.
The Standard had careful wording to allow that first call to non-const methods
(like &lt;code&gt;operator[]&lt;/code&gt;, &lt;code&gt;begin()&lt;/code&gt;, &lt;code&gt;at()&lt;/code&gt;) invalidate existing iterators.&lt;/p&gt;

&lt;p&gt;As multi-threaded computing became more popular, it turned out that
copy-on-write is slower than naive implementations due to necessary locking in
almost every non-&lt;code&gt;const&lt;/code&gt; operation. C++11 changed the wording, making
copy-on-write incompliant.&lt;/p&gt;

&lt;p&gt;GCC &amp;lt; 5 remained incompliant many years after the Standard had changed as they
didn&amp;rsquo;t want to introduce an ABI change on such a critical component. Microsoft&amp;rsquo;s
Visual Studio&amp;rsquo;s stl also dropped copy-on-write from as long as I can remember.&lt;/p&gt;

&lt;p&gt;In the following piece of code I create a &lt;code&gt;std::string&lt;/code&gt; with &lt;code&gt;50&lt;/code&gt; times the
character &lt;code&gt;c&lt;/code&gt;, then change the first character without the &lt;code&gt;std::string&lt;/code&gt; being
aware of it (and of course don&amp;rsquo;t use this in production code!). I use &lt;code&gt;50&lt;/code&gt; to
make sure I don&amp;rsquo;t come across any small-object-optimization.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;string&amp;gt;
#include &amp;lt;iostream&amp;gt;

int main() {
  std::string a(50, &#39;c&#39;);
  std::string b = a;
  
  *const_cast&amp;lt;char*&amp;gt;(a.c_str()) = &#39;A&#39;;
  std::cout &amp;lt;&amp;lt; &amp;quot;a: &amp;quot; &amp;lt;&amp;lt; a &amp;lt;&amp;lt; &amp;quot;\nb: &amp;quot; &amp;lt;&amp;lt; b &amp;lt;&amp;lt; std::endl;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Visual Studio 14&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;a: Accccccccccccccccccccccccccccccccccccccccccccccccc
b: cccccccccccccccccccccccccccccccccccccccccccccccccc
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;GCC &amp;lt; 5&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ g++ -std=c++11 main.cpp &amp;amp;&amp;amp; ./a.out
a: Accccccccccccccccccccccccccccccccccccccccccccccccc
b: Accccccccccccccccccccccccccccccccccccccccccccccccc
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Aha! Copy-on-write in action.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GCC &amp;gt;= 5&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ g++-5 -std=c++11 main.cpp &amp;amp;&amp;amp; ./a.out
a: Accccccccccccccccccccccccccccccccccccccccccccccccc
b: cccccccccccccccccccccccccccccccccccccccccccccccccc
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;clang&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++ -std=c++11 -stdlib=libc++ main.cpp &amp;amp;&amp;amp; ./a.out
a: Accccccccccccccccccccccccccccccccccccccccccccccccc
b: cccccccccccccccccccccccccccccccccccccccccccccccccc
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;allocate-on-reserve&#34;&gt;Allocate on &lt;code&gt;.reserve()&lt;/code&gt;?&lt;/h2&gt;

&lt;p&gt;As you may know, &lt;code&gt;std::string&lt;/code&gt; has a &lt;code&gt;.size()&lt;/code&gt; method returning the size of
the string it contains. This is not necessarily the allocated size, which might
be bigger (but can&amp;rsquo;t be smaller).&lt;/p&gt;

&lt;p&gt;One might wonder if there are any guarantees on &lt;code&gt;capacity()&lt;/code&gt; and &lt;code&gt;reserve()&lt;/code&gt;. In
other words: can we implement a Standard compliant &lt;code&gt;std::string&lt;/code&gt; with 2 members
(pointer and size)? The answer to this question is a straight no. To quote the
Standard:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;After &lt;code&gt;reserve()&lt;/code&gt;, &lt;code&gt;capacity()&lt;/code&gt; is greater or equal to the argument of
reserve.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So calling &lt;code&gt;reserve()&lt;/code&gt; with a number bigger than &lt;code&gt;size()&lt;/code&gt; forces &lt;code&gt;std::string&lt;/code&gt;
to grow. By the way, calling &lt;code&gt;reserve()&lt;/code&gt; with a number &amp;lt;= &lt;code&gt;size()&lt;/code&gt; is similar
to calling &lt;code&gt;shrink_to_fit()&lt;/code&gt;, which is a &lt;strong&gt;non-binding&lt;/strong&gt; request.&lt;/p&gt;

&lt;h2 id=&#34;c-str-and-data-are-the-same&#34;&gt;&lt;code&gt;.c_str()&lt;/code&gt; and &lt;code&gt;.data()&lt;/code&gt; are the same&lt;/h2&gt;

&lt;p&gt;Calling &lt;code&gt;.data()&lt;/code&gt; in C++98 would not necessarily return a null-terminated
string. C++11 changes this and now both &lt;code&gt;data()&lt;/code&gt; and &lt;code&gt;c_str()&lt;/code&gt; return a string
that must terminate with a &lt;code&gt;&#39;\0&#39;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Another thing C++11 brings is the ability to dereference the pointer returned
from &lt;code&gt;c_str()&lt;/code&gt;/&lt;code&gt;data()&lt;/code&gt; even if &lt;code&gt;empty() == true&lt;/code&gt;. No longer is it undefined to
do &lt;code&gt;for (const char* c = s.data(); *c != 0; ++c) ...&lt;/code&gt;, as now &lt;code&gt;c_str()&lt;/code&gt;/&lt;code&gt;data()&lt;/code&gt;
will return a pointer to &lt;code&gt;&#39;\0&#39;&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&#34;binary-data&#34;&gt;Binary data&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;std::string&lt;/code&gt; can hold binary data. That is, a string with &lt;code&gt;&#39;\0&#39;&lt;/code&gt; in any
position. Note that it&amp;rsquo;s not entirely trivial to &lt;em&gt;create&lt;/em&gt; such a string. For
example, the following will create a string with the content &lt;code&gt;&amp;quot;hello&amp;quot;&lt;/code&gt;
by-design, as we&amp;rsquo;re passing in a C-string:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;std::string s = &amp;quot;hello\0world&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Even doing something like the following will fail for the exact same reason:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;auto cstr = &amp;quot;hello\0world&amp;quot;;
std::string s(cstr, strlen(cstr));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The following, however, &lt;em&gt;will&lt;/em&gt; work:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;// We could use const char[] or decltype(auto) here, but not auto. Why?
// Because auto will be decayed to const char*, which has the wrong size.
decltype(auto) cstr = &amp;quot;Hello\0World!&amp;quot;;
std::string s(cstr, sizeof(cstr));
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Also note that printing such strings may also be problematic, as some
implementations stop at &lt;code&gt;&#39;\0&#39;&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&#34;growth-strategy&#34;&gt;Growth strategy&lt;/h2&gt;

&lt;p&gt;Like &lt;code&gt;std::vector&lt;/code&gt;, it&amp;rsquo;s important for &lt;code&gt;std::string&lt;/code&gt; to grow in an efficient
way. Let&amp;rsquo;s examine the following:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;cstdlib&amp;gt;
#include &amp;lt;iostream&amp;gt;
#include &amp;lt;string&amp;gt;

// replace operator new and delete to log allocations
void* operator new(std::size_t n) {
    std::cout &amp;lt;&amp;lt; &amp;quot;Allocating &amp;quot; &amp;lt;&amp;lt; n &amp;lt;&amp;lt; &amp;quot; bytes&amp;quot; &amp;lt;&amp;lt; std::endl;
    return malloc(n);
}
void operator delete(void* p) throw() {
    free(p);
}

int main() {
    std::string s;
    for (size_t i = 0; i != 1000000; ++i) {
        s += &#39;.&#39;;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Visual Studio 14&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;Allocating 32 bytes
Allocating 48 bytes
Allocating 71 bytes
Allocating 106 bytes
Allocating 158 bytes
Allocating 236 bytes
Allocating 353 bytes
Allocating 529 bytes
Allocating 793 bytes
Allocating 1189 bytes
Allocating 1783 bytes
Allocating 2674 bytes
Allocating 4010 bytes
Allocating 6053 bytes
Allocating 9059 bytes
Allocating 13568 bytes
Allocating 20332 bytes
Allocating 30478 bytes
Allocating 45697 bytes
Allocating 68525 bytes
Allocating 102767 bytes
Allocating 154130 bytes
Allocating 231175 bytes
Allocating 346742 bytes
Allocating 520093 bytes
Allocating 780119 bytes
Allocating 1170158 bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Growth strategy is 1.5x.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GCC &amp;lt; 5&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ g++ -std=c++11 main.cpp &amp;amp;&amp;amp; ./a.out
Allocating 26 bytes
Allocating 27 bytes
Allocating 29 bytes
Allocating 33 bytes
Allocating 41 bytes
Allocating 57 bytes
Allocating 89 bytes
Allocating 153 bytes
Allocating 281 bytes
Allocating 537 bytes
Allocating 1049 bytes
Allocating 2073 bytes
Allocating 8160 bytes
Allocating 16352 bytes
Allocating 32736 bytes
Allocating 65504 bytes
Allocating 131040 bytes
Allocating 262112 bytes
Allocating 524256 bytes
Allocating 1048544 bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Growth strategy is a bit tricky. On my machine the page size is 4096, and so
for strings larger than that it rounds up to fit to a page and grows 2x. For
small strings, growth is done by &lt;strong&gt;adding&lt;/strong&gt; multiples of 2 (thanks Marek!).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GCC &amp;gt;= 5&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ g++-5 -std=c++11 main.cpp &amp;amp;&amp;amp; ./a.out
Allocating 31 bytes
Allocating 61 bytes
Allocating 121 bytes
Allocating 241 bytes
Allocating 481 bytes
Allocating 961 bytes
Allocating 1921 bytes
Allocating 3841 bytes
Allocating 7681 bytes
Allocating 15361 bytes
Allocating 30721 bytes
Allocating 61441 bytes
Allocating 122881 bytes
Allocating 245761 bytes
Allocating 491521 bytes
Allocating 983041 bytes
Allocating 1966081 bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Growth strategy is 2x.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;clang&lt;/strong&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;$ clang++ -std=c++11 -stdlib=libc++ main.cpp &amp;amp;&amp;amp; ./a.out
Allocating 48 bytes
Allocating 96 bytes
Allocating 192 bytes
Allocating 384 bytes
Allocating 768 bytes
Allocating 1536 bytes
Allocating 3072 bytes
Allocating 6144 bytes
Allocating 12288 bytes
Allocating 24576 bytes
Allocating 49152 bytes
Allocating 98304 bytes
Allocating 196608 bytes
Allocating 393216 bytes
Allocating 786432 bytes
Allocating 1572864 bytes
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Growth strategy is 2x.&lt;/p&gt;

&lt;h2 id=&#34;move-semantics&#34;&gt;Move semantics&lt;/h2&gt;

&lt;p&gt;Taking advantage of C++11&amp;rsquo;s move semantics will benefit every library. Apache&amp;rsquo;s
retired stdcxx implementation is the only one I know of to not support them.&lt;/p&gt;

&lt;p&gt;One interesting this to note is that with small objects, where Small Object
Optimization exists, a move constructor is implemented as a copy constructor.
This of course is purely semantic, as the data structure&amp;rsquo;s fields need to be
copied anyway, but is nevertheless interesting to think about.&lt;/p&gt;

&lt;h2 id=&#34;memory&#34;&gt;Memory&lt;/h2&gt;

&lt;p&gt;The Standard instructs implementations to use contiguous memory for the entire
string, which is convenient when working with some C-APIs.&lt;/p&gt;

&lt;h2 id=&#34;summary&#34;&gt;Summary&lt;/h2&gt;

&lt;p&gt;C++&amp;rsquo;s built-in &lt;code&gt;std::string&lt;/code&gt; is a well-designed class with some great
implementations. Today we looked at some important features and hopefully
learned a thing or two. Please let me know if there are more things you&amp;rsquo;re
interested in, either via a comment below or through the &amp;lsquo;About&amp;rsquo; page.&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Template SFINAE &amp; type-traits</title>
      <link>https://shaharmike.com/cpp/sfinae/</link>
      <pubDate>Sat, 21 May 2016 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/sfinae/</guid>
      <description>&lt;p&gt;Today&amp;rsquo;s post is about template SFINAE &amp;amp; type-traits - cool C++ features with
great compile-time power.&lt;/p&gt;

&lt;h2 id=&#34;template-sfinae&#34;&gt;Template SFINAE&lt;/h2&gt;

&lt;p&gt;SFINAE is a scary-looking C++ acronym, which joins a long list of
hard-to-remember capital-letter concepts (such as RAII, RVO, RTTI, PIMPL, etc).&lt;/p&gt;

&lt;p&gt;SFINAE stands for &amp;ldquo;Substitution Failure In Not An Error&amp;rdquo;. Simply put, it means
that when a compiler fails to substitute a template parameter it should continue
looking instead of giving up.&lt;/p&gt;

&lt;p&gt;Here’s a quick example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
using namespace std;

template &amp;lt;typename T&amp;gt; void foo(typename T::type) { cout &amp;lt;&amp;lt; &amp;quot;1st&amp;quot; &amp;lt;&amp;lt; endl; }
template &amp;lt;typename T&amp;gt; void foo(T) { cout &amp;lt;&amp;lt; &amp;quot;2nd&amp;quot; &amp;lt;&amp;lt; endl; }

struct MyStruct {
    using type = int;
};

int main() {
    foo&amp;lt;MyStruct&amp;gt;(2);  // ok - calls first version
    foo&amp;lt;int&amp;gt;(2);       // also ok - calls second version
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;1st
2nd
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One important thing to note is that SFINAE, like its name suggests, only happens
during substitution. Essentially it means that, like above, providing an error
during function matching (aka overload resolution) is OK and the compiler will
continue on its journey to find the proper function. However, failure inside a
function&amp;rsquo;s body will yield an unrecoverable compiler error, afterwhich the
compiler will not continue to search for other functions.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s an example. If you don&amp;rsquo;t know what forward references are, you can safely
ignore the second version of &lt;code&gt;zoo()&lt;/code&gt;; All you need to know is that both
functions could match on any parameter passed.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;template &amp;lt;typename Container&amp;gt;
void zoo(const Container&amp;amp; container) {  // 1st version
    auto it = container.begin();
}

template &amp;lt;typename NonContainer&amp;gt;
void zoo(NonContainer&amp;amp;&amp;amp; non_container) {}  // 2nd version

int main() {
    std::less&amp;lt;int&amp;gt; l;

    // take a const-reference to ensure 1st version is called; If we used &#39;l&#39;
    // the 2nd version would be preferred with NonContainer == std::less&amp;lt;int&amp;gt;&amp;amp;
    const std::less&amp;lt;int&amp;gt;&amp;amp; r = l;

    zoo(r);  // ERROR: no member named &#39;begin&#39; in &#39;std::less&amp;lt;int&amp;gt;&#39;;
             // Compiler will *not* attempt to use the 2nd version of zoo
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;type-traits&#34;&gt;Type Traits&lt;/h2&gt;

&lt;p&gt;SFINAE has already been in C++98. C++11 introduces a new feature, not directly
related but often used together: template-partial-specialization (which might
have its own post at a later point). By combining these 2 features together one
can create some powerful tricks.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s a simple implementation of &lt;code&gt;std::enable_if&lt;/code&gt; (found in &lt;code&gt;&amp;lt;type_traits&amp;gt;&lt;/code&gt;):&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;// this is an actual complete implementation of std::enable_if found in std
// header &amp;lt;type_traits&amp;gt;
template &amp;lt;bool Condition, typename T = void&amp;gt;
struct enable_if {
    // No &#39;type&#39; here, so any attempt to use it will fail substitution
};

// partial specialization for when Condition==true
template &amp;lt;typename T&amp;gt;
struct enable_if&amp;lt;true, T&amp;gt; {
    using type = T;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This simple-but-powerful &lt;code&gt;struct&lt;/code&gt; allows us to set compile-time conditions on
our functions and classes. The way one uses &lt;code&gt;enable_if&lt;/code&gt; is by specifying a
condition as the first template-parameter, and a type &lt;code&gt;T&lt;/code&gt; (optional) that will
be used if the condition is &lt;code&gt;true&lt;/code&gt;. Take a minute to think about it as it&amp;rsquo;s not
trivial.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s a simple example of a function &lt;code&gt;bar()&lt;/code&gt; which only accepts arguments that
are &lt;code&gt;enum&lt;/code&gt;s. Attempting to pass non-&lt;code&gt;enum&lt;/code&gt; will fail compilation:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;// T must be an enum type.
// Second template argument is only used to enforce T&#39;s type, therefore it
// doesn&#39;t have a name and it is not used.
template &amp;lt;typename T,
          typename = typename enable_if&amp;lt;std::is_enum&amp;lt;T&amp;gt;::value, void&amp;gt;::type&amp;gt;
void bar(T t) {}

enum Enum1 { A, B };
enum class Enum2 { C, D };

int main() {
    bar(A);
    bar(Enum2::C);
    bar(1); // compile error - &amp;quot;no matching function for call to &#39;bar(int)&#39;&amp;quot;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Another way to achieve basically the same thing is as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;// If T is enum - return value is void, otherwise - substitution failure.
template &amp;lt;typename T&amp;gt;
typename std::enable_if&amp;lt;std::is_enum&amp;lt;T&amp;gt;::value, void&amp;gt;::type bar2(T t) {}

// Rest unchanged.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here&amp;rsquo;s a summary of the differences between &lt;code&gt;bar()&lt;/code&gt; and &lt;code&gt;bar2()&lt;/code&gt;:&lt;/p&gt;

&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;bar()&lt;/th&gt;
&lt;th&gt;bar2()&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2 template arguments&lt;/td&gt;
&lt;td&gt;1 template argument&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Simple &lt;code&gt;void&lt;/code&gt; return value&lt;/td&gt;
&lt;td&gt;Conditional, harder to understand return-value&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Bypassable &amp;ldquo;security&amp;rdquo; (by specifying 2nd template argument)&lt;/td&gt;
&lt;td&gt;Unbreakable&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Requires additional template parameter&lt;/td&gt;
&lt;td&gt;No additional parameters&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Please note that the above &lt;code&gt;bar*()&lt;/code&gt;s could have been implemented using
&lt;code&gt;static_assert()&lt;/code&gt; instead of &lt;code&gt;enable_if&lt;/code&gt;. Choose the right tool for the job.
Here&amp;rsquo;s a quick comparison:&lt;/p&gt;

&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;code&gt;enable_if&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;static_assert()&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Allows complicated overload rules without template specialization&lt;/td&gt;
&lt;td&gt;Forces compilation to fail when a criteria was not met&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Takes place during function matching&lt;/td&gt;
&lt;td&gt;Only takes place &lt;strong&gt;after&lt;/strong&gt; overload resolution, when a function has been selected&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;Long, harder to parse compilation errors&lt;/td&gt;
&lt;td&gt;Compile error is user-defined (2nd parameter to &lt;code&gt;static_assert()&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;For more awesome type traits check out the standard C++ header &lt;a href=&#34;http://en.cppreference.com/w/cpp/header/type_traits&#34;&gt;&lt;code&gt;&amp;lt;type_traits&amp;gt;&lt;/code&gt;&lt;/a&gt;
- it’s full of goodies you might find useful. Some traits are even impossible to
implement in C++, and require special compiler support (like
&lt;code&gt;has_virtual_destructor&lt;/code&gt;).&lt;/p&gt;

&lt;h2 id=&#34;try-it-out&#34;&gt;Try it out!&lt;/h2&gt;

&lt;p&gt;5 Minute Practice (use &lt;a href=&#34;http://cpp.sh&#34;&gt;cpp.sh&lt;/a&gt; if you don&amp;rsquo;t have a compiler
handy - it&amp;rsquo;s an online C++ compiler)&lt;/p&gt;

&lt;p&gt;As a practice, try to implement the following classes (they all should define
&lt;code&gt;static constexpr bool value&lt;/code&gt; as &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;, according to their names:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;http://en.cppreference.com/w/cpp/types/is_pointer&#34;&gt;&lt;code&gt;std::is_pointer&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt; -
&lt;code&gt;value&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; if &lt;code&gt;T&lt;/code&gt; is a pointer, &lt;code&gt;false&lt;/code&gt; otherwise&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://en.cppreference.com/w/cpp/types/is_const&#34;&gt;&lt;code&gt;std::is_const&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt; -
&lt;code&gt;value&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; if &lt;code&gt;T&lt;/code&gt; has &lt;code&gt;const&lt;/code&gt; qualifier, &lt;code&gt;false&lt;/code&gt; otherwise&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://en.cppreference.com/w/cpp/types/is_void&#34;&gt;&lt;code&gt;std::is_void&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt; -
&lt;code&gt;value&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; if &lt;code&gt;T&lt;/code&gt; is &lt;code&gt;void&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt; otherwise&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;http://en.cppreference.com/w/cpp/types/is_same&#34;&gt;&lt;code&gt;std::is_same&amp;lt;T, U&amp;gt;&lt;/code&gt;&lt;/a&gt; -
&lt;code&gt;value&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; if &lt;code&gt;T&lt;/code&gt; is of the same type as &lt;code&gt;U&lt;/code&gt;, &lt;code&gt;false&lt;/code&gt; otherwise&lt;/li&gt;
&lt;/ul&gt;</description>
    </item>
    
    <item>
      <title>UserData class</title>
      <link>https://shaharmike.com/cpp/user-data/</link>
      <pubDate>Tue, 26 Apr 2016 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/user-data/</guid>
      <description>&lt;p&gt;Many libraries provide their users with a way to add user-defined content to the library&amp;rsquo;s objects. This is especially true for libraries in the graphics domain. Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Win32&amp;rsquo;s &lt;a href=&#34;https://msdn.microsoft.com/en-us/library/windows/desktop/ms644898(v=vs.85).aspx&#34;&gt;&lt;code&gt;SetWindowLongPtr()&lt;/code&gt;&lt;/a&gt;: allows setting &lt;code&gt;void*&lt;/code&gt; on an &lt;code&gt;HWND&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;OGRE3d&amp;rsquo;s &lt;a href=&#34;http://www.ogre3d.org/docs/api/1.9/_ogre_user_object_bindings_8h_source.html&#34;&gt;&lt;code&gt;UserObjectBindings&lt;/code&gt;&lt;/a&gt;: allows setting &lt;code&gt;Any&lt;/code&gt; (similar to &lt;code&gt;boost::any&lt;/code&gt;) and also map strings to &lt;code&gt;Any&lt;/code&gt; on various objects;&lt;/li&gt;
&lt;li&gt;Cocos2d-x&amp;rsquo;s &lt;a href=&#34;http://www.cocos2d-x.org/reference/native-cpp/V3.0alpha0/d3/d82/classcocos2d_1_1_node.html#a38a0a018529a356b1a20ccc5d3508a1a&#34;&gt;&lt;code&gt;get/setUserData()&lt;/code&gt;&lt;/a&gt; and &lt;a href=&#34;http://www.cocos2d-x.org/reference/native-cpp/V3.0alpha0/d3/d82/classcocos2d_1_1_node.html#af75b22a41ff8bb0287d7dbcdc9a26a31&#34;&gt;&lt;code&gt;get/setUserObject()&lt;/code&gt;&lt;/a&gt;: allows setting &lt;code&gt;void*&lt;/code&gt; or inherit from &lt;code&gt;Object&lt;/code&gt; and store it in various objects;&lt;/li&gt;
&lt;li&gt;Box2d&amp;rsquo;s &lt;code&gt;Get/SetUserData()&lt;/code&gt;: allows saving a &lt;code&gt;void*&lt;/code&gt; on various physics objects in a world;&lt;/li&gt;
&lt;li&gt;And so on&amp;hellip;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You get the point. Sometimes libraries help you add context to their objects, but they don&amp;rsquo;t know your classes (nor should they). They are stuck with suboptimal solutions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;void*&lt;/code&gt; - type-unsafe, can&amp;rsquo;t be &lt;code&gt;delete&lt;/code&gt;d by the library, only allows 1 object;&lt;/li&gt;
&lt;li&gt;mapping a string to a &lt;code&gt;void*&lt;/code&gt; - type-unsafe, strings are typo-prone, can&amp;rsquo;t be &lt;code&gt;delete&lt;/code&gt;d by the library;&lt;/li&gt;
&lt;li&gt;Inheriting from an &lt;code&gt;Object&lt;/code&gt;-like class - downcast is type-unsafe, allows only 1 object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are many variations of these solutions, but I have yet to encounter one I like. So I&amp;rsquo;d like to propose one.&lt;/p&gt;

&lt;h2 id=&#34;userdata-class&#34;&gt;UserData class&lt;/h2&gt;

&lt;p&gt;Let&amp;rsquo;s start with an API:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;class UserData {
public:
    template &amp;lt;class T&amp;gt;
    void Set(const T&amp;amp; t);

    template &amp;lt;class T&amp;gt;
    T&amp;amp; Get() const;

    template &amp;lt;class T&amp;gt;
    bool Has() const;

    template &amp;lt;class T&amp;gt;
    void Clear();
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This allows users to store and retrieve their very own classes (plural!), in a type-safe manner. It is also not a templated class (though it obviously has templated methods), so it has a fixed size no matter what you store in it. I think this is a very neat API. In my particular scenario I don&amp;rsquo;t care about thread-safety, which simplifies things a bit.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Now take a moment to think about how you would implement this&lt;/em&gt;. It is non-trivial. If you don&amp;rsquo;t have a compiler at hand, try &lt;a href=&#34;https://ideone.com/&#34;&gt;Ideone.com&lt;/a&gt; or &lt;a href=&#34;cpp.sh&#34;&gt;cpp.sh&lt;/a&gt;. Come back with an answer!&lt;/p&gt;

&lt;p&gt;You&amp;rsquo;re back? Awesome. Let&amp;rsquo;s explore a few possible solutions for this interesting problem.&lt;/p&gt;

&lt;h2 id=&#34;static-based-solution&#34;&gt;&lt;code&gt;static&lt;/code&gt;-based solution&lt;/h2&gt;

&lt;p&gt;This is the first solution I came up with, and it is far from perfect. It is interesting nontheless, and so I decided to put it here despite how embarrassing it is.&lt;/p&gt;

&lt;p&gt;The idea here it to have a static template member-function (called it &lt;code&gt;Multitool()&lt;/code&gt;). This function has a &lt;code&gt;static unordered_map&amp;lt;int, T&amp;gt;&lt;/code&gt; (defined inside the function) which is used as storage for &lt;code&gt;T&lt;/code&gt;s. Each &lt;code&gt;UserData&lt;/code&gt; instance has a unique &lt;code&gt;int&lt;/code&gt; id (which is the key in the above map). This function takes care of storing and retrieving &lt;code&gt;T&lt;/code&gt;s, and is called by the templated-public API methods.&lt;/p&gt;

&lt;p&gt;This is one ugly solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Storage for &lt;code&gt;T&lt;/code&gt;s is static, so there&amp;rsquo;s some trickery to release memory when the &lt;code&gt;UserData&lt;/code&gt; object is released (see &lt;code&gt;m_Clear&lt;/code&gt; below);&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Multitool()&lt;/code&gt; has an unsafe &amp;amp; ugly API (although it&amp;rsquo;s private so it&amp;rsquo;s less horrible);&lt;/li&gt;
&lt;li&gt;We need to assign a unique id to each &lt;code&gt;UserData&lt;/code&gt; instance. Again, not horrible, but it would be nice to avoid.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here&amp;rsquo;s a complete implementation:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;class UserData final {
public:
    UserData();
    ~UserData();

    template &amp;lt;class T&amp;gt;
    bool Has() const {
        void* vt = nullptr;
        Multitool&amp;lt;T&amp;gt;(MultitoolCommand::Get, &amp;amp;vt, m_UniqueId);
        T* t = (T*)vt;
        return (t != nullptr);
    }

    template &amp;lt;class T&amp;gt;
    T&amp;amp; Get() const {
        void* vt = nullptr;
        Multitool&amp;lt;T&amp;gt;(MultitoolCommand::Get, &amp;amp;vt, m_UniqueId);
        T* t = (T*)vt;
        assert(t != nullptr);

        m_Clear.insert(Multitool&amp;lt;T&amp;gt;);

        return *t;
    }

    template &amp;lt;class T&amp;gt;
    void Set(const T&amp;amp; t) {
        T* tmp = &amp;amp;t;
        void* vt = (void*)tmp;
        Multitool&amp;lt;T&amp;gt;(MultitoolCommand::Set, &amp;amp;vt, m_UniqueId);

        m_Clear.insert(Multitool&amp;lt;T&amp;gt;);
    }

    template &amp;lt;class T&amp;gt;
    void Clear() {
        void* tmp = nullptr;
        Multitool&amp;lt;T&amp;gt;(MultitoolCommand::Clear, &amp;amp;tmp, m_UniqueId);

        m_Clear.erase(Multitool&amp;lt;T&amp;gt;);
    }

private:
    enum class MultitoolCommand {
        Get,
        Set,
        Clear,
    };

    // This function is weird because it is the single point of
    // storage for Ts, and so has to satisfy all public operations
    template &amp;lt;class T&amp;gt;
    static void Multitool(MultitoolCommand command, void** vt, int id) {
        static std::unordered_map&amp;lt;int, T&amp;gt; store;
        T*&amp;amp; t = *((T**)(vt));

        switch (command) {
        case MultitoolCommand::Get: {
                auto it = store.find(id);
                if (it == store.end()) {
                    t = nullptr;
                } else {
                    t = &amp;amp;(it-&amp;gt;second);
                }
            }
            break;
        case MultitoolCommand::Set: {
                auto it = store.find(id);
                if (it == store.end()) {
                    store.emplace(id, *t);
                } else {
                    it-&amp;gt;second = *t;
                }
            }
            break;
        case MultitoolCommand::Clear:
            store.erase(id);
            break;
        }
    }

    static int m_NextUniqueId;
    int const m_UniqueId;

    typedef void (*TClearFunc)(MultitoolCommand, void**, int);
    mutable std::set&amp;lt;TClearFunc&amp;gt; m_Clear;
};

// .cpp file:
int UserData::m_NextUniqueId = 0;

UserData::UserData()
:    m_UniqueId(m_NextUniqueId++) {
}

UserData::~UserData() {
    void* tmp = nullptr;
    for (auto it : m_Clear) {
        it(MultitoolCommand::Clear, &amp;amp;tmp, m_UniqueId);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;type-index-based-solution&#34;&gt;&lt;code&gt;type_index&lt;/code&gt;-based solution&lt;/h2&gt;

&lt;p&gt;In &amp;gt;= C++11 we can use &lt;a href=&#34;http://en.cppreference.com/w/cpp/types/type_index&#34;&gt;&lt;code&gt;type_index&lt;/code&gt;&lt;/a&gt; to store types as keys in associative containers (in this case an &lt;code&gt;unordered_map&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;We need some small trickery to call &lt;code&gt;T&lt;/code&gt;&amp;rsquo;s correct destructor. We also require RTTI support (although we don&amp;rsquo;t use the &amp;lsquo;runtime&amp;rsquo; part of it - we just need the tables to exist in the binary).&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s the complete solution:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;class UserData final {
public:
    UserData() = default;
    ~UserData() = default;

    template &amp;lt;class T&amp;gt;
    bool Has() const {
        auto const&amp;amp; it = m_Items.find(typeid(T));
        return (it != m_Items.end() &amp;amp;&amp;amp; it-&amp;gt;second.get() != nullptr);
    }

    template &amp;lt;class T&amp;gt;
    T&amp;amp; Get() const {
        auto const&amp;amp; it = m_Items.find(typeid(T));
        assert(it != m_Items.end());
        assert(it-&amp;gt;second.get() != nullptr);
        return static_cast&amp;lt;Wrapper&amp;lt;T&amp;gt;*&amp;gt;(it-&amp;gt;second.get())-&amp;gt;t;
    }

    // It may have been better to take a pointer to T instead. Up to you.
    template &amp;lt;class T&amp;gt;
    void Set(const T&amp;amp; t) {
        m_Items[typeid(T)] = std::make_unique&amp;lt;Wrapper&amp;lt;T&amp;gt;&amp;gt;(t);
    }

    template &amp;lt;class T&amp;gt;
    void Clear() {
        m_Items.erase(typeid(T));
    }

private:
    class EmptyBase {
    public:
        virtual ~EmptyBase() = default;
    };

    template &amp;lt;class T&amp;gt;
    class Wrapper : public EmptyBase {
    public:
        Wrapper() = default;
        Wrapper(T t_) : t(t_) {}

        T t;
    };

    std::unordered_map&amp;lt;std::type_index, std::unique_ptr&amp;lt;EmptyBase&amp;gt;&amp;gt; m_Items;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;custom-type-id-solution&#34;&gt;Custom type-id solution&lt;/h2&gt;

&lt;p&gt;I have not yet encountered a team who is asking the compiler to &lt;em&gt;not&lt;/em&gt; generate RTTI tables. I&amp;rsquo;m sure that there are such teams out there. But even if you have RTTI - it&amp;rsquo;s much slower than, say, a simple integer.&lt;/p&gt;

&lt;p&gt;But how can we assign a unique integer per-type? Using a simple templates trick:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;size_t type_id = 0;

template &amp;lt;typename T&amp;gt;
size_t GetTypeId() {
    static size_t t_id = type_id++; // use std::atomic for thread safety
    return t_id;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This even allows us to have a contiguous &lt;code&gt;std::vector&lt;/code&gt; (as long as we never shrink it). With a solution similar to &lt;code&gt;EmptyBase&lt;/code&gt; and &lt;code&gt;Wrapper&lt;/code&gt; above we can get better performance, better CPU caching and less memory usage. Unfortunately, we still have to use a &lt;code&gt;vector&lt;/code&gt; of pointers rather than have the objects directly in it, as we don&amp;rsquo;t know their sizes up front.&lt;/p&gt;

&lt;p&gt;Complete solution:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;class UserData {
public:
    template &amp;lt;class T&amp;gt;
    void Set(const T&amp;amp; t) {
        auto id = GetTypeId&amp;lt;T&amp;gt;();
        assert(m_Items.size() &amp;gt;= id);
        if (id &amp;lt;= m_Items.size()) {
            m_Items.resize(id+1);
        }
        m_Items[id] = std::make_unique&amp;lt;Wrapper&amp;lt;T&amp;gt;&amp;gt;(t);
    }

    template &amp;lt;class T&amp;gt;
    T&amp;amp; Get() const {
        auto id = GetTypeId&amp;lt;T&amp;gt;();
        assert(m_Items.size() &amp;gt; id);
        auto const&amp;amp; it = m_Items[id];
        assert(it);
        return static_cast&amp;lt;Wrapper&amp;lt;T&amp;gt;&amp;amp;&amp;gt;(*it.get()).t;
    }

    template &amp;lt;class T&amp;gt;
    bool Has() const {
        auto id = GetTypeId&amp;lt;T&amp;gt;();
        return m_Items.size() &amp;gt; id &amp;amp;&amp;amp; m_Items[id];
    }

    template &amp;lt;class T&amp;gt;
    void Clear() {
        auto id = GetTypeId&amp;lt;T&amp;gt;();
        assert(m_Items.size() &amp;gt; id);
        m_Items[id].reset();
    }

private:
    class EmptyBase {
    public:
        virtual ~EmptyBase() = default;
    };

    template &amp;lt;class T&amp;gt;
    class Wrapper : public EmptyBase {
    public:
        Wrapper() = default;
        Wrapper(T t_) : t(t_) {}

        T t;
    };

    std::vector&amp;lt;std::unique_ptr&amp;lt;EmptyBase&amp;gt;&amp;gt; m_Items;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you have better ideas I am happy to hear! Let me know what you think in the comments below.&lt;/p&gt;</description>
    </item>
    
    <item>
      <title>Naive std::function implementation</title>
      <link>https://shaharmike.com/cpp/naive-std-function/</link>
      <pubDate>Fri, 08 Apr 2016 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/naive-std-function/</guid>
      <description>&lt;p&gt;After &lt;a href=&#34;https://shaharmike.com/cpp/lambdas-and-functions/&#34;&gt;exploring &lt;code&gt;std::function&lt;/code&gt;&lt;/a&gt; in a previous post, I thought that it might be a good practice to implement a simple (and partial) &lt;code&gt;std::function&lt;/code&gt;. It turned out to be much less code than I anticipated. I hope you&amp;rsquo;ll like it.&lt;/p&gt;

&lt;h2 id=&#34;features&#34;&gt;Features&lt;/h2&gt;

&lt;p&gt;While &lt;a href=&#34;http://en.cppreference.com/w/cpp/utility/functional/function&#34;&gt;&lt;code&gt;std::function&lt;/code&gt; has a few &lt;code&gt;typedef&lt;/code&gt;s and methods&lt;/a&gt;, the core functionality is assignment and invocation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Assignment through &lt;code&gt;operator=&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Invocation through &lt;code&gt;operator()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even though &lt;code&gt;std::function&lt;/code&gt; has a bit more functionality to it, we will only implement the above 2 methods.&lt;/p&gt;

&lt;h2 id=&#34;declaration&#34;&gt;Declaration&lt;/h2&gt;

&lt;p&gt;The standard specifies that &lt;code&gt;std::function&lt;/code&gt; will be declared as follows:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;namespace std {
	template &amp;lt;typename&amp;gt;
	class function; // no definition

	template &amp;lt;typename ReturnValue, typename ... Args&amp;gt;
	class function&amp;lt;ReturnValue(Args...)&amp;gt; {
		// ...
	};
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Why have a &lt;code&gt;std::function&lt;/code&gt; with no definition that is never used? Well, ideally you&amp;rsquo;d only have the second version. However, that version is a &lt;em&gt;partial template specialization&lt;/em&gt; of the first. Another way could have been to define &lt;code&gt;std::function&lt;/code&gt; as:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;template &amp;lt;typename ReturnValue, typename ... Args&amp;gt;
class function { ... };
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;But this would mean that clients would look like &lt;code&gt;std::function&amp;lt;int, bool, float&amp;gt;&lt;/code&gt; rather than &lt;code&gt;std::function&amp;lt;int(bool, float)&amp;gt;&lt;/code&gt;. I personally think that the latter is much nicer, but there&amp;rsquo;s just no syntax to express this without partial specialization.&lt;/p&gt;

&lt;p&gt;So let&amp;rsquo;s copy that:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;template &amp;lt;typename&amp;gt;
class naive_function; // no definition

template &amp;lt;typename ReturnValue, typename ... Args&amp;gt;
class naive_function&amp;lt;ReturnValue(Args...)&amp;gt; {
public:
	// operator= goes here
	// operator() goes here
private:
	...
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now any attempt to (mis)use &lt;code&gt;naive_function&lt;/code&gt; with a simple argument list (example: &lt;code&gt;naive_function&amp;lt;bool, int&amp;gt;&lt;/code&gt;) will yield a compiler error along the lines of &amp;ldquo;using undefined class naive_function&amp;rdquo;.&lt;/p&gt;

&lt;h2 id=&#34;groundwork&#34;&gt;Groundwork&lt;/h2&gt;

&lt;p&gt;Before we move to implement &lt;code&gt;operator=&lt;/code&gt; and &lt;code&gt;operator()&lt;/code&gt; we need to write some supporting code. The following classes will be internal &lt;code&gt;private&lt;/code&gt; to &lt;code&gt;naive_function&lt;/code&gt;, so they know &lt;code&gt;ReturnValue&lt;/code&gt; and &lt;code&gt;Args&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s start with an interface:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;class ICallable {
public:
	virtual ~ICallable() = default;
	virtual ReturnValue Invoke(Args...) = 0;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Easy enough. Now for a concrete implementor:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;template &amp;lt;typename T&amp;gt;
class CallableT : public ICallable {
public:
	CallableT(const T&amp;amp; t)
		: t_(t) {
	}

	~CallableT() override = default;

	ReturnValue Invoke(Args... args) override {
		return t_(args...);
	}

private:
	T t_;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;implementation&#34;&gt;Implementation&lt;/h2&gt;

&lt;p&gt;With the help of the above very simple classes it is now almost trivial to implement &lt;code&gt;naive_function&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;template &amp;lt;typename ReturnValue, typename... Args&amp;gt;
class naive_function&amp;lt;ReturnValue(Args...)&amp;gt; {
public:
	template &amp;lt;typename T&amp;gt;
	naive_function&amp;amp; operator=(T t) {
		callable_ = std::make_unique&amp;lt;CallableT&amp;lt;T&amp;gt;&amp;gt;(t);
		return *this;
	}

	ReturnValue operator()(Args... args) const {
		assert(callable_);
		return callable_-&amp;gt;Invoke(args...);
	}

private:
	// ICallable as implemented above.
	// CallableT as implemented above.

	std::unique_ptr&amp;lt;ICallable&amp;gt; callable_;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;There&amp;rsquo;s not even a lot of magic here:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;operator=&lt;/code&gt; is templated, where T is anything that can be called with &lt;code&gt;Args...&lt;/code&gt; and return &lt;code&gt;ReturnValue&lt;/code&gt;. There we dynamically create a &lt;code&gt;CallableT&amp;lt;T&amp;gt;&lt;/code&gt; which is assigned to &lt;code&gt;callable_&lt;/code&gt; (of type &lt;code&gt;std::unique_ptr&amp;lt;ICallable&amp;gt;&lt;/code&gt;). Now the &lt;a href=&#34;https://shaharmike.com/cpp/vtable-part1/&#34;&gt;vtable&lt;/a&gt; knows how to execute the proper code at runtime.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;operator()&lt;/code&gt; is trivial. It is not allowed to be called before &lt;code&gt;operator=&lt;/code&gt; was called, thus the &lt;code&gt;assert&lt;/code&gt;. After that, simply &lt;code&gt;Invoke&lt;/code&gt; &lt;code&gt;callable_&lt;/code&gt; and return its return-value.&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s test our creation:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;void func() {
	cout &amp;lt;&amp;lt; &amp;quot;func&amp;quot; &amp;lt;&amp;lt; endl;
}

struct functor {
	void operator()() {
		cout &amp;lt;&amp;lt; &amp;quot;functor&amp;quot; &amp;lt;&amp;lt; endl;
	}
};

int main() {
	naive_function&amp;lt;void()&amp;gt; f;
	f = func;
	f();
	f = functor();
	f();
	f = []() { cout &amp;lt;&amp;lt; &amp;quot;lambda&amp;quot; &amp;lt;&amp;lt; endl; };
	f();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Output:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;func
functor
lambda
&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&#34;future-improvements&#34;&gt;Future improvements&lt;/h2&gt;

&lt;p&gt;This implementation lacks a few things which I consider beyond the scope of this post, but feel free to implement them on your own:&lt;/p&gt;

&lt;h3 id=&#34;forwarding-references-and-perfect-forwarding&#34;&gt;Forwarding references and perfect forwarding&lt;/h3&gt;

&lt;p&gt;Specifically in the following places:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;naive_function&lt;/code&gt;&amp;rsquo;s &lt;code&gt;operator=&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;naive_function&lt;/code&gt;&amp;rsquo;s &lt;code&gt;operator()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CallableT&lt;/code&gt;&amp;rsquo;s constructor&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&#34;small-object-optimization&#34;&gt;Small-object optimization&lt;/h3&gt;

&lt;p&gt;For more details about Small Object Optimization (SOO) or Small String
Optimization (SSO) see my previous posts about
&lt;a href=&#34;https://shaharmike.com/cpp/std-string/&#34;&gt;&lt;code&gt;std::string&lt;/code&gt;&lt;/a&gt; and
&lt;a href=&#34;https://shaharmike.com/cpp/lambdas-and-functions/&#34;&gt;&lt;code&gt;std::function&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clang reserves 16 bytes for small objects in order to save dynamic allocations.
In &lt;code&gt;naive_function&lt;/code&gt; we always allocate dynamically.&lt;/p&gt;

&lt;h3 id=&#34;special-handling-for-operator-with-naive-function&#34;&gt;Special handling for &lt;code&gt;operator=&lt;/code&gt; with &lt;code&gt;naive_function&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;We have a templated &lt;code&gt;operator=&lt;/code&gt;. Can you guess what happens in the following piece of code?:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;naive_function&amp;lt;void()&amp;gt; f;
naive_function&amp;lt;void()&amp;gt; f2;
f2 = f;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I was surprised by this, but this actually fails to compile (tested on Visual Studio and clang). Reason is that copy-assignment-operator is deleted due to the fact that &lt;code&gt;callable_&lt;/code&gt; has no copy-assignment-operator. It does not fallback to our &lt;code&gt;operator=&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But even if we got it to work, it would create an inefficient double-dereference (or more if this was assigned to yet another &lt;code&gt;naive_function&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;An operator which would copy the internals would save this, and will also behave more sanely when the user changes objects that have been copied.&lt;/p&gt;

&lt;h2 id=&#34;appendix-full-code&#34;&gt;Appendix: Full code&lt;/h2&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;memory&amp;gt;
#include &amp;lt;cassert&amp;gt;
using namespace std;

template &amp;lt;typename T&amp;gt;
class naive_function;

template &amp;lt;typename ReturnValue, typename... Args&amp;gt;
class naive_function&amp;lt;ReturnValue(Args...)&amp;gt; {
public:
	template &amp;lt;typename T&amp;gt;
	naive_function&amp;amp; operator=(T t) {
		callable_ = std::make_unique&amp;lt;CallableT&amp;lt;T&amp;gt;&amp;gt;(t);
		return *this;
	}

	ReturnValue operator()(Args... args) const {
		assert(callable_);
		return callable_-&amp;gt;Invoke(args...);
	}

private:
	class ICallable {
	public:
		virtual ~ICallable() = default;
		virtual ReturnValue Invoke(Args...) = 0;
	};

	template &amp;lt;typename T&amp;gt;
	class CallableT : public ICallable {
	public:
		CallableT(const T&amp;amp; t)
			: t_(t) {
		}

		~CallableT() override = default;

		ReturnValue Invoke(Args... args) override {
			return t_(args...);
		}

	private:
		T t_;
	};

	std::unique_ptr&amp;lt;ICallable&amp;gt; callable_;
};

void func() {
	cout &amp;lt;&amp;lt; &amp;quot;func&amp;quot; &amp;lt;&amp;lt; endl;
}

struct functor {
	void operator()() {
		cout &amp;lt;&amp;lt; &amp;quot;functor&amp;quot; &amp;lt;&amp;lt; endl;
	}
};

int main() {
	naive_function&amp;lt;void()&amp;gt; f;
	f = func;
	f();
	f = functor();
	f();
	f = []() { cout &amp;lt;&amp;lt; &amp;quot;lambda&amp;quot; &amp;lt;&amp;lt; endl; };
	f();
}
&lt;/code&gt;&lt;/pre&gt;</description>
    </item>
    
    <item>
      <title>C&#43;&#43; vtables - Part 4 - Compiler-Generated Code</title>
      <link>https://shaharmike.com/cpp/vtable-part4/</link>
      <pubDate>Tue, 22 Mar 2016 00:00:00 +0000</pubDate>
      
      <guid>https://shaharmike.com/cpp/vtable-part4/</guid>
      <description>&lt;p&gt;So far in this mini-series we learned how the vtables and typeinfo records are placed in our binaries and how the compiler uses them. Now we&amp;rsquo;ll understand some of the work the compiler does for us automatically.&lt;/p&gt;

&lt;h2 id=&#34;constructors&#34;&gt;Constructors&lt;/h2&gt;

&lt;p&gt;For any class&amp;rsquo;s constructor the following code is generated:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Call parent(s) constructors if there are any;&lt;/li&gt;
&lt;li&gt;Set vtable pointer(s) if there are any;&lt;/li&gt;
&lt;li&gt;Initialize members according to initializer list;&lt;/li&gt;
&lt;li&gt;Execute code inside constructor&amp;rsquo;s brackets.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of the above can happen without explicit code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parent default constructors happen automatically unless otherwise specified;&lt;/li&gt;
&lt;li&gt;Members are default initialized unless they have a default value or an entry in the initializer list;&lt;/li&gt;
&lt;li&gt;The entire constructor can be marked &lt;code&gt;= default&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Only the vtable assignment is always hidden.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here&amp;rsquo;s an example:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
#include &amp;lt;string&amp;gt;
using namespace std;

class Parent {
public:
    Parent() { Foo(); }
    virtual ~Parent() = default;
    virtual void Foo() { cout &amp;lt;&amp;lt; &amp;quot;Parent&amp;quot; &amp;lt;&amp;lt; endl; }
    int i = 0;
};

class Child : public Parent {
public:
    Child() : j(1) { Foo(); }
    void Foo() override { cout &amp;lt;&amp;lt; &amp;quot;Child&amp;quot; &amp;lt;&amp;lt; endl; }
    int j;
};

class Grandchild : public Child {
public:
    Grandchild() { Foo(); s = &amp;quot;hello&amp;quot;; }
    void Foo() override { cout &amp;lt;&amp;lt; &amp;quot;Grandchild&amp;quot; &amp;lt;&amp;lt; endl; }
    string s;
};

int main() {
    Grandchild g;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Let&amp;rsquo;s write the pseudo-code for each class&amp;rsquo;s constructor:&lt;/p&gt;

&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Parent&lt;/th&gt;
&lt;th&gt;Child&lt;/th&gt;
&lt;th&gt;Grandchild&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;

&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1. vtable = Parent&amp;rsquo;s vtable;&lt;/td&gt;
&lt;td&gt;1. Call Parent&amp;rsquo;s default c&amp;rsquo;tor;&lt;/td&gt;
&lt;td&gt;1. Call Child&amp;rsquo;s default c&amp;rsquo;tor;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;2. i = 0;&lt;/td&gt;
&lt;td&gt;2. vtable = Child&amp;rsquo;s vtable;&lt;/td&gt;
&lt;td&gt;2. vtable = Grandchild&amp;rsquo;s vtable;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;3. Call Foo();&lt;/td&gt;
&lt;td&gt;3. j = 1;&lt;/td&gt;
&lt;td&gt;3. Call s&amp;rsquo;s default c&amp;rsquo;tor;&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;4. Call Foo();&lt;/td&gt;
&lt;td&gt;4. Call Foo();&lt;/td&gt;
&lt;/tr&gt;

&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;5. Call operator= on s;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Given this, it&amp;rsquo;s no surprise that in the context of a class constructor, the vtable points to that very class&amp;rsquo;s vtable rather than its concrete class. This means that virtual calls are resolved as if no inheritors are available. Thus the output is:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-console&#34;&gt;Parent
Child
Grandchild
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What about pure virtual functions? If they are not implemented (yes, you &lt;em&gt;can&lt;/em&gt; implement pure virtual functions, but why would you?) you&amp;rsquo;re probably (and hopefully) going to segfault. Some compilers actually omit an error about this, which is cool.&lt;/p&gt;

&lt;h2 id=&#34;destructors&#34;&gt;Destructors&lt;/h2&gt;

&lt;p&gt;As one might imagine, destructors have the same behavior of constructors, only happen in reverse order.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s a quick thought-exercise: why do destructors change the vtable pointer to point to the their own class&amp;rsquo;s rather than keep it pointing to the concrete class?  Answer: Because by the time the destructor runs, any inheriting class had already been destroyed. Calling such class&amp;rsquo;s methods is not something you want to do.&lt;/p&gt;

&lt;h2 id=&#34;implicit-casts&#34;&gt;Implicit casts&lt;/h2&gt;

&lt;p&gt;As we saw in &lt;a href=&#34;https://shaharmike.com/cpp/vtable-part2/&#34;&gt;Part 2&lt;/a&gt; &amp;amp; &lt;a href=&#34;https://shaharmike.com/cpp/vtable-part3/&#34;&gt;Part 3&lt;/a&gt;, a pointer to a child is not necessarily equal to the same instance&amp;rsquo;s parent pointer (like in multiple inheritance).&lt;/p&gt;

&lt;p&gt;Yet, there&amp;rsquo;s no added work for you (the developer) to call a function that receives a parent&amp;rsquo;s pointer. This is because the compiler implicitly offsets &lt;code&gt;this&lt;/code&gt; when you up-cast pointers and references to parent classes.&lt;/p&gt;

&lt;h2 id=&#34;dynamic-casts-rtti&#34;&gt;Dynamic casts (RTTI)&lt;/h2&gt;

&lt;p&gt;Dynamic casts use the typeinfo tables we explored in &lt;a href=&#34;https://shaharmike.com/cpp/vtable-part1/&#34;&gt;Part 1&lt;/a&gt;. They do it in runtime by looking at the typeinfo record that&amp;rsquo;s 1 pointer before what vtable pointer points to, and use the class there to check whether or not a cast is possible.&lt;/p&gt;

&lt;p&gt;This explains the &lt;a href=&#34;https://tinodidriksen.com/2010/04/14/cpp-dynamic-cast-performance/&#34;&gt;cost of &lt;code&gt;dynamic_cast&lt;/code&gt;&lt;/a&gt; when used a lot.&lt;/p&gt;

&lt;h2 id=&#34;method-pointers&#34;&gt;Method pointers&lt;/h2&gt;

&lt;p&gt;I plan to write a full post about method pointers in the future. Until then I&amp;rsquo;d like to stress that a method pointer pointing at a virtual function will actually call the overridden method (unlike non-member function pointers).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// TODO: add a link when the post is alive&lt;/code&gt;&lt;/p&gt;

&lt;h2 id=&#34;test-yourself&#34;&gt;Test yourself!&lt;/h2&gt;

&lt;p&gt;You should now be able to explain &lt;em&gt;to yourself&lt;/em&gt; why the following piece of code behaves the way it does:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&#34;language-cpp&#34;&gt;#include &amp;lt;iostream&amp;gt;
using namespace std;

class FooInterface {
public:
	virtual ~FooInterface() = default;
	virtual void Foo() = 0;
};

class BarInterface {
public:
	virtual ~BarInterface() = default;

	virtual void Bar() = 0;
};

class Concrete : public FooInterface, public BarInterface {
public:
	void Foo() override { cout &amp;lt;&amp;lt; &amp;quot;Foo()&amp;quot; &amp;lt;&amp;lt; endl; }
	void Bar() override { cout &amp;lt;&amp;lt; &amp;quot;Bar()&amp;quot; &amp;lt;&amp;lt; endl; }
};

int main() {
	Concrete c;
	c.Foo();
	c.Bar();

	FooInterface* foo = &amp;amp;c;
	foo-&amp;gt;Foo();

	BarInterface* bar = (BarInterface*)(foo);
	bar-&amp;gt;Bar(); // Prints &amp;quot;Foo()&amp;quot; - WTF?
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This concludes my first blog post, which grew to become a 4 piece post. I hope you learned some new things, I know I sure did.&lt;/p&gt;</description>
    </item>
    
  </channel>
</rss>