weak pointer Member functions use_count, reset, expired, lock, swap

Among the weak pointer member functions use_count, reset, expired, lock, swap, etc. Some of them are similar to the shared_ptr member functions. The function name which is similar to the name of the shared_ptr function also perform the same operations. This is expected because weak_ptr purpose is to manage the memory of shared_ptr -although it has no direct ownership over the storage- and see that code using shared_ptr does not render any unexpected output. And weak_ptr does so through it’s member functions. Here we will see all the member functions of weak_ptr which are similar to shared_ptr and which are not.

Link :C++11 weak pointer

i)use_count()

use_count() gives the number of shared_ptr currently pointing to the storage which the weak_ptr points to.

shared_ptr<char<sp(new char(‘B’)) , sp1(sp) ;
weak_ptr<char> wp(sp) ;

cout<< wp.use_count() ;

The output is 2.


ii)reset()

The purpose of reset() function is to relinquish weak_ptr from the memory it manages.So if a reset() function is called the weak_ptr will no longer point to any storage.

shared_ptr<int> sp(new int(789) );
weak_ptr<int> wp(sp);

wp.reset() ;

cout<< wp.use_count() ;

The output is 0


iii)expired()

This function will return true if the use_count() is 0.So the underlying meaning of expired() is it returns true only when the weak_ptr points to some storage and when it does not-when it is a doodler- it will return false.

shared_ptr<string>sp(new string(“Shoes and games”);

weak_ptr<string>wp(sp) , wp1 ;

cout< wp.expired() << endl //gives true
 << wp1.expired() ; //gives false


iv)lock()

If the weak_ptr expired() function returns true then lock( ) will return NULL but if the expired() function returns false then lock( ) will return a shared_ptr to the object which is managed by the weak_ptr.

weak_ptr<string>wp(sp) ;

if( auto sp1=wp.lock() )
{
cout<< sp1.use_count() ;
}

v)swap()

The swap() function is a non-member function of weak_ptr.It’s function is to simply swap the memory of the two weak_ptr passed as arguments.

shared_ptr<string>sSp(new string(“Games n fun”)) , sSp1(sSp) ;

weak_ptr<string> sWp(sSp) ,wp(sp) ;

cout<< sWp.use_count() << endl //gives 2
 << wp.use_count() ; //gives 1

vi)owner_before( )

This function will accept either a weak_ptr or a shared_ptr as its argument and the return type is bool. As the name states it checks if the weak_ptr points to the storage of the argument passed formerly.The function returns true if the weak_ptr point to the storage formerly else it returns false.

shared_ptr<int>sp4(new int(90)) ;

weak_ptr<int>wp2(sp4) , wp3=wp2 ;

cout<< wp2.owner_before(wp3) ;
 << wp3.owner_before(wp2) ;

wp3.reset( ) ;

cout << wp3.owner_before(sp4) << endl
  << wp3.owner_before(wp2) << endl ;

The output is

0
0
1
1

After reset() is called on wp3 it does not point to any storage but note that it is a former pointer to the storage of sp4 and wp2.So the output of the owner_before() is true after reset() is called.