]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / any / misc / any_cast.cc
CommitLineData
6458742a 1// { dg-do run { target c++17 } }
52e86221 2
83ffe9cd 3// Copyright (C) 2014-2023 Free Software Foundation, Inc.
52e86221
VV
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 <string>
f9bfdfa2 22#include <utility>
52e86221
VV
23#include <cstring>
24#include <testsuite_hooks.h>
25
26using std::any;
27using std::any_cast;
28
29void 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
8240175b 39 VERIFY(any_cast<int>(x) == 10);
52e86221
VV
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");
8240175b 48 s = move(any_cast<string&>(x)); // move from any
52e86221
VV
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
58void 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
80static int move_count = 0;
81
82void 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);
52e86221
VV
98}
99
62549523
VV
100void 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
38a9e5a6
JW
112void 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
f9bfdfa2
JW
124void test06()
125{
126 // The contained value of a std::any is always an object type,
127 // but std::any_cast does not forbid checking for function types.
128
129 any a(1);
130 void (*p1)() = any_cast<void()>(&a);
131 VERIFY( p1 == nullptr );
132 int (*p2)(int) = any_cast<int(int)>(&a);
133 VERIFY( p2 == nullptr );
134 int (*p3)() = any_cast<int()>(&std::as_const(a));
135 VERIFY( p3 == nullptr );
136
137 try {
138 any_cast<int(&)()>(a);
139 VERIFY( false );
140 } catch (const std::bad_any_cast&) {
141 }
142
143 try {
144 any_cast<int(&)()>(std::move(a));
145 VERIFY( false );
146 } catch (const std::bad_any_cast&) {
147 }
148
149 try {
150 any_cast<int(&)()>(std::as_const(a));
151 VERIFY( false );
152 } catch (const std::bad_any_cast&) {
153 }
154}
155
92750002
JW
156void test07()
157{
158 int arr[3];
159 any a(arr);
b6b66006 160#if __cpp_rtti
92750002 161 VERIFY( a.type() == typeid(int*) ); // contained value is decayed
b6b66006 162#endif
92750002
JW
163
164 int (*p1)[3] = any_cast<int[3]>(&a);
b6b66006 165#if __cpp_rtti
92750002 166 VERIFY( a.type() != typeid(int[3]) ); // so any_cast should return nullptr
b6b66006 167#endif
92750002
JW
168 VERIFY( p1 == nullptr );
169 int (*p2)[] = any_cast<int[]>(&a);
b6b66006 170#if __cpp_rtti
92750002 171 VERIFY( a.type() != typeid(int[]) ); // so any_cast should return nullptr
b6b66006 172#endif
92750002
JW
173 VERIFY( p2 == nullptr );
174 const int (*p3)[] = any_cast<int[]>(&std::as_const(a));
175 VERIFY( p3 == nullptr );
176}
177
52e86221
VV
178int main()
179{
180 test01();
181 test02();
182 test03();
62549523 183 test04();
38a9e5a6 184 test05();
f9bfdfa2 185 test06();
92750002 186 test07();
52e86221 187}