c++ - Upgradeable pointer (unique_ptr -> shared_ptr) -


i'm told shared_ptr slower unique_ptr, should strive use unique_ptr's when possible. have potential use case 1 object have single owner majority of time, may have multiple owners in scenarios. in case, there sort of implementation of unique_ptr can upgraded shared_ptr when needed instead of having declared shared_ptr in first place?

ie instead of using shared_ptr, more beneficial if used following if case of upgrading rare, or there pitfall i'm not seeing here?

class upgrade_ptr<t> {   shared_ptr<t> shared_;   unique_ptr<t> unique_;   upgrade_ptr(t obj) {     unique_ = unique_ptr<t>(obj);   }   shared_ptr<t> share() {     if (shared_ == nullptr) {       shared_ = shared_ptr<t>(unique_);     }     return shared_;   } } 


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -