Debian Codegraph @

gcc/testsuite/g++.dg/lto/pr80287_0.C

raw
1 // { dg-lto-options { "-std=gnu++17" } }
2 // { dg-lto-do run }
3 // { dg-skip-if "requires hosted libstdc++ for any" { ! hostedlib } }
4
5 // Copyright (C) 2014-2017 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 #include <any>
23 #define VERIFY(x) if (!(x)) __builtin_abort()
24
25 using std::any;
26 using std::any_cast;
27
28 bool moved = false;
29 bool copied = false;
30
31 struct X
32 {
33 X() = default;
34 X(const X&) { copied = true; }
35 X(X&& x) { moved = true; }
36 };
37
38 struct X2
39 {
40 X2() = default;
41 X2(const X2&) { copied = true; }
42 X2(X2&& x) noexcept { moved = true; }
43 }__attribute((may_alias));
44
45 void test01()
46 {
47 moved = false;
48 X x;
49 any a1;
50 a1 = x;
51 VERIFY(moved == false);
52 any a2;
53 copied = false;
54 a2 = std::move(x);
55 VERIFY(moved == true);
56 VERIFY(copied == false);
57 }
58
59 void test02()
60 {
61 moved = false;
62 X x;
63 any a1;
64 a1 = x;
65 VERIFY(moved == false);
66 any a2;
67 copied = false;
68 a2 = std::move(a1);
69 VERIFY(moved == false);
70 VERIFY(copied == false);
71 }
72
73 void test03()
74 {
75 moved = false;
76 X2 x;
77 any a1;
78 a1 = x;
79 VERIFY(copied && moved);
80 any a2;
81 moved = false;
82 copied = false;
83 a2 = std::move(a1);
84 VERIFY(moved == true);
85 VERIFY(copied == false);
86 }
87
88 int main()
89 {
90 test01();
91 test02();
92 test03();
93 }