Share via


weak_ptr::use_count

Counts number of designated shared_ptr objects.

long use_count() const;

Remarks

The member function returns the number of shared_ptr objects that own the resource pointed to by *this.

Example

 

// std_tr1__memory__weak_ptr_use_count.cpp 
// compile with: /EHsc 
#include <memory> 
#include <iostream> 
 
int main() 
    { 
    std::shared_ptr<int> sp1(new int(5)); 
    std::weak_ptr<int> wp(sp1); 
    std::cout << "wp.use_count() == " 
        << wp.use_count() << std::endl; 
 
    std::shared_ptr<int> sp2(sp1); 
    std::cout << "wp.use_count() == " 
        << wp.use_count() << std::endl; 
 
    return (0); 
    } 
 
wp.use_count() == 1
wp.use_count() == 2

Requirements

Header: <memory>

Namespace: std

See Also

Reference

weak_ptr Class