]> git.ipfire.org Git - thirdparty/gcc.git/blame - 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
CommitLineData
a5544970 1// Copyright (C) 2015-2019 Free Software Foundation, Inc.
28eca950
JW
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
52066eae 18// { dg-do run { target c++11 } }
28eca950
JW
19
20#include <memory>
21#include <testsuite_hooks.h>
22
4f63d614
JW
23template<typename T>
24bool not_enabled(T& t)
28eca950 25{
4f63d614
JW
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}
28eca950 37
4f63d614
JW
38struct A : std::enable_shared_from_this<A> { };
39struct B : std::enable_shared_from_this<B> { };
40struct D : A, B { };
28eca950 41
4f63d614 42void test01()
28eca950 43{
4f63d614
JW
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}
28eca950 50
4f63d614
JW
51struct E : std::__enable_shared_from_this<E> { };
52struct F : std::__enable_shared_from_this<F> { };
53struct G : E, F { };
54
55void test02()
28eca950 56{
4f63d614
JW
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}
28eca950 63
4f63d614
JW
64struct H : D, G { };
65
66void 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) ) );
28eca950
JW
77}
78
79int
80main()
81{
82 test01();
4f63d614
JW
83 test02();
84 test03();
28eca950 85}