[英]Why doesn't unique_ptr::reset have overloads that take a deleter?
Is there a reason unique_ptr::reset
doesn't have overloads that take a const deleter&
and deleter&&
to match its constructors that take those as a second argument? 有没有一个原因,
unique_ptr::reset
没有带有const deleter&
和deleter&&
重载来匹配它的构造函数,这些构造函数将这些作为第二个参数?
The stored deleter in unique_ptr
would be copy assigned or move assigned with the argument from reset
. unique_ptr
存储的删除器将被复制分配或移动分配来自reset
的参数。 If the deleter is noncopyable or nonmovable, calling the corresponding overload of the reset
wouldn't compile. 如果删除器是不可复制的或不可移动的,则调用相应的
reset
重载将无法编译。 This seems like it would be consistent behavior with the constructors. 这似乎与构造函数的一致行为。
I thought about adding that but you can get the equivalent functionality with a move assignment operator: 我想添加它,但你可以使用移动赋值运算符获得等效的功能:
ptr = unique_ptr<T, D>(new T(another_value), D(another_state));
So I opted for not saying the same thing with reset
in the interest of keeping the API reasonably small. 因此,为了保持API合理地小,我选择不对
reset
说同样的事情。
Update 更新
And I live and learn... 我生活和学习......
The syntax can actually be much simpler than what I show above: 语法实际上可以比我上面显示的简单得多:
ptr = {new T(another_value), D(another_state)};
Because the deleter is stored in the object at construction. 因为删除器存储在构造中的对象中。 As the deleter type is a template argument, after construction there is no way to "convert" the class to use another one.
由于删除器类型是模板参数,因此在构造之后无法“转换”类以使用另一个类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.