]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/any/misc/any_cast.cc
1 // { dg-do run { target c++14 } }
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/>.
20 #include <experimental/any>
23 #include <testsuite_hooks.h>
25 using std::experimental::any
;
26 using std::experimental::any_cast
;
27 using std::experimental::bad_any_cast
;
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
);
61 auto p
= any_cast
<double>(&x
);
65 p
= any_cast
<double>(&x
);
69 p
= any_cast
<double>(&x
);
75 } catch (const bad_any_cast
&) {
79 static int move_count
= 0;
85 MoveEnabled(MoveEnabled
&&)
89 MoveEnabled() = default;
90 MoveEnabled(const MoveEnabled
&) = default;
93 MoveEnabled m2
= any_cast
<MoveEnabled
>(any(m
));
94 VERIFY(move_count
== 1);
95 MoveEnabled
&& m3
= any_cast
<MoveEnabled
&&>(any(m
));
96 VERIFY(move_count
== 1);
99 MoveDeleted(MoveDeleted
&&) = delete;
100 MoveDeleted() = default;
101 MoveDeleted(const MoveDeleted
&) = default;
104 MoveDeleted
&& md2
= any_cast
<MoveDeleted
>(any(std::move(md
)));
105 MoveDeleted
&& md3
= any_cast
<MoveDeleted
&&>(any(std::move(md
)));
110 // PR libstdc++/69321
112 noncopyable(noncopyable
const&) = delete;
116 auto p
= any_cast
<noncopyable
>(&a
);
117 VERIFY( p
== nullptr );
122 // The contained value of a std::any is always an object type,
123 // but any_cast does not forbid checking for function types.
126 void (*p1
)() = any_cast
<void()>(&a
);
127 VERIFY( p1
== nullptr );
128 int (*p2
)(int) = any_cast
<int(int)>(&a
);
129 VERIFY( p2
== nullptr );
130 int (*p3
)() = any_cast
<int()>(&const_cast<const any
&>(a
));
131 VERIFY( p3
== nullptr );
134 any_cast
<int(&)()>(a
);
136 } catch (const bad_any_cast
&) {
140 any_cast
<int(&)()>(std::move(a
));
142 } catch (const bad_any_cast
&) {
146 any_cast
<int(&)()>(const_cast<const any
&>(a
));
148 } catch (const bad_any_cast
&) {
156 VERIFY( a
.type() == typeid(int*) ); // contained value is decayed
158 int (*p1
)[3] = any_cast
<int[3]>(&a
);
159 VERIFY( a
.type() != typeid(int[3]) ); // so any_cast should return nullptr
160 VERIFY( p1
== nullptr );
161 int (*p2
)[] = any_cast
<int[]>(&a
);
162 VERIFY( a
.type() != typeid(int[]) ); // so any_cast should return nullptr
163 VERIFY( p2
== nullptr );
164 const int (*p3
)[] = any_cast
<int[]>(&const_cast<const any
&>(a
));
165 VERIFY( p3
== nullptr );