| 1 |
// { dg-do run { target c++11 } } |
| 2 |
|
| 3 |
// 2008-05-25 Sebastian Redl <[email protected]> |
| 4 |
|
| 5 |
// Copyright (C) 2008-2025 Free Software Foundation, Inc. |
| 6 |
// |
| 7 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 8 |
// software; you can redistribute it and/or modify it under the |
| 9 |
// terms of the GNU General Public License as published by the |
| 10 |
// Free Software Foundation; either version 3, or (at your option) |
| 11 |
// any later version. |
| 12 |
|
| 13 |
// This library is distributed in the hope that it will be useful, |
| 14 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
// GNU General Public License for more details. |
| 17 |
|
| 18 |
// You should have received a copy of the GNU General Public License along |
| 19 |
// with this library; see the file COPYING3. If not see |
| 20 |
// <http://www.gnu.org/licenses/>. |
| 21 |
|
| 22 |
// Tests the life span of the exception object. |
| 23 |
|
| 24 |
#include <exception> |
| 25 |
#include <testsuite_hooks.h> |
| 26 |
|
| 27 |
bool may_destruct = false; |
| 28 |
|
| 29 |
class destructing |
| 30 |
{ |
| 31 |
mutable bool copied; |
| 32 |
|
| 33 |
public: |
| 34 |
destructing() : copied(false) { } |
| 35 |
destructing(const destructing &o) : copied(false) { o.copied = true; } |
| 36 |
~destructing() { VERIFY( copied || may_destruct ); } |
| 37 |
}; |
| 38 |
|
| 39 |
void test01() |
| 40 |
{ |
| 41 |
using namespace std; |
| 42 |
|
| 43 |
may_destruct = false; |
| 44 |
|
| 45 |
// Test the destructing class. |
| 46 |
{ |
| 47 |
destructing *d = new destructing; |
| 48 |
destructing d2(*d); |
| 49 |
delete d; |
| 50 |
may_destruct = true; |
| 51 |
} |
| 52 |
may_destruct = false; |
| 53 |
} |
| 54 |
|
| 55 |
void test02() |
| 56 |
{ |
| 57 |
using namespace std; |
| 58 |
|
| 59 |
may_destruct = false; |
| 60 |
|
| 61 |
try { |
| 62 |
throw destructing(); |
| 63 |
} catch(...) { |
| 64 |
may_destruct = true; |
| 65 |
} |
| 66 |
may_destruct = false; |
| 67 |
} |
| 68 |
|
| 69 |
void test03() |
| 70 |
{ |
| 71 |
using namespace std; |
| 72 |
|
| 73 |
may_destruct = false; |
| 74 |
|
| 75 |
try { |
| 76 |
throw destructing(); |
| 77 |
} catch(...) { |
| 78 |
{ |
| 79 |
exception_ptr ep = current_exception(); |
| 80 |
} |
| 81 |
may_destruct = true; |
| 82 |
} |
| 83 |
may_destruct = false; |
| 84 |
} |
| 85 |
|
| 86 |
void test04() |
| 87 |
{ |
| 88 |
using namespace std; |
| 89 |
|
| 90 |
may_destruct = false; |
| 91 |
|
| 92 |
{ |
| 93 |
exception_ptr ep; |
| 94 |
try { |
| 95 |
throw destructing(); |
| 96 |
} catch(...) { |
| 97 |
ep = current_exception(); |
| 98 |
} |
| 99 |
may_destruct = true; |
| 100 |
} |
| 101 |
may_destruct = false; |
| 102 |
} |
| 103 |
|
| 104 |
void test05_helper() |
| 105 |
{ |
| 106 |
using namespace std; |
| 107 |
try { |
| 108 |
throw destructing(); |
| 109 |
} catch(...) { |
| 110 |
exception_ptr ep = current_exception(); |
| 111 |
rethrow_exception(ep); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void test05() |
| 116 |
{ |
| 117 |
using namespace std; |
| 118 |
|
| 119 |
may_destruct = false; |
| 120 |
|
| 121 |
try { |
| 122 |
test05_helper(); |
| 123 |
} catch(...) { |
| 124 |
may_destruct = true; |
| 125 |
} |
| 126 |
may_destruct = false; |
| 127 |
} |
| 128 |
|
| 129 |
void test06_helper() |
| 130 |
{ |
| 131 |
using namespace std; |
| 132 |
try { |
| 133 |
throw destructing(); |
| 134 |
} catch(...) { |
| 135 |
exception_ptr ep = current_exception(); |
| 136 |
throw; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
void test06() |
| 141 |
{ |
| 142 |
using namespace std; |
| 143 |
|
| 144 |
may_destruct = false; |
| 145 |
|
| 146 |
try |
| 147 |
{ |
| 148 |
test06_helper(); |
| 149 |
} |
| 150 |
catch(...) |
| 151 |
{ |
| 152 |
may_destruct = true; |
| 153 |
} |
| 154 |
may_destruct = false; |
| 155 |
} |
| 156 |
|
| 157 |
std::exception_ptr gep; |
| 158 |
|
| 159 |
void test99() |
| 160 |
{ |
| 161 |
using namespace std; |
| 162 |
|
| 163 |
may_destruct = false; |
| 164 |
|
| 165 |
try |
| 166 |
{ |
| 167 |
throw destructing(); |
| 168 |
} |
| 169 |
catch(...) |
| 170 |
{ |
| 171 |
gep = current_exception(); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
int main() |
| 176 |
{ |
| 177 |
test01(); |
| 178 |
test02(); |
| 179 |
test03(); |
| 180 |
test04(); |
| 181 |
test05(); |
| 182 |
test06(); |
| 183 |
|
| 184 |
test99(); |
| 185 |
may_destruct = true; |
| 186 |
return 0; |
| 187 |
} |