From 5dd2c621327b68b5412e17fa49a932ff8f5690de Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 23 Nov 2002 12:47:38 +0000 Subject: [PATCH] weak_ptr converting constructor removed; operator< can now take different types to aid ownership tests. [SVN r16376] --- include/boost/shared_ptr.hpp | 4 ++-- include/boost/weak_ptr.hpp | 25 ++++++------------------- shared_ptr_test.cpp | 12 +++--------- 3 files changed, 11 insertions(+), 30 deletions(-) diff --git a/include/boost/shared_ptr.hpp b/include/boost/shared_ptr.hpp index 07ca043..6ffca27 100644 --- a/include/boost/shared_ptr.hpp +++ b/include/boost/shared_ptr.hpp @@ -272,7 +272,7 @@ public: pn.swap(other.pn); } - bool _internal_less(this_type const & rhs) const // implementation detail, never throws + template bool _internal_less(shared_ptr const & rhs) const { return pn < rhs.pn; } @@ -321,7 +321,7 @@ template inline bool operator!=(shared_ptr const & a, shared_ptr #endif -template inline bool operator<(shared_ptr const & a, shared_ptr const & b) +template inline bool operator<(shared_ptr const & a, shared_ptr const & b) { return a._internal_less(b); } diff --git a/include/boost/weak_ptr.hpp b/include/boost/weak_ptr.hpp index cce5cb8..03e3800 100644 --- a/include/boost/weak_ptr.hpp +++ b/include/boost/weak_ptr.hpp @@ -45,7 +45,7 @@ public: // -// The simple implementation of the converting constructor: +// The "obvious" converting constructor implementation: // // template // weak_ptr(weak_ptr const & r): px(r.px), pn(r.pn) // never throws @@ -60,23 +60,10 @@ public: // It is not possible to avoid spurious access violations since // in multithreaded programs r.px may be invalidated at any point. // -// A safe conversion between weak_ptr and weak_ptr must involve -// a temporary shared_ptr. +// A weak_ptr can safely be obtained from a weak_ptr by using // -// It is not yet clear whether the converting constructor should be -// present at all, or it's better to require an explicit make_shared call. +// weak_ptr wpt = make_shared(wpu); // -// The only case for the converting constructor is the weak_ptr -> weak_ptr -// conversion. -// - - template - weak_ptr(weak_ptr const & r) // never throws - { - shared_ptr tmp = make_shared(r); - px = tmp.px; - pn = tmp.pn; - } template weak_ptr(shared_ptr const & r): px(r.px), pn(r.pn) // never throws @@ -116,13 +103,13 @@ public: pn.swap(other.pn); } - void _internal_assign(T * px2, detail::shared_count const & pn2) // implementation detail + void _internal_assign(T * px2, detail::shared_count const & pn2) { px = px2; pn = pn2; } - bool _internal_less(this_type const & rhs) const // implementation detail, never throws + template bool _internal_less(weak_ptr const & rhs) const { return pn < rhs.pn; } @@ -144,7 +131,7 @@ private: }; // weak_ptr -template inline bool operator<(weak_ptr const & a, weak_ptr const & b) +template inline bool operator<(weak_ptr const & a, weak_ptr const & b) { return a._internal_less(b); } diff --git a/shared_ptr_test.cpp b/shared_ptr_test.cpp index cbaaeed..99ba2de 100644 --- a/shared_ptr_test.cpp +++ b/shared_ptr_test.cpp @@ -773,11 +773,6 @@ void weak_ptr_constructor() catch(boost::bad_weak_ptr) { } - - { - boost::weak_ptr wp2(wp); - boost::weak_ptr wp3(wp); - } } void auto_ptr_constructor() @@ -2256,13 +2251,13 @@ template void test_ne(T const & a, T const & b) BOOST_TEST(!(a < b && b < a)); } -void test_shared(boost::weak_ptr const & a, boost::weak_ptr const & b) +template void test_shared(boost::weak_ptr const & a, boost::weak_ptr const & b) { BOOST_TEST(!(a < b)); BOOST_TEST(!(b < a)); } -void test_nonshared(boost::weak_ptr const & a, boost::weak_ptr const & b) +template void test_nonshared(boost::weak_ptr const & a, boost::weak_ptr const & b) { BOOST_TEST(a < b || b < a); BOOST_TEST(!(a < b && b < a)); @@ -2381,7 +2376,7 @@ void test() BOOST_TEST(wp3.use_count() == 1); test_shared(wp2, wp3); - weak_ptr wp4(wp3); + weak_ptr wp4 = boost::make_shared(wp3); BOOST_TEST(wp4.use_count() == 1); test_shared(wp2, wp4); @@ -2390,7 +2385,6 @@ void test() test_is_zero(boost::make_shared(wp1)); wp1 = p4; - wp1 = wp3; wp1 = wp2; BOOST_TEST(wp1.use_count() == 1);