As I understand it, a std::weak_ptr
is used as a safe way of referencing memory referenced by a std::shared_ptr
which may have been deallocated. Can it be used with a std::unique_ptr
as well?

- 8,038
- 2
- 40
- 58

- 896
- 8
- 15
-
3Might find this an interesting read: ["Why can't a weak_ptr be constructed from a unique_ptr?"](http://stackoverflow.com/questions/29059343/why-cant-a-weak-ptr-be-constructed-from-a-unique-ptr) – WhozCraig Mar 09 '17 at 16:46
-
4No, it doesn't make sense. A `std::weak_ptr` requires *shared ownership* which `std::unique_ptr` does not possess. – Galik Mar 09 '17 at 16:50
-
2A unique_ptr is *unique*, that's its purpose in life. – n. m. could be an AI Mar 09 '17 at 16:58
4 Answers
As I understand it std::weak_ptr is used as a safe way of referencing memory referenced by shared_ptrs which may have been deallocated.
You understand wrong. std::weak_ptr
allowes to access object, which ownership is maintained by std::shared_ptr
without sharing it. Now when you really understand what std::weak_ptr
does you should understand that your question about std::unique_ptr
does not make any sense.

- 43,454
- 1
- 47
- 90
There is an equivalent pointer that act as an observer to a std::unique_ptr
, but will not provide safety in the same way as std::weak_ptr
.
That pointer is the raw pointer. They are safe to use if they don't outlive the unique owner.
If you need to know if the lifetime of the owner of a raw pointer ended, then no, there is no way and it's not the goal of std::unique_ptr
.

- 39,621
- 9
- 77
- 141
Nope.
Unfortunately you will have to use std::shared_ptr
, and document that the std::weak_ptr
s should only be locked temporarily.

- 62,093
- 7
- 131
- 191
-
Unfortunately or fortunately? Dual ownership is not a feature of `unique_ptr` – curiousguy Sep 09 '17 at 14:25
-
@curiousguy "unfortunately" because you must rely on outer code to never hold onto a locked `std::weak_ptr` to keep the long-term single ownership semantic. – Quentin Sep 09 '17 at 16:53
-
The distinct syntax and semantic superiority of unique owning smart ptr over shared owning smart ptr is the `release` method (no exception, guaranteed behavior). You can have a unique_ptr_except_locked_weak_ptr, and expect locking to be "short", but by definition a lock (of a weak ref) can outlive something and be the last owner, `release()` cannot be guaranteed, etc. – curiousguy Sep 15 '17 at 01:10
-
@curiousguy Yes, that's why it's unfortunate that you need to waive these advantages and switch over to `std::shared_ptr` if you want weak pointers. It looks like we're agreeing here :) – Quentin Sep 15 '17 at 07:54
-
I am not sure what you are suggesting as an alternative. Weak implies multiple owners at least for a small time. And "small" can last as long as the programmer wants. You could outlaw direct duplication of a strong ref and only allow strong->weak->strong, but I don't see the point. – curiousguy Sep 15 '17 at 18:20
-
1@curiousguy well I don't see any alternative. There is only `std::shared_ptr`, and trusting the next guy not to lock its `std::weak_ptr`s for too long. – Quentin Sep 15 '17 at 18:39
No.
The only constructors available to a weak_ptr
are one that take either a shared_ptr
or another weak_ptr
.

- 100,020
- 15
- 103
- 173