为什么 unique_ptr::reset 没有带有删除器的重载?

发布于 2025-01-02 14:54:59 字数 281 浏览 3 评论 0原文

是否有原因 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爺獨霸怡葒院 2025-01-09 14:54:59

我考虑过添加它,但您可以使用移动赋值运算符获得等效的功能:

ptr = unique_ptr<T, D>(new T(another_value), D(another_state));

因此,为了保持 API 相当小,我选择不对 reset 说同样的事情。

更新

我生活和学习......

语法实际上可以比我上面显示的简单得多:

ptr = {new T(another_value), D(another_state)};

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.

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)};
握住我的手 2025-01-09 14:54:59

因为删除器在构造时就存储在对象中。由于删除器类型是模板参数,因此在构造之后无法“转换”该类以使用另一个类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文