shared_ptr发布 [英] shared_ptr release
问题描述
我正在尝试发布shared_ptr(您可以发布unique_ptr的方式)。我知道,当shared_ptr不唯一时,这没有任何意义,但是我有一个shared_ptr,它保证是唯一的。我已经尝试过...
I'm attempting to release a shared_ptr (the way you can release a unique_ptr). I know this doesn't make sense when the shared_ptr isn't unique, but I have a shared_ptr which is guaranteed to be unique. I've tried...
m_pObj.reset((T*)nullptr, [](T* const){});
...但是它只是删除对象。我不确定如果shared_ptr无论如何都会调用delete,那么deleter参数有什么用。
...but it just deletes the object anyway. I'm not sure what that deleter argument is good for if shared_ptr winds up calling delete anyway.
有没有办法实现这一点(如果特定于VS2010的解决方案,欢迎使用没有其他方法。)
Is there any way to achieve this (solutions specific to VS2010 are welcome if there is no other way).
推荐答案
在以dup关闭此获取之前,我将对此添加看法
我认为有很多很好的理由使您偶尔想要释放一个shared_ptr(或boost :: scoped_ptr)。但是,设计这些课程的人认为您不应该这样做。 (这是他们的孩子,他们有权)。
I think there are a lot of good reasons why you would occasionally want to release a shared_ptr (or boost::scoped_ptr). However, the people who designed these classes think that you should not be able to do this. (It's their baby, they have a right to.)
据我所知,根本不可能拆下shared_ptr。您将需要使用另一个类,或者设计一个不需要分离它的设计。
As far as I can see, it is simply not possible to detach a shared_ptr. You will need to either use another class or come up with a design where you do not need to detach it.
我应该在您可以实际使用以下方法读取伪造文件的同时,非常特殊的删除器。
And I should read the dupes as you can actually hack it together using a very special deleter.
这篇关于shared_ptr发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!