Cur*_*ous 0 c++ unique-ptr c++11
参考以下代码
#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;
}
Run Code Online (Sandbox Code Playgroud)
哪个构造函数被调用unique_ptr?我看了一下cppreference.com和我发现的构造函数(前c ++ 17)(为了完成)
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 );
Run Code Online (Sandbox Code Playgroud)
他们似乎都没有接受另一种类型的指针.我错过了什么?调用哪个构造函数?
谢谢!
数字6,模板构造函数.
template< class U, class E >
unique_ptr( unique_ptr<U, E>&& u );
Run Code Online (Sandbox Code Playgroud)
在其他一些条件中,这需要" unique_ptr<U, E>::pointer可以隐含地转换为pointer".换句话说,U*需要隐式转换为您的unique_ptr商店的指针类型.在你的情况下,它是,因为Derived*可以隐式转换为Base*.