Namespaces
Variants
Views
Actions

std::shared_ptr::use_count

From cppreference.com
< cpp‎ | memory‎ | shared ptr
Revision as of 15:35, 2 November 2012 by P12bot (Talk | contribs)

 
 
 
 
 
long use_count() const;

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

Contents

Parameters

(none)

Return value

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

Example

#include <memory> 
#include <iostream> 
 
void fun(std::shared_ptr<int> sp)
{
    std::cout << "fun: sp.use_count() == " << sp.use_count() << std::endl; 
}
 
int main() 
{ 
    std::shared_ptr<int> sp1 {std::make_shared<int>(5)};
    std::cout << "sp1.use_count() == " << sp1.use_count() << std::endl; 
 
    fun(sp1);
}

Output:

sp1.use_count() == 1
fun: sp.use_count() == 2

See also

Template:cpp/memory/shared ptr/dcl list unique