0

I am storing a weak_ptr for avoiding circular structure, and I know that in some particular context the shared_ptr is still valid.

Can I get the raw pointer from weak_ptr without casting to shared_ptr (which involves memory write)?

(note: this is not a duplicate of How to maintain a weak pointer to a parent in C++?, I am specifically asking about an unsafe way, not about getting the pointer as such)

Community
  • 1
  • 1
eudoxos
  • 18,545
  • 10
  • 61
  • 110

1 Answers1

0

Can I get the raw pointer from weak_ptr without casting to shared_ptr

No.

N.B. you don't "cast" you construct a shared_ptr from the weak_ptr.

(which involves memory write)?

What do you mean here? Creating a shared_ptr will increment a reference count, is that what you're referring to?

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • Yes, I mean exactly the reference count increment. If I use the pointer only briefly and know it is valid, that is useless. – eudoxos Oct 02 '12 at 08:59
  • PS sorry for the `cast` wording, I know it means constructing a `shared_ptr`. – eudoxos Oct 02 '12 at 08:59