Why does unique_ptr<Derived> implicitly cast to unique_ptr<Base>? : <p>I wrote the following code that uses <code>unique_ptr&lt;Derived&gt;</code> where a <code>unique_ptr&lt;Base&gt;</code> is expected</p>

<pre><code>class Base {
    int i;
 public:
    Base( int i ) : i(i) {}
    int getI() const { return i; }
};

class Derived : public Base {
    float f;
 public:
    Derived( int i, float f ) : Base(i), f(f) {}
    float getF() const { return f; }
};

void printBase( unique_ptr&lt;Base&gt; base )
{
    cout &lt;&lt; "f: " &lt;&lt; base-&gt;getI() &lt;&lt; endl;
}

unique_ptr&lt;Base&gt; makeBase()
{
    return make_unique&lt;Derived&gt;( 2, 3.0f );
}

unique_ptr&lt;Derived&gt; makeDerived()
{
    return make_unique&lt;Derived&gt;( 2, 3.0f );
}

int main( int argc, char * argv [] )
{
    unique_ptr&lt;Base&gt; base1 = makeBase();
    unique_ptr&lt;Base&gt; base2 = makeDerived();
    printBase( make_unique&lt;Derived&gt;( 2, 3.0f ) );

    return 0;
}
</code></pre>

<p>and i expected this code to not compile, because according to my understanding <code>unique_ptr&lt;Base&gt;</code> and <code>unique_ptr&lt;Derived&gt;</code> are unrelated types and <code>unique_ptr&lt;Derived&gt;</code> isn't in fact derived from <code>unique_ptr&lt;Base&gt;</code> so the assignment shouldn't work.</p>

<p>But thanks to some magic it works, and i don't understand why, or even if it's safe to do so.
Can someone explain please?</p>
