]>
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-2019 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
;
33 // taken from example in N3804 proposal
35 any
x(5); // x holds int
36 VERIFY(any_cast
<int>(x
) == 5); // cast to value
37 any_cast
<int&>(x
) = 10; // cast to reference
38 VERIFY(any_cast
<int>(x
) == 10);
40 x
= "Meow"; // x holds const char*
41 VERIFY(strcmp(any_cast
<const char*>(x
), "Meow") == 0);
42 any_cast
<const char*&>(x
) = "Harry";
43 VERIFY(strcmp(any_cast
<const char*>(x
), "Harry") == 0);
45 x
= string("Meow"); // x holds string
47 s
= move(any_cast
<string
&>(x
)); // move from any
49 any_cast
<string
&>(x
) = move(s2
); // move to any
50 VERIFY(any_cast
<const string
&>(x
) == "Jane");
53 const any
y(cat
); // const y holds string
54 VERIFY(any_cast
<const string
&>(y
) == cat
);
59 using std::experimental::bad_any_cast
;
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 );