[英]Why cannot I delete unique_ptr<char[]> with reset()?
当我有一个指向单个对象的唯一指针时,我可以使用reset()
删除它:
std::unique_ptr<char> variable(new char);
variable.reset();
但是,这对包含数组的std::unique_ptr
不起作用。 为什么? 删除这样的指针的正确方法是什么?
我正在使用Embarcadero C ++ Builder 10.1。 相关标准是C ++ 11。
当我有一个包含数组的唯一指针时,无法编译:
std::unique_ptr<char[]> variable(new char[10]);
variable.reset();
错误消息no matching function to call for 'reset'
。
这也失败了:
std::unique_ptr<char[]> variable(new char[10]);
variable.reset(nullptr);
错误消息cannot initialize a variable of type 'pointer' (aka 'char *') with an lvalue of type '<bound member function type>'
并assigning to '<bound member function type>' from incompatible type '_Null_ptr_type' (aka 'nullptr_t')
。
这编译:
std::unique_ptr<char[]> variable(new char[10]);
variable = nullptr;
template<class _Uty>
using _Enable_ctor_reset = enable_if_t<
is_same<_Uty, pointer>::value
|| (is_same<pointer, element_type *>::value
&& is_pointer<_Uty>::value
&& is_convertible<
remove_pointer_t<_Uty>(*)[],
element_type(*)[]
>::value)>;
_Myt& operator=(_Null_ptr_type) _NOEXCEPT
{ // assign a null pointer
reset(pointer());
return (*this);
}
_NOINLINE void reset(_Null_ptr_type _Ptr) _NOEXCEPT
{ // establish new null pointer
pointer _Old = this->_Myptr;
this->_Myptr = _Ptr;
if (_Old != pointer())
this->get_deleter()(_Old);
}
template<class _Uty,
class = _Enable_ctor_reset<_Uty> >
void reset(_Uty _Ptr) _NOEXCEPT
{ // establish new pointer
pointer _Old = get();
this->_Myptr() = _Ptr;
if (_Old != pointer())
this->get_deleter()(_Old);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.