]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/any/misc/any_cast.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / any / misc / any_cast.cc
1 // { dg-do run { target c++14 } }
2
3 // Copyright (C) 2014-2019 Free Software Foundation, Inc.
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 <experimental/any>
21 #include <string>
22 #include <cstring>
23 #include <testsuite_hooks.h>
24
25 using std::experimental::any;
26 using std::experimental::any_cast;
27
28 void test01()
29 {
30 using std::string;
31 using std::strcmp;
32
33 // taken from example in N3804 proposal
34
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);
39
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);
44
45 x = string("Meow"); // x holds string
46 string s, s2("Jane");
47 s = move(any_cast<string&>(x)); // move from any
48 VERIFY(s == "Meow");
49 any_cast<string&>(x) = move(s2); // move to any
50 VERIFY(any_cast<const string&>(x) == "Jane");
51
52 string cat("Meow");
53 const any y(cat); // const y holds string
54 VERIFY(any_cast<const string&>(y) == cat);
55 }
56
57 void test02()
58 {
59 using std::experimental::bad_any_cast;
60 any x(1);
61 auto p = any_cast<double>(&x);
62 VERIFY(p == nullptr);
63
64 x = 1.0;
65 p = any_cast<double>(&x);
66 VERIFY(p != nullptr);
67
68 x = any();
69 p = any_cast<double>(&x);
70 VERIFY(p == nullptr);
71
72 try {
73 any_cast<double>(x);
74 VERIFY(false);
75 } catch (const bad_any_cast&) {
76 }
77 }
78
79 static int move_count = 0;
80
81 void test03()
82 {
83 struct MoveEnabled
84 {
85 MoveEnabled(MoveEnabled&&)
86 {
87 ++move_count;
88 }
89 MoveEnabled() = default;
90 MoveEnabled(const MoveEnabled&) = default;
91 };
92 MoveEnabled m;
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);
97 struct MoveDeleted
98 {
99 MoveDeleted(MoveDeleted&&) = delete;
100 MoveDeleted() = default;
101 MoveDeleted(const MoveDeleted&) = default;
102 };
103 MoveDeleted md;
104 MoveDeleted&& md2 = any_cast<MoveDeleted>(any(std::move(md)));
105 MoveDeleted&& md3 = any_cast<MoveDeleted&&>(any(std::move(md)));
106 }
107
108 void test04()
109 {
110 // PR libstdc++/69321
111 struct noncopyable {
112 noncopyable(noncopyable const&) = delete;
113 };
114
115 any a;
116 auto p = any_cast<noncopyable>(&a);
117 VERIFY( p == nullptr );
118 }
119
120 int main()
121 {
122 test01();
123 test02();
124 test03();
125 test04();
126 }