]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/any/assign/exception.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / any / assign / exception.cc
1 // { dg-do run { target c++14 } }
2
3 // Copyright (C) 2016-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 <experimental/any>
21 #include <testsuite_hooks.h>
22
23 using std::experimental::any;
24 using std::experimental::any_cast;
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 any a1 = Good();
52 del_count = 0;
53 try {
54 Bad b;
55 any a2 = b;
56 should_throw = true;
57 a1 = a2;
58 } catch (...) {
59 auto x = any_cast<Good>(a1);
60 VERIFY( del_count == 0 );
61 VERIFY( !a1.empty() );
62 any_cast<Good>(a1);
63 }
64 any a3 = Good();
65 del_count = 0;
66 try {
67 Bad2 b;
68 any a4 = b;
69 should_throw = true;
70 a3 = a4;
71 } catch (...) {
72 auto x = any_cast<Good>(a1);
73 VERIFY( del_count == 0 );
74 VERIFY( !a1.empty() );
75 any_cast<Good>(a1);
76 }
77 }