]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/any/assign/2.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / any / assign / 2.cc
1 // { dg-do run { target c++17 } }
2
3 // Copyright (C) 2014-2023 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 using std::any_cast;
25
26 bool moved = false;
27 bool copied = false;
28
29 struct X
30 {
31 X() = default;
32 X(const X&) { copied = true; }
33 X(X&&) { moved = true; }
34 };
35
36 struct X2
37 {
38 X2() = default;
39 X2(const X2&) { copied = true; }
40 X2(X2&&) noexcept { moved = true; }
41 };
42
43 void test01()
44 {
45 moved = false;
46 X x;
47 any a1;
48 a1 = x;
49 VERIFY(moved == false);
50 any a2;
51 copied = false;
52 a2 = std::move(x);
53 VERIFY(moved == true);
54 VERIFY(copied == false);
55 }
56
57 void test02()
58 {
59 moved = false;
60 X x;
61 any a1;
62 a1 = x;
63 VERIFY(moved == false);
64 any a2;
65 copied = false;
66 a2 = std::move(a1);
67 VERIFY(moved == false);
68 VERIFY(copied == false);
69 }
70
71 void test03()
72 {
73 moved = false;
74 X2 x;
75 any a1;
76 a1 = x;
77 VERIFY(copied && moved);
78 any a2;
79 moved = false;
80 copied = false;
81 a2 = std::move(a1);
82 VERIFY(moved == true);
83 VERIFY(copied == false);
84 }
85
86 int main()
87 {
88 test01();
89 test02();
90 test03();
91 }