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)
[edit] 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)
- if it's not an established usage pattern (I don't think it is, I checked Debian Code search and found two cases of deriving from
enable_shared_from_this<const T>
: getfem and pdns), it seems excessive for an already large example dealing with intended use. As far as const T issues, it *might* be worth noting that callingshared_from_this
in a const member function pops ashared_ptr<const T>
(because it surprises some programmers - seen that on reddit and stackoverflow), but that probably belongs in cpp/memory/enable_shared_from_this/shared_from_this anyway. --Cubbi (talk) 12:48, 13 July 2023 (PDT)