【发布时间】:2021-05-30 13:57:45
【问题描述】:
来自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里面的指针就会被删除,它会被设置为null,怎么会呢?
对 (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(*)[].
我真的无法理解,有人可以解释/改写吗?
【问题讨论】:
标签: c++ c++11 smart-pointers unique-ptr