17

I have a class A which has a field of type std::unique_ptr:

class A
{
public:
   std::unique_ptr pointer;
// class body
};

And somewhere in code, I'm using few std::shared_ptrs which point to the same object. Now what I'd like to achieve is to move ownership to this std::unique_ptr in my class, so that if all shared_ptrs will be destroyed, my object will stay alive as long as this unique_ptr will stay alive.

My question is - is it possible to move ownership from std::shared_ptr to std::unique_ptr and if yes, how can I do this?

7
  • 8
    Logically such a scenario doesn't make sense to me. Suppose for a while that it is possible to transfer the onwership, but you could do that only when you're sure that there is only one shared_ptr alives; if that is the case, then you can still use shared_ptr as member of A and pretend that it is unique_ptr. Commented Mar 11, 2013 at 11:35
  • 1
    "move ownership to this std::unique_ptr in my class, so that if all shared_ptrs will be destroyed, my object will stay alive as long as this unique_ptr will stay alive..." Why not make just one more shared in A? Commented Mar 11, 2013 at 11:36
  • 1
    what if there are N other shared ptrs pointing to the same object? How could you possibly handle that? shared_ptr doesn't provide release method for a reason. Commented Mar 11, 2013 at 11:37
  • 1
    Maybe what you want is a shared_ptr in A and all other weak_ptr?? Commented Mar 11, 2013 at 11:46
  • 3
    @Mosquito: In that case, you're looking at the wrong smart pointer. What you probably need is called std::weak_ptr. If so, then make sure you use one std::shared_ptr and all others as std::weak_ptr. Commented Mar 11, 2013 at 12:01

1 Answer 1

16

Logically such a scenario doesn't make sense to me.

Suppose for a while that it is possible to transfer the ownership, but you could do that only when you're sure that there is only one shared_ptr alives; if that is the case, then you can still use shared_ptr as member of A and pretend that it is unique_ptr.

And then you commented:

That's true, I could create shared_ptr in A class. I think I misunderstood a concept a bit again. I wanted to behave it in this way: if unique_ptr dies, the object itself dies too, even though shared_ptrs still point to it, but that's silly as they wouldn't know that object itself was destroyed and therefore they wouldn't be nullptrs.

In that case, you're looking at the wrong smart pointer. What you probably need is called std::weak_ptr. If so, then make sure you use one std::shared_ptr and all others as std::weak_ptr.

Sign up to request clarification or add additional context in comments.

1 Comment

Well, in fact the scenario just came up legitimately for me since my code uses unique_ptr (for good reasons), but I have to get data from a lib that can't handle noncopyable types. Sure, I'll want to fix the lib, but upstream & deployment will take time, so there you have it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.