Debian Codegraph @

libstdc++-v3/testsuite/20_util/any/assign/exception.cc

raw
1 // { dg-do run { target c++17 } }
2
3 // Copyright (C) 2016-2025 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 <any>
21 #include <testsuite_hooks.h>
22
23 using std::any;
24
25 bool should_throw = false;
26 struct Bad
27 {
28 Bad() = default;
29 Bad(const Bad&) {if (should_throw) throw 666;}
30 };
31
32 struct Bad2
33 {
34 Bad2() = default;
35 Bad2(const Bad2&) {if (should_throw) throw 666;}
36 Bad2(Bad2&&) noexcept {}
37 };
38
39 int del_count = 0;
40 struct Good
41 {
42 Good() = default;
43 Good(const Good&) = default;
44 Good(Good&&) = default;
45 ~Good() {++del_count;}
46 };
47
48 int main()
49 {
50 std::any a1 = Good();
51 del_count = 0;
52 try {
53 Bad b;
54 std::any a2 = b;
55 should_throw = true;
56 a1 = a2;
57 } catch (...) {
58 auto x = std::any_cast<Good>(a1);
59 VERIFY( del_count == 0 );
60 VERIFY( a1.has_value() );
61 std::any_cast<Good>(a1);
62 }
63 std::any a3 = Good();
64 del_count = 0;
65 try {
66 Bad2 b;
67 std::any a4 = b;
68 should_throw = true;
69 a3 = a4;
70 } catch (...) {
71 auto x = std::any_cast<Good>(a1);
72 VERIFY( del_count == 0 );
73 VERIFY( a1.has_value() );
74 std::any_cast<Good>(a1);
75 }
76 }