GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::shared_ptr::~shared_ptr(3) C++ Standard Libary std::shared_ptr::~shared_ptr(3)

std::shared_ptr::~shared_ptr - std::shared_ptr::~shared_ptr


~shared_ptr();


If *this owns an object and it is the last shared_ptr owning it, the object is
destroyed through the owned deleter.


After the destruction, the smart pointers that shared ownership with *this, if any,
will report a use_count() that is one less than its previous value.


Unlike std::unique_ptr, the deleter of std::shared_ptr is invoked even if the
managed pointer is null.

// Run this code


#include <memory>
#include <iostream>


struct S {
S() { std::cout << "S::S()\n"; }
~S() { std::cout << "S::~S()\n"; }
struct Deleter {
void operator()(S* s) const {
std::cout << "S::Deleter()\n";
delete s;
}
};
};


int main()
{
auto sp = std::shared_ptr<S>{ new S, S::Deleter{} };


auto use_count = [&sp](char c) {
std::cout << c << ") use_count(): " << sp.use_count() << '\n';
};


use_count('A');
{
auto sp2 = sp;
use_count('B');
{
auto sp3 = sp;
use_count('C');
}
use_count('D');
}
use_count('E');


// sp.reset();
// use_count('F'); // would print "F) use_count(): 0"
}


S::S()
A) use_count(): 1
B) use_count(): 2
C) use_count(): 3
D) use_count(): 2
E) use_count(): 1
S::Deleter()
S::~S()


destructor destroys a weak_ptr
(public member function of std::weak_ptr<T>)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.