From 7b9a96921515dc0e76ac3bf2c3170e25600e14f9 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 2 Apr 2020 00:28:02 +0300 Subject: [PATCH 01/10] Add deduction guides to shared_ptr and weak_ptr. Fixes #73. --- include/boost/smart_ptr/shared_ptr.hpp | 7 +++++++ include/boost/smart_ptr/weak_ptr.hpp | 6 ++++++ test/Jamfile | 4 ++++ test/sp_guides_test.cpp | 25 +++++++++++++++++++++++++ test/sp_guides_test2.cpp | 23 +++++++++++++++++++++++ test/wp_guides_test.cpp | 24 ++++++++++++++++++++++++ 6 files changed, 89 insertions(+) create mode 100644 test/sp_guides_test.cpp create mode 100644 test/sp_guides_test2.cpp create mode 100644 test/wp_guides_test.cpp diff --git a/include/boost/smart_ptr/shared_ptr.hpp b/include/boost/smart_ptr/shared_ptr.hpp index 7237a7d..0405ab0 100644 --- a/include/boost/smart_ptr/shared_ptr.hpp +++ b/include/boost/smart_ptr/shared_ptr.hpp @@ -1175,6 +1175,13 @@ template D const * basic_get_local_deleter( D const *, shared_ } // namespace detail +#if defined(__cpp_deduction_guides) + +template shared_ptr( weak_ptr ) -> shared_ptr; +template shared_ptr( std::unique_ptr ) -> shared_ptr; + +#endif + } // namespace boost #if defined( BOOST_SP_DISABLE_DEPRECATED ) diff --git a/include/boost/smart_ptr/weak_ptr.hpp b/include/boost/smart_ptr/weak_ptr.hpp index 5230f35..07ba189 100644 --- a/include/boost/smart_ptr/weak_ptr.hpp +++ b/include/boost/smart_ptr/weak_ptr.hpp @@ -264,6 +264,12 @@ template void swap(weak_ptr & a, weak_ptr & b) BOOST_SP_NOEXCEPT a.swap(b); } +#if defined(__cpp_deduction_guides) + +template weak_ptr( shared_ptr ) -> weak_ptr; + +#endif + } // namespace boost #endif // #ifndef BOOST_SMART_PTR_WEAK_PTR_HPP_INCLUDED diff --git a/test/Jamfile b/test/Jamfile index f055d21..8e121b8 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -352,3 +352,7 @@ run allocate_unique_noinit_test.cpp ; run allocate_unique_test.cpp ; run allocate_unique_throws_test.cpp ; run allocate_unique_value_test.cpp ; + +run sp_guides_test.cpp ; +run sp_guides_test2.cpp ; +run wp_guides_test.cpp ; diff --git a/test/sp_guides_test.cpp b/test/sp_guides_test.cpp new file mode 100644 index 0000000..a7c6ff4 --- /dev/null +++ b/test/sp_guides_test.cpp @@ -0,0 +1,25 @@ +// Copyright 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#if defined(__cpp_deduction_guides) + +#include +#include + +int main() +{ + boost::shared_ptr p1( new int ); + boost::weak_ptr p2( p1 ); + boost::shared_ptr p3( p2 ); +} + +#else + +#include + +BOOST_PRAGMA_MESSAGE( "Skipping test because __cpp_deduction_guides is not defined" ) + +int main() {} + +#endif diff --git a/test/sp_guides_test2.cpp b/test/sp_guides_test2.cpp new file mode 100644 index 0000000..e7ccfbc --- /dev/null +++ b/test/sp_guides_test2.cpp @@ -0,0 +1,23 @@ +// Copyright 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#if defined(__cpp_deduction_guides) + +#include +#include + +int main() +{ + boost::shared_ptr p2( std::unique_ptr( new int ) ); +} + +#else + +#include + +BOOST_PRAGMA_MESSAGE( "Skipping test because __cpp_deduction_guides is not defined" ) + +int main() {} + +#endif diff --git a/test/wp_guides_test.cpp b/test/wp_guides_test.cpp new file mode 100644 index 0000000..92f95a0 --- /dev/null +++ b/test/wp_guides_test.cpp @@ -0,0 +1,24 @@ +// Copyright 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#if defined(__cpp_deduction_guides) + +#include +#include + +int main() +{ + boost::shared_ptr p1( new int ); + boost::weak_ptr p2( p1 ); +} + +#else + +#include + +BOOST_PRAGMA_MESSAGE( "Skipping test because __cpp_deduction_guides is not defined" ) + +int main() {} + +#endif From da81452f1f4161c45ec1fead6cb92577d6f21188 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 2 Apr 2020 02:16:59 +0300 Subject: [PATCH 02/10] 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(); +} From 54b549820866ecfb269e43117595a9aafcb56814 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 2 Apr 2020 04:01:21 +0300 Subject: [PATCH 03/10] Manually convince clang-cl to fail shared_from_fail and weak_from_fail --- test/shared_from_fail.cpp | 6 ++++++ test/weak_from_fail.cpp | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/test/shared_from_fail.cpp b/test/shared_from_fail.cpp index b7275a7..5d670fa 100644 --- a/test/shared_from_fail.cpp +++ b/test/shared_from_fail.cpp @@ -9,6 +9,12 @@ struct X: public boost::enable_shared_from { }; +#if defined(__clang__) && defined(_MSC_VER) +// clang-cl claims that it accepts this code for compatibility +// with msvc, but no version of msvc accepts it +# pragma clang diagnostic error "-Wmicrosoft-using-decl" +#endif + int main() { boost::shared_ptr px( new X ); diff --git a/test/weak_from_fail.cpp b/test/weak_from_fail.cpp index 5d75e40..7503c3f 100644 --- a/test/weak_from_fail.cpp +++ b/test/weak_from_fail.cpp @@ -9,6 +9,12 @@ struct X: public boost::enable_shared_from { }; +#if defined(__clang__) && defined(_MSC_VER) +// clang-cl claims that it accepts this code for compatibility +// with msvc, but no version of msvc accepts it +# pragma clang diagnostic error "-Wmicrosoft-using-decl" +#endif + int main() { boost::shared_ptr px( new X ); From 977544feda129535e84c05e91c1576e7fd86f72d Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Wed, 8 Apr 2020 21:25:36 +0300 Subject: [PATCH 04/10] Add sp_warning_test --- test/Jamfile | 2 ++ test/sp_warning_test.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 test/sp_warning_test.cpp diff --git a/test/Jamfile b/test/Jamfile index c62fd0b..6078da9 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -359,3 +359,5 @@ run wp_guides_test.cpp ; compile-fail shared_from_fail.cpp ; compile-fail weak_from_fail.cpp ; + +compile sp_warning_test.cpp ; diff --git a/test/sp_warning_test.cpp b/test/sp_warning_test.cpp new file mode 100644 index 0000000..26aa78d --- /dev/null +++ b/test/sp_warning_test.cpp @@ -0,0 +1,13 @@ +// Copyright 2020 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#if defined(__GNUC__) && __GNUC__ >= 5 +# pragma GCC diagnostic error "-Wsuggest-override" +#endif + +#include + +int main() +{ +} From 2dd35e5fbc4fbd6024b49b6fae4ff5f9d2811a68 Mon Sep 17 00:00:00 2001 From: Glen Fernandes Date: Mon, 13 Apr 2020 15:34:13 -0400 Subject: [PATCH 05/10] Mark functions with BOOST_OVERRIDE --- .../boost/smart_ptr/allocate_local_shared_array.hpp | 5 +++-- include/boost/smart_ptr/allocate_shared_array.hpp | 11 ++++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/include/boost/smart_ptr/allocate_local_shared_array.hpp b/include/boost/smart_ptr/allocate_local_shared_array.hpp index 0890849..89d3285 100644 --- a/include/boost/smart_ptr/allocate_local_shared_array.hpp +++ b/include/boost/smart_ptr/allocate_local_shared_array.hpp @@ -21,11 +21,12 @@ public: count_ = shared_count(base); } - virtual void local_cb_destroy() BOOST_SP_NOEXCEPT { + void local_cb_destroy() BOOST_SP_NOEXCEPT BOOST_OVERRIDE { shared_count().swap(count_); } - virtual shared_count local_cb_get_shared_count() const BOOST_SP_NOEXCEPT { + shared_count local_cb_get_shared_count() const + BOOST_SP_NOEXCEPT BOOST_OVERRIDE { return count_; } diff --git a/include/boost/smart_ptr/allocate_shared_array.hpp b/include/boost/smart_ptr/allocate_shared_array.hpp index bca9865..9bf7322 100644 --- a/include/boost/smart_ptr/allocate_shared_array.hpp +++ b/include/boost/smart_ptr/allocate_shared_array.hpp @@ -206,28 +206,29 @@ public: return state_; } - virtual void dispose() BOOST_SP_NOEXCEPT { + void dispose() BOOST_SP_NOEXCEPT BOOST_OVERRIDE { boost::alloc_destroy_n(state_.allocator(), boost::first_scalar(sp_array_start(this)), state_.size() * sp_array_count::value); } - virtual void destroy() BOOST_SP_NOEXCEPT { + void destroy() BOOST_SP_NOEXCEPT BOOST_OVERRIDE { sp_array_creator other(state_.allocator(), state_.size()); this->~sp_array_base(); other.destroy(this); } - virtual void* get_deleter(const sp_typeinfo_&) BOOST_SP_NOEXCEPT { + void* get_deleter(const sp_typeinfo_&) BOOST_SP_NOEXCEPT BOOST_OVERRIDE { return 0; } - virtual void* get_local_deleter(const sp_typeinfo_&) BOOST_SP_NOEXCEPT { + void* get_local_deleter(const sp_typeinfo_&) + BOOST_SP_NOEXCEPT BOOST_OVERRIDE { return 0; } - virtual void* get_untyped_deleter() BOOST_SP_NOEXCEPT { + void* get_untyped_deleter() BOOST_SP_NOEXCEPT BOOST_OVERRIDE { return 0; } From 296c2031353e5bfcbc2ce19a50bd52a8e299133f Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 14 Apr 2020 00:15:32 +0300 Subject: [PATCH 06/10] Rename sp_warning_test to sp_override_test --- test/Jamfile | 2 +- test/{sp_warning_test.cpp => sp_override_test.cpp} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename test/{sp_warning_test.cpp => sp_override_test.cpp} (100%) diff --git a/test/Jamfile b/test/Jamfile index 6078da9..0d31010 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -360,4 +360,4 @@ run wp_guides_test.cpp ; compile-fail shared_from_fail.cpp ; compile-fail weak_from_fail.cpp ; -compile sp_warning_test.cpp ; +compile sp_override_test.cpp ; diff --git a/test/sp_warning_test.cpp b/test/sp_override_test.cpp similarity index 100% rename from test/sp_warning_test.cpp rename to test/sp_override_test.cpp From 7ab4093f4614274f84d1181e209ed996e0f4845e Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 14 Apr 2020 00:21:02 +0300 Subject: [PATCH 07/10] Use shared_ptr and make_shared in sp_override_test --- test/sp_override_test.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/sp_override_test.cpp b/test/sp_override_test.cpp index 26aa78d..fca67d3 100644 --- a/test/sp_override_test.cpp +++ b/test/sp_override_test.cpp @@ -10,4 +10,9 @@ int main() { + boost::shared_ptr p1( new int ); + boost::shared_ptr p2( new int[1] ); + + boost::make_shared(); + boost::make_shared( 1 ); } From 2320dafc0357cfb13a416cfc9da961095a9a403a Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 14 Apr 2020 00:42:57 +0300 Subject: [PATCH 08/10] Add BOOST_OVERRIDE to bad_weak_ptr.hpp and local_counted_base.hpp --- include/boost/smart_ptr/bad_weak_ptr.hpp | 2 +- include/boost/smart_ptr/detail/local_counted_base.hpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/boost/smart_ptr/bad_weak_ptr.hpp b/include/boost/smart_ptr/bad_weak_ptr.hpp index e3e81f9..c21a2c5 100644 --- a/include/boost/smart_ptr/bad_weak_ptr.hpp +++ b/include/boost/smart_ptr/bad_weak_ptr.hpp @@ -47,7 +47,7 @@ class bad_weak_ptr: public std::exception { public: - virtual char const * what() const BOOST_NOEXCEPT_OR_NOTHROW + virtual char const * what() const BOOST_NOEXCEPT_OR_NOTHROW BOOST_OVERRIDE { return "tr1::bad_weak_ptr"; } diff --git a/include/boost/smart_ptr/detail/local_counted_base.hpp b/include/boost/smart_ptr/detail/local_counted_base.hpp index 405ef30..3914aa1 100644 --- a/include/boost/smart_ptr/detail/local_counted_base.hpp +++ b/include/boost/smart_ptr/detail/local_counted_base.hpp @@ -113,12 +113,12 @@ public: #endif - virtual void local_cb_destroy() BOOST_SP_NOEXCEPT + virtual void local_cb_destroy() BOOST_SP_NOEXCEPT BOOST_OVERRIDE { delete this; } - virtual boost::detail::shared_count local_cb_get_shared_count() const BOOST_SP_NOEXCEPT + virtual boost::detail::shared_count local_cb_get_shared_count() const BOOST_SP_NOEXCEPT BOOST_OVERRIDE { return pn_; } @@ -130,12 +130,12 @@ public: shared_count pn_; - virtual void local_cb_destroy() BOOST_SP_NOEXCEPT + virtual void local_cb_destroy() BOOST_SP_NOEXCEPT BOOST_OVERRIDE { shared_count().swap( pn_ ); } - virtual boost::detail::shared_count local_cb_get_shared_count() const BOOST_SP_NOEXCEPT + virtual boost::detail::shared_count local_cb_get_shared_count() const BOOST_SP_NOEXCEPT BOOST_OVERRIDE { return pn_; } From a2732e207ad925a2e7de04976a1966527d31c7ea Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 14 Apr 2020 00:55:52 +0300 Subject: [PATCH 09/10] Add BOOST_OVERRIDE to sp_counted_impl.hpp --- .../boost/smart_ptr/detail/sp_counted_impl.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/include/boost/smart_ptr/detail/sp_counted_impl.hpp b/include/boost/smart_ptr/detail/sp_counted_impl.hpp index 96c9da3..23ea339 100644 --- a/include/boost/smart_ptr/detail/sp_counted_impl.hpp +++ b/include/boost/smart_ptr/detail/sp_counted_impl.hpp @@ -85,7 +85,7 @@ public: #endif } - virtual void dispose() BOOST_SP_NOEXCEPT + virtual void dispose() BOOST_SP_NOEXCEPT BOOST_OVERRIDE { #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) boost::sp_scalar_destructor_hook( px_, sizeof(X), this ); @@ -93,17 +93,17 @@ public: boost::checked_delete( px_ ); } - virtual void * get_deleter( sp_typeinfo_ const & ) BOOST_SP_NOEXCEPT + virtual void * get_deleter( sp_typeinfo_ const & ) BOOST_SP_NOEXCEPT BOOST_OVERRIDE { return 0; } - virtual void * get_local_deleter( sp_typeinfo_ const & ) BOOST_SP_NOEXCEPT + virtual void * get_local_deleter( sp_typeinfo_ const & ) BOOST_SP_NOEXCEPT BOOST_OVERRIDE { return 0; } - virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT + virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT BOOST_OVERRIDE { return 0; } @@ -168,22 +168,22 @@ public: { } - virtual void dispose() BOOST_SP_NOEXCEPT + virtual void dispose() BOOST_SP_NOEXCEPT BOOST_OVERRIDE { del( ptr ); } - virtual void * get_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT + virtual void * get_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT BOOST_OVERRIDE { return ti == BOOST_SP_TYPEID_(D)? &reinterpret_cast( del ): 0; } - virtual void * get_local_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT + virtual void * get_local_deleter( sp_typeinfo_ const & ti ) BOOST_SP_NOEXCEPT BOOST_OVERRIDE { return ti == BOOST_SP_TYPEID_(D)? boost::detail::get_local_deleter( boost::addressof( del ) ): 0; } - virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT + virtual void * get_untyped_deleter() BOOST_SP_NOEXCEPT BOOST_OVERRIDE { return &reinterpret_cast( del ); } From 0ddfab493c0e19021df53af0104a31d8f0cf1373 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 14 Apr 2020 07:12:35 +0300 Subject: [PATCH 10/10] Do not enable -Wsuggest-override in C++03 mode --- test/sp_override_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sp_override_test.cpp b/test/sp_override_test.cpp index fca67d3..8eb72dd 100644 --- a/test/sp_override_test.cpp +++ b/test/sp_override_test.cpp @@ -2,7 +2,7 @@ // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt -#if defined(__GNUC__) && __GNUC__ >= 5 +#if defined(__GNUC__) && __GNUC__ >= 5 && __cplusplus >= 201103L # pragma GCC diagnostic error "-Wsuggest-override" #endif