C++ 如何将一种类型的唯一_ptr复制到另一种类型
参考以下代码C++ 如何将一种类型的唯一_ptr复制到另一种类型,c++,c++11,unique-ptr,C++,C++11,Unique Ptr,参考以下代码 #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<Derive
#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;
}
它们似乎都不接受其他类型的指针。我错过了什么?调用哪个构造函数
谢谢 第6个,模板构造函数
template< class U, class E >
unique_ptr( unique_ptr<U, E>&& u );
模板
唯一的(唯一的);
在其他一些条件中,它要求“unique\u ptr::pointer
可以隐式转换为pointer
”。换句话说,U*
需要隐式转换为unique\U ptr
存储的指针类型。在您的例子中,它是,因为派生的*
隐式地可转换为Base*
template< class U, class E >
unique_ptr( unique_ptr<U, E>&& u );