From 4f63d614fa51b4f2f28ea8d61af765c6b58b90e2 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Thu, 20 Oct 2016 11:13:04 +0100 Subject: [PATCH] Add more tests for enable_shared_from_this ambiguities * testsuite/20_util/enable_shared_from_this/56383.cc: Add tests for additional ambiguous cases. From-SVN: r241364 --- libstdc++-v3/ChangeLog | 5 ++ .../20_util/enable_shared_from_this/56383.cc | 69 +++++++++++++------ 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 93a8f3ec4ca..340a7f6bfe3 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,8 @@ +2016-10-20 Jonathan Wakely + + * testsuite/20_util/enable_shared_from_this/56383.cc: Add tests for + additional ambiguous cases. + 2016-10-19 Jonathan Wakely * include/backward/auto_ptr.h (__shared_ptr(auto_ptr&&)): Call diff --git a/libstdc++-v3/testsuite/20_util/enable_shared_from_this/56383.cc b/libstdc++-v3/testsuite/20_util/enable_shared_from_this/56383.cc index fb3fa69d073..9220eaf4e82 100644 --- a/libstdc++-v3/testsuite/20_util/enable_shared_from_this/56383.cc +++ b/libstdc++-v3/testsuite/20_util/enable_shared_from_this/56383.cc @@ -20,37 +20,66 @@ #include #include -struct A : std::enable_shared_from_this +template +bool not_enabled(T& t) { - void* a() { return shared_from_this().get(); } -}; +#if __cpp_lib_enable_shared_from_this >= 201603 + return t.weak_from_this().expired(); +#else + try { + t.shared_from_this(); + return false; + } catch (const std::bad_weak_ptr&) { + return true; + } +#endif +} -struct B : std::enable_shared_from_this -{ -}; +struct A : std::enable_shared_from_this { }; +struct B : std::enable_shared_from_this { }; +struct D : A, B { }; -struct D : A, B +void test01() { -}; + auto d = std::make_shared(); + VERIFY( not_enabled( static_cast(*d) ) ); + VERIFY( not_enabled( static_cast(*d) ) ); + VERIFY( not_enabled( static_cast(*d) ) ); + VERIFY( not_enabled( static_cast(*d) ) ); +} -void test01() +struct E : std::__enable_shared_from_this { }; +struct F : std::__enable_shared_from_this { }; +struct G : E, F { }; + +void test02() { - bool test = false; + auto g = std::make_shared(); + VERIFY( not_enabled( static_cast(*g) ) ); + VERIFY( not_enabled( static_cast(*g) ) ); + VERIFY( not_enabled( static_cast(*g) ) ); + VERIFY( not_enabled( static_cast(*g) ) ); +} - auto d = std::make_shared(); - try - { - d->a(); - } - catch (const std::bad_weak_ptr&) - { - test = true; - } - VERIFY(test); +struct H : D, G { }; + +void test03() +{ + auto h = std::make_shared(); + VERIFY( not_enabled( static_cast(*h) ) ); + VERIFY( not_enabled( static_cast(*h) ) ); + VERIFY( not_enabled( static_cast(*h) ) ); + VERIFY( not_enabled( static_cast(*h) ) ); + VERIFY( not_enabled( static_cast(*h) ) ); + VERIFY( not_enabled( static_cast(*h) ) ); + VERIFY( not_enabled( static_cast(*h) ) ); + VERIFY( not_enabled( static_cast(*h) ) ); } int main() { test01(); + test02(); + test03(); } -- 2.30.2