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?

Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
  • 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`. – Nawaz Mar 11 '13 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? – qPCR4vir Mar 11 '13 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. –  Mar 11 '13 at 11:37
  • You could fudge it by checking the reference count and checking it is 1, but you shouldn't. What would you do if it wasn't equal to 1? – Alex Chamberlain Mar 11 '13 at 11:43
  • @Nawaz 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_ptr`s still point to it, but that's silly as they wouldn't know that object itself was destroyed and therefore they wouldn't be `nullptr`s. Post it as an answer so that I can accept it. :-) – Piotr Chojnacki Mar 11 '13 at 11:45
  • 1
    Maybe what you want is a shared_ptr in A and all other weak_ptr?? – qPCR4vir Mar 11 '13 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`. – Nawaz Mar 11 '13 at 12:01

1 Answers1

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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 5
    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. – Victor Mataré Oct 19 '16 at 17:09