title: Is make_shared really more efficient than new?
body: <p>I was experimenting with <code>shared_ptr</code> and <code>make_shared</code> from C++11 and programmed a little toy example to see what is actually happening when calling <code>make_shared</code>. As infrastructure I was using llvm/clang 3.0 along with the llvm std c++ library within XCode4.</p>
  
  <pre><code>class Object
  {
  public:
      Object(const string&amp; str)
      {
          cout &lt;&lt; "Constructor " &lt;&lt; str &lt;&lt; endl;
      }
  
      Object()
      {
          cout &lt;&lt; "Default constructor" &lt;&lt; endl;
  
      }
  
      ~Object()
      {
          cout &lt;&lt; "Destructor" &lt;&lt; endl;
      }
  
      Object(const Object&amp; rhs)
      {
          cout &lt;&lt; "Copy constructor..." &lt;&lt; endl;
      }
  };
  
  void make_shared_example()
  {
      cout &lt;&lt; "Create smart_ptr using make_shared..." &lt;&lt; endl;
      auto ptr_res1 = make_shared&lt;Object&gt;("make_shared");
      cout &lt;&lt; "Create smart_ptr using make_shared: done." &lt;&lt; endl;
  
      cout &lt;&lt; "Create smart_ptr using new..." &lt;&lt; endl;
      shared_ptr&lt;Object&gt; ptr_res2(new Object("new"));
      cout &lt;&lt; "Create smart_ptr using new: done." &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Now have a look at the output, please:</p>
  
  <blockquote>
    <p>Create smart_ptr using make_shared...</p>
    
    <p>Constructor make_shared</p>
    
    <p>Copy constructor...</p>
    
    <p>Copy constructor...</p>
    
    <p>Destructor</p>
    
    <p>Destructor</p>
    
    <p>Create smart_ptr using make_shared: done.</p>
    
    <p>Create smart_ptr using new... </p>
    
    <p>Constructor new</p>
    
    <p>Create smart_ptr using new: done.</p>
    
    <p>Destructor</p>
    
    <p>Destructor</p>
  </blockquote>
  
  <p>It appears that <code>make_shared</code> is calling the copy constructor two times. If I allocate memory for an <code>Object</code> using a regular <code>new</code> this does not happen, only one <code>Object</code> is constructed.</p>
  
  <p>What I am wondering about is the following. I heard that <code>make_shared</code> is supposed to be more efficient than using <code>new</code><sup>(<a href="http://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared" rel="noreferrer">1</a>, <a href="http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/STL11-Magic-Secrets" rel="noreferrer" title="around minute 10">2</a>)</sup>. One reason is because <code>make_shared</code> allocates the reference count together with the object to be managed in the same block of memory. OK, I got the point. This is of course more efficient than two separate allocation operations.</p>
  
  <p>On the contrary I don't understand why this has to come with the cost of two calls to the copy constructor of <code>Object</code>. Because of this I am not convinced that <code>make_shared</code> is more efficient than allocation using <code>new</code> in <em>every</em> case. Am I wrong here? Well OK, One could implement a move constructor for <code>Object</code> but still I am not sure whether this this is more efficient than just allocating <code>Object</code> through <code>new</code>. At least not in every case. It would be true if copying <code>Object</code> is less expensive than allocating memory for a reference counter. But the <code>shared_ptr</code>-internal reference counter could be implemented using a couple of primitive data types, right?</p>
  
  <p>Can you help and explain why <code>make_shared</code> is the way to go in terms of efficiency, despite the outlined copy overhead? </p>