std::weak_ptr::reset(3)
π 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
- 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