diff --git a/include/boost/enable_shared_from_this.hpp b/include/boost/enable_shared_from_this.hpp index 4dfdab6..d3f8ab8 100644 --- a/include/boost/enable_shared_from_this.hpp +++ b/include/boost/enable_shared_from_this.hpp @@ -24,6 +24,25 @@ namespace boost template class enable_shared_from_this { +protected: + + enable_shared_from_this() + { + } + + enable_shared_from_this(enable_shared_from_this const &) + { + } + + enable_shared_from_this & operator=(enable_shared_from_this const &) + { + return *this; + } + + ~enable_shared_from_this() + { + } + public: shared_ptr shared_from_this() diff --git a/test/shared_from_this_test.cpp b/test/shared_from_this_test.cpp index d5dbe6e..2b40363 100644 --- a/test/shared_from_this_test.cpp +++ b/test/shared_from_this_test.cpp @@ -67,18 +67,20 @@ void test() } void test2(); +void test3(); int main() { test(); test2(); + test3(); return boost::report_errors(); } -// virtual inheritance from Y to stress the implementation -// (prevents Y* -> impl* casts) +// virtual inheritance to stress the implementation +// (prevents Y* -> impl*, enable_shared_from_this* -> impl* casts) -class impl: public X, public virtual Y, public boost::enable_shared_from_this +class impl: public X, public virtual Y, public virtual boost::enable_shared_from_this { public: @@ -110,3 +112,41 @@ void test2() { boost::shared_ptr pi(static_cast(0)); } + +// + +struct V: public boost::enable_shared_from_this +{ +}; + +void test3() +{ + boost::shared_ptr p(new V); + + boost::shared_ptr q = p->shared_from_this(); + BOOST_TEST(p == q); + BOOST_TEST(!(p < q) && !(q < p)); + + V v2(*p); + + try + { + boost::shared_ptr r = v2.shared_from_this(); + BOOST_ERROR("v2.shared_from_this() failed to throw"); + } + catch(boost::bad_weak_ptr const &) + { + } + + try + { + *p = V(); + boost::shared_ptr r = p->shared_from_this(); + BOOST_TEST(p == r); + BOOST_TEST(!(p < r) && !(r < p)); + } + catch(boost::bad_weak_ptr const &) + { + BOOST_ERROR("p->shared_from_this() threw bad_weak_ptr after *p = V()"); + } +}