Topic: determining if any weak_ptrs are created from a given shared_ptr.


Author: cbarron413@adelphia.net (Carl Barron)
Date: Thu, 27 Jul 2006 03:56:25 GMT
Raw View
   I believe that the mutual interaction of weak_ptr and shared_ptr
imply that all weak_ptrs created from the same shared_ptr or copy of
that shared_ptr 'share' the same control implementation. If so how
do we determine if there are any weak ptrs associated with a given
shared ptr?  Unique tells me that this is the only shared ptr
associated with the T * it contains. [use_count() == 1] how
to determine the number [actually only are there any? is enough]
weak ptrs associated with this shared_ptr or any of its copies?

   struct DAG
   {
      weak_ptr<DAG> left,right;
      int data;
   };

   std::list<shared_ptr<DAG> > nodes;

   bool kill_this_one(shared_ptr<DAG> &x)
   {
      return x.unique() && x_has_no_weak_ptrs;
   }

   void clean_up()
   {
      nodes.remove_if(kill_this_one);
   }
   weak_ptr<DAG> create_DAG(weak_ptr<DAG> a,weak_ptr<DAG> b,int c)
   {
      shared_ptr<DAG> sp(new DAG);
      sp->left = a;
      sp->right = b;
      sp ->data = c;
      nodes.push_back(p);
      return weak_ptr<DAG>(p);
   }

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]