std::enable...ed_from_this(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

hey bestie πŸ’– this is the std::enable...ed_from_this(3) page, and it lives in the C++ Standard Libary manpage set (yes, "Libary" is what the header says β€” we're keeping the typo, it's iconic πŸ’…).

std::enable_shared_from_this::enable_shared_from_this - std::enable_shared_from_this::enable_shared_from_this

periodt. that's the constructor-page full name, and the NAME line is literally that same path twice with a dash between them. πŸŽ€

✨ Synopsis ✨

okay so there are two constructor overloads in the synopsis, and both are tagged (since C++11). here's the synopsis verbatim, because bestie we do not mess with signatures:

constexpr enable_shared_from_this() noexcept;
(1) (since C++11)
enable_shared_from_this( const enable_shared_from_this& other )
(2) (since C++11)
noexcept;

The description is simple: it constructs a new enable_shared_from_this object. The exposition-only std::weak_ptr<T> member weak-this is value-initialized. That's it, that's the construction lore. ✨

🫢 Parameters 🫢

  • other β€” an enable_shared_from_this to copy. (the one and only parameter, bestie 🫢)

πŸ’€ Notes πŸ’€

there is no move constructor: moving from an object derived from enable_shared_from_this does not transfer its shared identity. so don't expect the shared identity to come with you when you move β€” it literally does not. 😭

🌈 Example 🌈

this example is giving main character energy. run it if you want:

// Run this code

#include <memory>

struct Foo : public std::enable_shared_from_this<Foo>
{
    Foo() {} // implicitly calls enable_shared_from_this constructor
    std::shared_ptr<Foo> getFoo() { return shared_from_this(); }
};

int main()
{
    std::shared_ptr<Foo> pf1(new Foo);
    auto pf2 = pf1->getFoo(); // shares ownership of object with pf1
}

πŸ‘‘ See also πŸ‘‘

  • shared_ptr β€” smart pointer with shared object ownership semantics (C++11) (class template). iykyk

that's the whole page, bestie 🫢 source: http://cppreference.com · date: 2024.06.10 · page: std::enable...ed_from_this(3).

πŸ“¦ where this page comes from