]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/enable_shared_from_this/56383.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / enable_shared_from_this / 56383.cc
1 // Copyright (C) 2015-2019 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++11 } }
19
20 #include <memory>
21 #include <testsuite_hooks.h>
22
23 template<typename T>
24 bool not_enabled(T& t)
25 {
26 #if __cpp_lib_enable_shared_from_this >= 201603
27 return t.weak_from_this().expired();
28 #else
29 try {
30 t.shared_from_this();
31 return false;
32 } catch (const std::bad_weak_ptr&) {
33 return true;
34 }
35 #endif
36 }
37
38 struct A : std::enable_shared_from_this<A> { };
39 struct B : std::enable_shared_from_this<B> { };
40 struct D : A, B { };
41
42 void test01()
43 {
44 auto d = std::make_shared<D>();
45 VERIFY( not_enabled( static_cast<A&>(*d) ) );
46 VERIFY( not_enabled( static_cast<const A&>(*d) ) );
47 VERIFY( not_enabled( static_cast<B&>(*d) ) );
48 VERIFY( not_enabled( static_cast<const B&>(*d) ) );
49 }
50
51 struct E : std::__enable_shared_from_this<E> { };
52 struct F : std::__enable_shared_from_this<F> { };
53 struct G : E, F { };
54
55 void test02()
56 {
57 auto g = std::make_shared<G>();
58 VERIFY( not_enabled( static_cast<E&>(*g) ) );
59 VERIFY( not_enabled( static_cast<const E&>(*g) ) );
60 VERIFY( not_enabled( static_cast<F&>(*g) ) );
61 VERIFY( not_enabled( static_cast<const F&>(*g) ) );
62 }
63
64 struct H : D, G { };
65
66 void test03()
67 {
68 auto h = std::make_shared<H>();
69 VERIFY( not_enabled( static_cast<A&>(*h) ) );
70 VERIFY( not_enabled( static_cast<const A&>(*h) ) );
71 VERIFY( not_enabled( static_cast<B&>(*h) ) );
72 VERIFY( not_enabled( static_cast<const B&>(*h) ) );
73 VERIFY( not_enabled( static_cast<E&>(*h) ) );
74 VERIFY( not_enabled( static_cast<const E&>(*h) ) );
75 VERIFY( not_enabled( static_cast<F&>(*h) ) );
76 VERIFY( not_enabled( static_cast<const F&>(*h) ) );
77 }
78
79 int
80 main()
81 {
82 test01();
83 test02();
84 test03();
85 }