title: Converting std::unique_ptr<Derived> to std::unique_ptr<Base>
body: <p>Using C++11, let's say I have factory functions dealing with base and derived classes:</p>
  
  <pre><code>#include &lt;memory&gt;
  
  using namespace std;
  
  struct B { virtual ~B() {} };
  struct D : B {};
  
  unique_ptr&lt;B&gt; MakeB()
  {
      auto b = unique_ptr&lt;B&gt;( new B() );
      return b; // Ok!
  }
  
  unique_ptr&lt;B&gt; MakeD()
  {
      auto d = unique_ptr&lt;D&gt;( new D() );
      return d; // Doh!
  }
  </code></pre>
  
  <p>On the last line above, I need <code>move(d)</code> in order <a href="http://coliru.stacked-crooked.com/a/2722d5cc691bd21d" rel="noreferrer">to make it work</a>, otherwise <a href="http://coliru.stacked-crooked.com/a/52c975960ea541be" rel="noreferrer">I get</a> "Error: invalid conversion from <code>std::unique_ptr&lt;D&gt;</code> to <code>std::unique_ptr&lt;D&gt;&amp;&amp;</code>." My intuition said that in this context, the compiler should know that it could implicitly make <code>d</code> an rvalue and move it into the base pointer, but it doesn't.</p>
  
  <p>Is this a non-conformancy in my compilers (gcc 4.8.1 and VS2012)? The intended design of <code>unique_ptr</code>? A defect in the standard?</p>
  
  <hr>
  
  <p><em>Update</em>: C++14 fixes this. Newer compilers such as GCC 9 <a href="http://coliru.stacked-crooked.com/a/ad7c126d1ba99c6e" rel="noreferrer">accept the original code</a> even with <code>-std=c++11</code>.</p>