title: C++ what happens when function returns vector<shared_ptr<>>?
body: <p>I have the following code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;memory&gt;
  #include &lt;vector&gt;
  
  class Test
  {
  public:
      Test() {}
      ~Test() { std::cerr &lt;&lt; "Delete\n"; }
  };
  
  std::vector&lt;std::shared_ptr&lt;Test&gt;&gt; makeList()
  {
      std::vector&lt;std::shared_ptr&lt;Test&gt;&gt; list;
      list.push_back(std::make_shared&lt;Test&gt;(Test()));
  
      return std::move(list);
  }
  
  int main(int argc ,char **argv)
  {
  
      std::vector&lt;std::shared_ptr&lt;Test&gt;&gt; list;
      std::cerr &lt;&lt; "Before\n";
      list = makeList();
      std::cerr &lt;&lt; "After\n";
  
      return 0;
  }
  </code></pre>
  
  <p>Which I compile with:</p>
  
  <pre><code>clang++ -std=c++14 -o ptr ptr.cpp
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>Before
  Delete
  After
  Delete
  </code></pre>
  
  <p>My question is:  why is there an object being deleted in the <code>makeList</code> function? My assumption was that the <code>list</code> from the function would be moved into <code>list</code> from <code>main</code> and that therefore no object would be deleted/recreated in the process?</p>
  
  <p>Can this be avoided (as obviously this code is not optimum)?</p>