]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc
1 // { dg-do run { target c++17 } }
3 // Copyright (C) 2014-2022 Free Software Foundation, Inc.
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)
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.
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/>.
24 #include <testsuite_hooks.h>
34 // taken from example in N3804 proposal
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);
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);
46 x
= string("Meow"); // x holds string
48 s
= move(any_cast
<string
&>(x
)); // move from any
50 any_cast
<string
&>(x
) = move(s2
); // move to any
51 VERIFY(any_cast
<const string
&>(x
) == "Jane");
54 const any
y(cat
); // const y holds string
55 VERIFY(any_cast
<const string
&>(y
) == cat
);
60 using std::bad_any_cast
;
62 auto p
= any_cast
<double>(&x
);
66 p
= any_cast
<double>(&x
);
70 p
= any_cast
<double>(&x
);
76 } catch (const bad_any_cast
&) {
80 static int move_count
= 0;
86 MoveEnabled(MoveEnabled
&&)
90 MoveEnabled() = default;
91 MoveEnabled(const MoveEnabled
&) = default;
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);
104 ExplicitCopy() = default;
105 explicit ExplicitCopy(const ExplicitCopy
&) = default;
107 any x
= ExplicitCopy();
108 ExplicitCopy ec
{any_cast
<ExplicitCopy
>(x
)};
109 ExplicitCopy ec2
{any_cast
<ExplicitCopy
>(std::move(x
))};
114 // PR libstdc++/69321
116 noncopyable(noncopyable
const&) = delete;
120 auto p
= any_cast
<noncopyable
>(&a
);
121 VERIFY( p
== nullptr );
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.
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 );
138 any_cast
<int(&)()>(a
);
140 } catch (const std::bad_any_cast
&) {
144 any_cast
<int(&)()>(std::move(a
));
146 } catch (const std::bad_any_cast
&) {
150 any_cast
<int(&)()>(std::as_const(a
));
152 } catch (const std::bad_any_cast
&) {
160 VERIFY( a
.type() == typeid(int*) ); // contained value is decayed
162 int (*p1
)[3] = any_cast
<int[3]>(&a
);
163 VERIFY( a
.type() != typeid(int[3]) ); // so any_cast should return nullptr
164 VERIFY( p1
== nullptr );
165 int (*p2
)[] = any_cast
<int[]>(&a
);
166 VERIFY( a
.type() != typeid(int[]) ); // so any_cast should return nullptr
167 VERIFY( p2
== nullptr );
168 const int (*p3
)[] = any_cast
<int[]>(&std::as_const(a
));
169 VERIFY( p3
== nullptr );