std::weak_ptr::reset(3)

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

πŸ’… std::weak_ptr::reset(3) πŸ’…

okay so bestie, this is a page straight outta the C++ Standard Library β€” that's the std family, the weak_ptr branch specifically, and today we're talking about reset. yes, that reset. the little no-args king that just... lets go. 🫢


✨ NAME ✨

std::weak_ptr::reset β€” and yes, the name is literally just std::weak_ptr::reset, she said "my name is my name" and honestly? iconic. πŸ‘‘


🌸 Synopsis 🌸

void reset() noexcept;  (since C++11)

so there's no parameters, no overloads, nothing extra β€” just vibes and noexcept. she's been around (since C++11) and she has never once thrown an exception. lowkey a role model. ✨


πŸ¦‹ Description πŸ¦‹

Here's the tea, bestie: this function releases the reference to the managed object. that's it, that's the whole plot. πŸŽ€

After the call, *this manages no object. empty. free. unbothered. main character energy but make it a null weak pointer. πŸ’…

ngl it's giving "block and delete" but for a reference count. respectfully. no cap.

πŸ“ Parameters πŸ“

  • (none) β€” she takes nothing. zero arguments. she doesn't need them. πŸ™ˆ

πŸ’œ Return value πŸ’œ

  • (none) β€” and she returns nothing either. pure action. no receipts. 🧚

🌈 Example 🌈

// Run this code

#include <iostream>
#include <memory>

int main()
{
    auto shared = std::make_shared<int>(), shared2 = shared, shared3 =
shared2;

    auto weak = std::weak_ptr<int>{shared};

    std::cout << std::boolalpha
              << "shared.use_count(): " << shared.use_count() << '\n'
              << "weak.use_count(): " << weak.use_count() << '\n'
              << "weak.expired(): " << weak.expired() << '\n';

    weak.reset();

    std::cout << "weak.reset();\n"
              << "shared.use_count(): " << shared.use_count() << '\n'
              << "weak.use_count(): " << weak.use_count() << '\n'
              << "weak.expired(): " << weak.expired() << '\n';
}

like look at her go β€” three shared_ptrs holding hands (shared, shared2, shared3), one lil weak observer watching from the group chat, and then she hits weak.reset() and just πŸ‘‹ dips. the shared ones don't even notice (their count stays 3, slay), but the weak one goes from 3 to 0 and expired() flips to true. ate. πŸ”₯


⭐ Output ⭐

shared.use_count(): 3
weak.use_count(): 3
weak.expired(): false
weak.reset();
shared.use_count(): 3
weak.use_count(): 0
weak.expired(): true

the before/after is genuinely so satisfying to look at, i'm obsessed. πŸ˜­πŸ’–


πŸ‘‘ See also πŸ‘‘

  • expired β€” checks whether the referenced object was already deleted (public member function) β€” basically the vibe check that tells you if the thing you're weakly watching is gone. πŸ’€

okay that's the whole page, no gatekeeping here πŸ’… fr fr thanks for reading bestie, mwah 🫢

this lil writeup came to you from http://cppreference.com, dated 2024.06.10, and it's the std::weak_ptr::reset(3) page β€” catch you in the next manpage, bye bestie! πŸŒΈβœ¨πŸŽ€

πŸ“¦ where this page comes from