]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/any/assign/exception.cc
bf93f4d2e2ba52dff6686bdc5a35bce33679a9a3
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / any / assign / exception.cc
1 // { dg-options "-std=gnu++17" }
2 // { dg-do run }
3
4 // Copyright (C) 2016-2019 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
20
21 #include <any>
22 #include <testsuite_hooks.h>
23
24 using std::any;
25
26 bool should_throw = false;
27 struct Bad
28 {
29 Bad() = default;
30 Bad(const Bad&) {if (should_throw) throw 666;}
31 };
32
33 struct Bad2
34 {
35 Bad2() = default;
36 Bad2(const Bad2&) {if (should_throw) throw 666;}
37 Bad2(Bad2&&) noexcept {}
38 };
39
40 int del_count = 0;
41 struct Good
42 {
43 Good() = default;
44 Good(const Good&) = default;
45 Good(Good&&) = default;
46 ~Good() {++del_count;}
47 };
48
49 int main()
50 {
51 std::any a1 = Good();
52 del_count = 0;
53 try {
54 Bad b;
55 std::any a2 = b;
56 should_throw = true;
57 a1 = a2;
58 } catch (...) {
59 auto x = std::any_cast<Good>(a1);
60 VERIFY( del_count == 0 );
61 VERIFY( a1.has_value() );
62 std::any_cast<Good>(a1);
63 }
64 std::any a3 = Good();
65 del_count = 0;
66 try {
67 Bad2 b;
68 std::any a4 = b;
69 should_throw = true;
70 a3 = a4;
71 } catch (...) {
72 auto x = std::any_cast<Good>(a1);
73 VERIFY( del_count == 0 );
74 VERIFY( a1.has_value() );
75 std::any_cast<Good>(a1);
76 }
77 }