+template<typename T>
+class WeakPtr
+{
+ template<typename U> friend class RefPtr;
+ template<typename U> friend class WeakPtr;
+
+private:
+ T *data;
+ RefCounts *counts;
+
+public:
+ WeakPtr(): data(0), counts(0) { }
+private:
+ WeakPtr(T *d, RefCounts *c): data(d), counts(d ? c : 0) { incref(); }
+
+public:
+ WeakPtr(const WeakPtr &p): data(p.get()), counts(data ? p.counts : 0) { incref(); }
+
+ template<typename U>
+ WeakPtr(const WeakPtr<U> &p): data(p.get()), counts(data ? p.counts : 0) { incref(); }
+
+ template<typename U>
+ WeakPtr(const RefPtr<U> &p): data(p.data), counts(p.counts) { incref(); }
+
+ WeakPtr &operator=(const WeakPtr &p) { return assign(p); }
+
+ template<typename U>
+ WeakPtr &operator=(const WeakPtr<U> &p) { return assign(p); }
+
+ template<typename U>
+ WeakPtr &operator=(const RefPtr<U> &p) { return assign(WeakPtr(p)); }
+
+private:
+ template<typename U>
+ WeakPtr &assign(const WeakPtr<U> &);
+
+ T *get() const { return (counts && counts->count ? data : 0); }
+ void incref() { if(counts) ++counts->weak_count; }
+ void decref();
+};
+
+