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::use_count(3) C++ Standard Libary std::shared_ptr::use_count(3)

std::shared_ptr::use_count - std::shared_ptr::use_count


long use_count() const noexcept;


Returns the number of different shared_ptr instances (this included) managing the
current object. If there is no managed object, 0 is returned.


In multithreaded environment, the value returned by use_count is approximate
(typical implementations use a memory_order_relaxed load)


(none)


the number of std::shared_ptr instances managing the current object or 0 if
there is no managed object.


Common use cases include


* comparison with 0. If use_count returns zero, the shared pointer is empty
and manages no objects (whether or not its stored pointer is nullptr). In
multithreaded environment, this does not imply that the destructor of the
managed object has completed.
* comparison with 1. If use_count returns 1, there are no other owners.
The
deprecated
(since C++17) member function unique() is provided for this use case.
(until C++20) In multithreaded environment, this does not imply that the object
is safe to modify because accesses to the managed object by former shared owners
may not have completed, and because new shared owners may be introduced
concurrently, such as by std::weak_ptr::lock.

// Run this code


#include <memory>
#include <iostream>


void fun(std::shared_ptr<int> sp)
{
std::cout << "in fun(): sp.use_count() == " << sp.use_count()
<< " (object @ " << sp << ")\n";
}


int main()
{
auto sp1 = std::make_shared<int>(5);
std::cout << "in main(): sp1.use_count() == " << sp1.use_count()
<< " (object @ " << sp1 << ")\n";


fun(sp1);
}


in main(): sp1.use_count() == 1 (object @ 0x20eec30)
in fun(): sp.use_count() == 2 (object @ 0x20eec30)


unique checks whether the managed object is managed only by the current
(until C++20) shared_ptr instance
(public member function)

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.