vendredi 1 juillet 2016

How copying a unique_ptr of one type to another works


With reference to the following code

#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::make_unique;

struct Base {};
struct Derived : public Base {};

int main() {

    auto base_uptr = std::unique_ptr<Base>{make_unique<Derived>()};
    return 0;
}

which constructor gets called for the unique_ptr? I took a look at cppreference.com and the constructors I found (pre c++17) were (for the sake of completion)

constexpr unique_ptr();
constexpr unique_ptr( nullptr_t );
explicit unique_ptr( pointer p );
unique_ptr( pointer p, /* see below */ d1 );
unique_ptr( pointer p, /* see below */ d2 );
unique_ptr( unique_ptr&& u );

template< class U, class E >
unique_ptr( unique_ptr<U, E>&& u );

None of them seem to accept the pointer of the other type. What am I missing? Which constructor gets called?

Thanks!


Aucun commentaire:

Enregistrer un commentaire