1

Is it possible to get the weak reference count to a shared_ptr? I know how I can get the shared_ptr use count using

std::shared_ptr::use_count

but I would like to know if there are any non-implementation specific ways to get the number of weak_count references?

Juan
  • 3,667
  • 3
  • 28
  • 32
  • There's none. At most, there might be a way many implementations happen to support. – Deduplicator Aug 07 '14 at 16:19
  • 1
    What is the real problem you're trying to solve? (And also note the standard puts no performance constraints on `use_count` either `[20.7.2.2.5/8]`). – Mark B Aug 07 '14 at 16:38
  • @MarkB: I am interested in this too; since there is no way to prevent someone else to create another reference concurrently... – Matthieu M. Aug 07 '14 at 16:42
  • Even if you could get to the weak reference count, it's unlikely to be useful because the value is implementation dependent. A library may choose to implement it as `weak_count = num_weak_refs + (use_count > 0)` or `weak_count = num_weak_refs + use_count` – Praetorian Aug 07 '14 at 16:43
  • I'm just exploring options to prevent shared_ptr cycles. – Juan Aug 07 '14 at 16:44

2 Answers2

2

Not in C++11, and not in this draft of C++14.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
-1

No, remember that weak_ptrs are just observers rather than an actual pointer to the object they are not designed to return their counts.