Category : enable-shared-from-this

I would like to enforce the following in a particular codebase on classes that inherit from std::enable_shared_from_this classes that inherit from enable_shared_from_this are final constructors and assignment operators aren’t public (as there should instead be a factory function that returns a shared_ptr) classes that inherit from enable_shared_from_this inherit publicly Is this possible? Is this sensible ..

Read more

I understand that most std library implementations choose to implement std::enable_shared_from_this by storing a std::weak_ptr in the base class. This leads to the following: #include <memory> class Foo : public std::enable_shared_from_this<Foo> { public: std::shared_ptr<Foo> GetSharedBar() const { // returns shared_ptr<const Foo> instead of std::shared_ptr<Foo> return shared_from_this(); // Error in VC++2019 due to mismatch. } }; ..

Read more