title: c++ unique_ptr argument passing
body: <p>Suppose I have the following code:</p>
  
  <pre><code>class B { /* */ };
  
  class A {
      vector&lt;B*&gt; vb;
  public:
      void add(B* b) { vb.push_back(b); }
  };
  
  
  int main() {
  
      A a;
      B* b(new B());
      a.add(b);
  }
  </code></pre>
  
  <p>Suppose that in this case, all raw pointers <code>B*</code> can be handled through <code>unique_ptr&lt;B&gt;</code>.</p>
  
  <p>Surprisingly, I wasn't able to find how to convert this code using <code>unique_ptr</code>. After a few tries, I came up with the following code, which compiles:</p>
  
  <pre><code>class A {
      vector&lt;unique_ptr&lt;B&gt;&gt; vb;
  public:
      void add(unique_ptr&lt;B&gt; b) { vb.push_back(move(b)); }
  };
  
  
  int main() {
  
      A a;
      unique_ptr&lt;B&gt; b(new B());
      a.add(move(b));
  }
  </code></pre>
  
  <p>So my simple question: is this the way to do it and in particular, is <code>move(b)</code> the only way to do it? (I was thinking of rvalue references but I don't fully understand them.)</p>
  
  <p>And if you have a link with complete explanations of move semantics, <code>unique_ptr</code>, etc. that I was not able to find, don't hesitate to share it.</p>
  
  <p>EDIT According to <a href="http://thbecker.net/articles/rvalue_references/section_01.html" rel="noreferrer">http://thbecker.net/articles/rvalue_references/section_01.html</a>, my code seems to be OK.</p>
  
  <p>Actually, std::move is just syntactic sugar. With object x of class X, <code>move(x)</code> is just the same as: </p>
  
  <pre><code>static_cast &lt;X&amp;&amp;&gt;(x)
  </code></pre>
  
  <p>These 2 move functions are needed because casting to a rvalue reference:</p>
  
  <ol>
  <li>prevents function "add" from passing by value</li>
  <li>makes <code>push_back</code> use the default move constructor of B</li>
  </ol>
  
  <p>Apparently, I do not need the second <code>std::move</code> in my <code>main()</code> if I change my "add" function to pass by reference (ordinary lvalue ref).</p>
  
  <p>I would like some confirmation of all this, though...</p>