std::enable...d_from_this(3)
π NAME
std::enable_shared_from_this::shared_from_this β yes bestie, that's std::enable_shared_from_this::shared_from_this, the one and only π
Ok so real quick, this page lives in the C++ Standard Library section of the manual β it's std::enable...d_from_this(3), the truncated-looking one. Don't let the name scare you, it's giving "long title, simple job" energy.
β¨ Synopsis β¨
std::shared_ptr<T> shared_from_this(); (1) (since C++11)
std::shared_ptr<T const> shared_from_this() const; (2) (since C++11)
So what does it do? It returns a std::shared_ptr<T> that shares ownership of *this with all the existing std::shared_ptrs that refer to *this. Like, it's the "hey, I'm already owned, let me hand you a proper share of me" move π«Ά
Effectively it executes std::shared_ptr<T>(weak_this), where weak_this is the exposition-only mutable std::weak_ptr<T> member of enable_shared_from_this. Yes, exposition-only, no cap β it's a real member that the standard just describes rather than names for you π
π Return value
A std::shared_ptr<T> that shares ownership of *this with pre-existing std::shared_ptrs. That's it, that's the tea π
π Exceptions
Listen closely bestie, this is the important one:
Ifshared_from_thisis called on an object that is not previously shared bystd::shared_ptr,std::bad_weak_ptris thrown β and it's thrown by theshared_ptrconstructor from a default-constructedweak_this. So like, don't call this on a stack object and expect it to slay. It will not. It will throw π¨
πΈ Example
// Run this code
#include <iostream>
#include <memory>
struct Foo : public std::enable_shared_from_this<Foo>
{
Foo() { std::cout << "Foo::Foo\n"; }
~Foo() { std::cout << "Foo::~Foo\n"; }
std::shared_ptr<Foo> getFoo() { return shared_from_this(); }
};
int main()
{
Foo *f = new Foo;
std::shared_ptr<Foo> pf1;
{
std::shared_ptr<Foo> pf2(f);
pf1 = pf2->getFoo(); // shares ownership of object with pf2
}
std::cout << "pf2 is gone\n";
}
β¨ Output:
Foo::Foo
pf2 is gone
Foo::~Foo
Look at that destructor firing after pf2 is gone β because pf1 is still holding on, bestie. Shared ownership, iconic behaviour π₯
π¦ Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. (Retroactive glow-ups, we love to see it π )
LWG 2529β applied to C++11.- Behavior as published: calling
shared_from_thison an unshared object was undefined behavior. - Correct behavior:
std::bad_weak_ptris thrown. Much better. Way less "hope for the best" energy π
- Behavior as published: calling
π See also
shared_ptrβ smart pointer with shared object ownership semantics (C++11) (class template)
Ok that's the whole page, go forth and share your ownership responsibly π§π
Sign-off szn: this lil masterpiece came from http://cppreference.com, dated 2024.06.10, and it's filed under std::enable...d_from_this(3). K bye, ily, stan smart pointers πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms