I want to assign constructed shared_ptr to weak_ptr:
std::weak_ptr<void> rw = std::shared_ptr<void>(operator new(60), [](void *pi) { operator delete(pi); });
But, when I do rw.expired()
, it shows expired means it is empty. Any suggestions where I am going wrong?
Thanks in advance.
Purpose of
std::shared_ptr
is to release managed object when last shared pointer which points to it is destroyed or reassigned to somewhere else. You created a temporary shared ptr, assgned it tostd::weak_ptr
and then it is just destroyed at the end of the expression. You need to keep it alive:now
rw
would not expire at least whileshared
is still alive.