I know that I can "promote" weak_ptr
to shared_ptr
like that:
// std::weak_ptr<T> weak;
std::shared_ptr<T> promoted(weak);
My question is: can that be prevented somehow?
As an exercise I wanted to create my own veeery simple implementation of a WeakPtrFactory. It is initiated with this
as a class member, and then spawn weak_ptr
s which will be invalidated on object's destruction. My attempt simply used shared_ptr
as private member and returned weak_ptr
s created with it (no op deleter is passed just in case). But it has obvious disadvantage since everyone can just promote them back to shared_ptr
and break whole mechanism.