From da81452f1f4161c45ec1fead6cb92577d6f21188 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 2 Apr 2020 02:16:59 +0300 Subject: [PATCH] Make shared_from_this and weak_from_this private in enable_shared_from. Fixes #75. --- include/boost/smart_ptr/enable_shared_from.hpp | 10 +++++++--- test/Jamfile | 3 +++ test/shared_from_fail.cpp | 16 ++++++++++++++++ test/weak_from_fail.cpp | 16 ++++++++++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 test/shared_from_fail.cpp create mode 100644 test/weak_from_fail.cpp diff --git a/include/boost/smart_ptr/enable_shared_from.hpp b/include/boost/smart_ptr/enable_shared_from.hpp index db347e6..be88b30 100644 --- a/include/boost/smart_ptr/enable_shared_from.hpp +++ b/include/boost/smart_ptr/enable_shared_from.hpp @@ -3,7 +3,7 @@ // enable_shared_from.hpp // -// Copyright 2019 Peter Dimov +// Copyright 2019, 2020 Peter Dimov // // Distributed under the Boost Software License, Version 1.0. // See accompanying file LICENSE_1_0.txt or copy at @@ -19,17 +19,21 @@ namespace boost class enable_shared_from: public enable_shared_from_this { +private: + + using enable_shared_from_this::shared_from_this; + using enable_shared_from_this::weak_from_this; }; template shared_ptr shared_from( T * p ) { - return shared_ptr( p->enable_shared_from::shared_from_this(), p ); + return shared_ptr( p->enable_shared_from_this::shared_from_this(), p ); } template weak_ptr weak_from( T * p ) BOOST_SP_NOEXCEPT { - return weak_ptr( p->enable_shared_from::weak_from_this(), p ); + return weak_ptr( p->enable_shared_from_this::weak_from_this(), p ); } } // namespace boost diff --git a/test/Jamfile b/test/Jamfile index 8e121b8..c62fd0b 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -356,3 +356,6 @@ run allocate_unique_value_test.cpp ; run sp_guides_test.cpp ; run sp_guides_test2.cpp ; run wp_guides_test.cpp ; + +compile-fail shared_from_fail.cpp ; +compile-fail weak_from_fail.cpp ; diff --git a/test/shared_from_fail.cpp b/test/shared_from_fail.cpp new file mode 100644 index 0000000..b7275a7 --- /dev/null +++ b/test/shared_from_fail.cpp @@ -0,0 +1,16 @@ +// Copyright 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include + +struct X: public boost::enable_shared_from +{ +}; + +int main() +{ + boost::shared_ptr px( new X ); + px->shared_from_this(); +} diff --git a/test/weak_from_fail.cpp b/test/weak_from_fail.cpp new file mode 100644 index 0000000..5d75e40 --- /dev/null +++ b/test/weak_from_fail.cpp @@ -0,0 +1,16 @@ +// Copyright 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include +#include + +struct X: public boost::enable_shared_from +{ +}; + +int main() +{ + boost::shared_ptr px( new X ); + px->weak_from_this(); +}