[英]Implementation of std::unique_ptr<T[]>::reset in gcc 6
Since GCC 6 in C++ the declaration/definition of a unique_ptr<T[]>::reset
method (not the one, that accepts only nullptr_t
) looks like this: 由于C ++中的GCC 6,
unique_ptr<T[]>::reset
方法(不是一个仅接受nullptr_t
)的声明/定义如下所示:
template <typename _Up,
typename = _Require<
__or_<is_same<_Up, pointer>,
__and_<is_same<pointer, element_type*>,
is_pointer<_Up>,
is_convertible<
typename remove_pointer<_Up>::type(*)[],
element_type(*)[]
>
>
>
>>
void
reset(_Up __p) noexcept
{
using std::swap;
swap(std::get<0>(_M_t), __p);
if (__p != nullptr)
get_deleter()(__p);
}
This was changed at some point to implement N4089 . 为了实施N4089,对此进行了某些更改。 According to that document:
根据该文件:
This function behaves the same as the reset member of the primary template, except that it shall not participate in overload resolution unless either
此函数的行为与主模板的reset成员相同,不同之处在于,除非该函数不参与任何重载解析,否则
—
U
is the same type aspointer
, or—
U
与pointer
具有相同的类型,或者—
pointer
is the same type aselement_type*
,U
is a pointer typeV*
, andV(*)[]
is convertible toelement_type(*)[]
.—
pointer
的类型与element_type*
相同,U
是指针类型V*
,并且V(*)[]
可转换为element_type(*)[]
。
Let's consider the following example: 让我们考虑以下示例:
std::unique_ptr<const char []> ptr1;
std::unique_ptr<char []> ptr2(new char[5]);
ptr1 = std::move(ptr2);
Since version 6 GCC produces an error, complaining that it cannot call std::swap
with const char*&
and char*&
. 由于版本6 GCC产生错误,因此抱怨它无法使用
const char*&
和char*&
调用std::swap
。 reset
method takes place in the overload resolution as char[]
is convertible to const char[]
, but naturally std::swap
awaits two references of the same type. reset
方法在重载解析中发生,因为char[]
可转换为const char[]
,但自然std::swap
等待两个相同类型的引用。
Is this considered to be a correct behavior? 这被认为是正确的行为吗? If so, then why?
如果是这样,那为什么呢? If I can implicitly convert
char[]
to const char[]
, why the same should not be possible with unique_ptr
? 如果我可以将
char[]
隐式转换为const char[]
,为什么对unique_ptr
不能做到相同?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.