]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/18_support/nested_exception/rethrow_if_nested.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 18_support / nested_exception / rethrow_if_nested.cc
1 // { dg-do run { target c++11 } }
2
3 // Copyright (C) 2009-2019 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 #include <exception>
21 #include <testsuite_hooks.h>
22
23 struct derived : std::nested_exception { };
24
25 struct base { virtual ~base() noexcept; };
26 inline base::~base() noexcept = default;
27
28 struct derived2 : base, std::nested_exception { };
29
30 void test01()
31 {
32 bool test = false;
33
34 try
35 {
36 throw 42;
37 }
38 catch (...)
39 {
40 derived d;
41 try
42 {
43 std::rethrow_if_nested(d);
44 }
45 catch (const int& i)
46 {
47 test = true;
48 VERIFY( i == 42 );
49 }
50 }
51
52 VERIFY( test );
53 }
54
55 void test02()
56 {
57 bool test = false;
58
59 try
60 {
61 throw base();
62 }
63 catch (const base& b)
64 {
65 std::rethrow_if_nested(b);
66 test = true;
67 }
68
69 VERIFY( test );
70 }
71
72 void test03()
73 {
74 bool test = false;
75
76 try
77 {
78 throw 42;
79 }
80 catch (...)
81 {
82 try
83 {
84 throw derived2();
85 }
86 catch (const base& b)
87 {
88 try
89 {
90 std::rethrow_if_nested(b);
91 }
92 catch (const int& i)
93 {
94 VERIFY( i == 42 );
95 test = true;
96 }
97 }
98 }
99
100 VERIFY( test );
101 }
102
103 void
104 test04()
105 {
106 // LWG 2484 requires that these cases are well-formed, but don't rethrow.
107
108 std::rethrow_if_nested(1);
109
110 struct S { } nonpolymorphic;
111 std::rethrow_if_nested(nonpolymorphic);
112
113 struct derived3 : derived, derived2 { };
114 derived3 ambiguous_base;
115 std::rethrow_if_nested(ambiguous_base);
116
117 struct derived4 : private std::nested_exception { };
118 derived4 private_base;
119 std::rethrow_if_nested(private_base);
120 }
121
122 int main()
123 {
124 test01();
125 test02();
126 test03();
127 test04();
128 return 0;
129 }