On the other hand, in C++11 there is std::shared_ptr and std::weak_ptr. Our code precedes C++11, that's why we are having our own smart pointer class. Our RefCountedPtr is intrusive, which std::shared_ptr is not, and we are exploiting this property in our code (i.e. we create other RefCountedPtr instances from bare pointers). However, this is also possible with C++11 by inheriting from std::enable_shared_from_this (which I just discovered, I really wish I had known this before "no_global_galaxy").
I'm wondering if we should start migrating to the C++11 way enabling us to use weak pointers. I had to use some hacks to break cycles in the "No global galaxy" code and I would rather get rid of them sooner or later. There is no need to migrate all RefCounted classes at once. Migration could be done class-by-class as needed. For each class it would involve:
- Inheriting from std::enable_shared_from_this instead of from RefCounted
- Change all RefCountedPtr<Class> to std::shared_ptr<Class>
- Change all constructions of smart pointers from bare pointers (except for the first creation) from a constructor call or
Code: Select all
smart_pointer = std::shared_ptr<Class>(bare_pointer)
toCode: Select all
smart_pointer = make_shared(bare_pointer)
Code: Select all
smart_pointer = bare_pointer->shared_from_this()