Talk:cpp/memory/enable shared from this
While the example does publicly inherit, it's a bit deceptive. I missed it the first time I read the example, and some guy in the edit history did too. Maybe should be more explicit by putting the public keyword in? 203.6.69.2 16:18, 4 March 2018 (PST)
Valid T types?
Experimenting on Godbolt, it appears that I can do struct S : std::enable_shared_from_this<const S> {};
, allowing s->shared_from_this()
to only produce a std::shared_ptr<const S>
, which is nice. Also, it appears I can do
struct X; struct B : std::enable_shared_from_this<X> {}; struct X : B {};
which leads to B
being constructible outside the context of shared pointers, but as soon as you turn it into a shared pointer with std::make_shared<B>()
or std::shared_ptr<B>(new B)
, it won't compile (very reasonable!), but you can std::make_shared<X>()
and std::shared_ptr<B>(new X)
.
Are these behaviors guaranteed? Should we add such examples? BenFrantzDale (talk) 07:06, 13 July 2023 (PDT)