]>
Commit | Line | Data |
---|---|---|
84508c51 | 1 | // { dg-options "-std=gnu++17" } |
2 | // { dg-do run } | |
3 | ||
fbd26352 | 4 | // Copyright (C) 2014-2019 Free Software Foundation, Inc. |
84508c51 | 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 <string> | |
23 | #include <cstring> | |
24 | #include <testsuite_hooks.h> | |
25 | ||
26 | using std::any; | |
27 | using std::any_cast; | |
28 | ||
29 | void test01() | |
30 | { | |
31 | using std::string; | |
32 | using std::strcmp; | |
33 | ||
34 | // taken from example in N3804 proposal | |
35 | ||
36 | any x(5); // x holds int | |
37 | VERIFY(any_cast<int>(x) == 5); // cast to value | |
38 | any_cast<int&>(x) = 10; // cast to reference | |
39 | VERIFY(any_cast<int>(x) == 10); | |
40 | ||
41 | x = "Meow"; // x holds const char* | |
42 | VERIFY(strcmp(any_cast<const char*>(x), "Meow") == 0); | |
43 | any_cast<const char*&>(x) = "Harry"; | |
44 | VERIFY(strcmp(any_cast<const char*>(x), "Harry") == 0); | |
45 | ||
46 | x = string("Meow"); // x holds string | |
47 | string s, s2("Jane"); | |
48 | s = move(any_cast<string&>(x)); // move from any | |
49 | VERIFY(s == "Meow"); | |
50 | any_cast<string&>(x) = move(s2); // move to any | |
51 | VERIFY(any_cast<const string&>(x) == "Jane"); | |
52 | ||
53 | string cat("Meow"); | |
54 | const any y(cat); // const y holds string | |
55 | VERIFY(any_cast<const string&>(y) == cat); | |
56 | } | |
57 | ||
58 | void test02() | |
59 | { | |
60 | using std::bad_any_cast; | |
61 | any x(1); | |
62 | auto p = any_cast<double>(&x); | |
63 | VERIFY(p == nullptr); | |
64 | ||
65 | x = 1.0; | |
66 | p = any_cast<double>(&x); | |
67 | VERIFY(p != nullptr); | |
68 | ||
69 | x = any(); | |
70 | p = any_cast<double>(&x); | |
71 | VERIFY(p == nullptr); | |
72 | ||
73 | try { | |
74 | any_cast<double>(x); | |
75 | VERIFY(false); | |
76 | } catch (const bad_any_cast&) { | |
77 | } | |
78 | } | |
79 | ||
80 | static int move_count = 0; | |
81 | ||
82 | void test03() | |
83 | { | |
84 | struct MoveEnabled | |
85 | { | |
86 | MoveEnabled(MoveEnabled&&) | |
87 | { | |
88 | ++move_count; | |
89 | } | |
90 | MoveEnabled() = default; | |
91 | MoveEnabled(const MoveEnabled&) = default; | |
92 | }; | |
93 | MoveEnabled m; | |
94 | MoveEnabled m2 = any_cast<MoveEnabled>(any(m)); | |
95 | VERIFY(move_count == 1); | |
96 | MoveEnabled&& m3 = any_cast<MoveEnabled&&>(any(m)); | |
97 | VERIFY(move_count == 1); | |
84508c51 | 98 | } |
99 | ||
83954fa1 | 100 | void test04() |
101 | { | |
102 | struct ExplicitCopy | |
103 | { | |
104 | ExplicitCopy() = default; | |
105 | explicit ExplicitCopy(const ExplicitCopy&) = default; | |
106 | }; | |
107 | any x = ExplicitCopy(); | |
108 | ExplicitCopy ec{any_cast<ExplicitCopy>(x)}; | |
109 | ExplicitCopy ec2{any_cast<ExplicitCopy>(std::move(x))}; | |
110 | } | |
111 | ||
64ac1079 | 112 | void test05() |
113 | { | |
114 | // PR libstdc++/69321 | |
115 | struct noncopyable { | |
116 | noncopyable(noncopyable const&) = delete; | |
117 | }; | |
118 | ||
119 | any a; | |
120 | auto p = any_cast<noncopyable>(&a); | |
121 | VERIFY( p == nullptr ); | |
122 | } | |
123 | ||
84508c51 | 124 | int main() |
125 | { | |
126 | test01(); | |
127 | test02(); | |
128 | test03(); | |
83954fa1 | 129 | test04(); |
64ac1079 | 130 | test05(); |
84508c51 | 131 | } |