#include <memory>

struct Base : std::enable_shared_from_this<Base>
{};


struct Derived : Base
{
        std::shared_ptr<Derived> shared_from_this() { return std::static_pointer_cast<Derived>(Base::shared_from_this()); }
};

main()
{
  std::shared_ptr<Derived> ptr(new Derived);

  std::shared_ptr<Derived> d_ptr = ptr->shared_from_this();

  std::shared_ptr<Base> b_ptr = ptr->shared_from_this();
}