从https://en.cppreference.com/w/cpp/memory/unique_ptr/reset主模板unique_ptr
的成员void reset( pointer ptr = pointer() ) noexcept; (1)
template< class U >
void reset( U ) noexcept; (2)
void reset( std::nullptr_t p = nullptr ) noexcept; (3)
对于我来说,对于(1),如果没有参数是给予者,那么指针类型的默认构造函数将被调用。但是它应该表现为一个nullptr,这样unique_ptr内部的指针将被删除,它将被设置为空,怎么来的?
(2)的解释是
2) Behaves the same as the reset member of the primary template, except that it will only participate in overload resolution if either:
U is the same type as pointer, or
pointer is the same type as element_type* and U is a pointer type V* such that V(*)[] is convertible to element_type(*)[].
我实在听不懂,有人能解释一下吗?对于我来说,似乎对于(1),如果没有参数是给予者,那么指针类型的默认构造函数将被调用。但是它应该表现为一个nullptr,这样unique_ptr内部的指针将被删除,它将被设置为空,怎么来的?
">指针类型的默认构造函数"不是一个真正的东西-但是,是的,默认参数是一个零初始化的T*
,它将具有与您所追求的相同的效果。pointer()
(其中pointer
是T*
的typedef
)将初始化为nullptr
。
using pointer = foo*; // example pointer typedef
pointer ptr = pointer(); // initialized to nullptr by default
与nullptr
比较,所有占用的资源都将被释放。
重载(2)和(3)是针对数组专门化的,其中U = T[]
.