c++ - Syntax for converting expired weak_ptr<T> to shared_ptr<T> -
from i've read, shared_ptr<t>
not de-allocated until both strong references , weak references dropped.
i understand shared object considered expired when there no more strong references it. standard lock() function of weak_ptr<t>
therefore fails in such case because object considered 'expired'.
however, if deleter of shared pointer overridden such managed object not deleted, should valid generated shared_ptr<t>
weak_ptr<t>
- cannot find right syntax this.
std::shared_ptr<int> s_ptr(new(42), d()); std::weak_ptr<int) w_ptr(s_ptr); s_ptr.reset(); s_ptr = std::shared_ptr<int>(w_ptr, false);
edit
to clarify bit further, i'm trying construct object pool of re-usable shared_ptr<t>
. reason behind because every use of shared_ptr results in 1 or more heap memory allocations. i've added deleter every shared_ptr<t>
stores weak_ptr<t>
reference such deleter gets called should able re-add pool of available shared_ptr<t>
objects (with managed object intact). keeping weak_ptr<t>
stored inside shared_ptr<t>
's deleter, therefore shouldn't stop deleter being called.
the end goal obtain smart pointer doesn't consisant heap allocation - or @ least small number.
from i've read, shared_ptr not de-allocated until both strong references , weak references dropped.
wrong. std::shared_ptr
has 2 blocks - control block contains reference counts, etc, block actual data.
when shared count goes 0, data block (usually) deallocated. why illegal make std::shared_ptr
expired std::weak_ptr
.
on note, why ever want functionality? ruins entire point of std::weak_ptr
, able "store" pointer object stored std::shared_ptr
without incrementing reference count. if want that, please use std::shared_ptr
Comments
Post a Comment