开发者

shared_ptr release [duplicate]

This question already has answers here: Closed 11 years ago. 开发者_如何转开发

Possible Duplicate:

How to release pointer from boost::shared_ptr?

Detach a pointer from a 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){});

...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.

Is there any way to achieve this (solutions specific to VS2010 are welcome if there is no other way).


Before this get's closed as dup, I'll add my take on this.

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.)

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜