为什么 unique_ptr::reset 没有带有删除器的重载?
是否有原因 unique_ptr::reset
没有采用 const deleter&
和 deleter&&
来匹配其构造函数的重载将这些作为第二个参数?
unique_ptr
中存储的删除器将使用 reset
中的参数进行复制分配或移动分配。如果删除器是不可复制或不可移动的,则调用 reset
的相应重载将无法编译。这似乎与构造函数的行为一致。
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?
The stored deleter in unique_ptr
would be copy assigned or move assigned with the argument from reset
. If the deleter is noncopyable or nonmovable, calling the corresponding overload of the reset
wouldn't compile. This seems like it would be consistent behavior with the constructors.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我考虑过添加它,但您可以使用移动赋值运算符获得等效的功能:
因此,为了保持 API 相当小,我选择不对
reset
说同样的事情。更新
我生活和学习......
语法实际上可以比我上面显示的简单得多:
I thought about adding that but you can get the equivalent functionality with a move assignment operator:
So I opted for not saying the same thing with
reset
in the interest of keeping the API reasonably small.Update
And I live and learn...
The syntax can actually be much simpler than what I show above:
因为删除器在构造时就存储在对象中。由于删除器类型是模板参数,因此在构造之后无法“转换”该类以使用另一个类。
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.