[ weak_ptr ]

The weak_ptr<> is class template that can be used in place of shared_ptr<> to break cycles in data structures.

Constructor:

#include <tr1/memory>
using std::tr1::weak_ptr;

weak_ptr<int> wp1;
weak_ptr<int> wp2(wp1);
weak_ptr<int> wp3(sp1);

Member function lock() and constructors:


shared_ptr<int> sp2(wp2);               // throw if wp is expired
shared_ptr<int> sp2 = wp2.lock();       // return a default-constructed sp if wp is expired

Member function expired:

return true if the weak_ptr is expired:

bool weak_ptr<Ty>::expired() const;

if (wp.expired()) 
{ ... }

Member function use_count:

return the number of shared_ptr that own the resource:

long weak_ptr<Ty>::use_count() const;
wp.use_count();

Member function operator=:


weak_ptr & 
weak_ptr::operator=(const weak_ptr &wp);

template <class O>
weak_ptr & 
weak_ptr::operator=(const weak_ptr<O> &wp);

template <class O>
weak_ptr & 
weak_ptr::operator=(shared_ptr<O> &sp);

Member function operator<:


The function implements a strict weak ordering: !(lhs < rhs) && !(rhs <lhs) is true only if lhs and rhs control the same resource.

template<class T1, class T2>
bool operator< (const weak_ptr<T1> & lhs, const weak_ptr<T2> &rhs);

Member function reset and swap:

        
void weak_ptr<Ty>::reset() const;        
void weak_ptr<Ty>::swap(weak_ptr<Ty> &rh);


Comments