| 1 | // { dg-do run { target c++17 } } |
| 2 | |
| 3 | // Copyright (C) 2014-2025 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 <any> |
| 21 | #include <string> |
| 22 | #include <utility> |
| 23 | #include <cstring> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | using std::any; |
| 27 | using std::any_cast; |
| 28 | |
| 29 | void 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 |
| 39 | VERIFY(any_cast<int>(x) == 10); |
| 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"); |
| 48 | s = move(any_cast<string&>(x)); // move from any |
| 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 | |
| 58 | void 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 | |
| 80 | static int move_count = 0; |
| 81 | |
| 82 | void 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); |
| 98 | } |
| 99 | |
| 100 | void 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 | |
| 112 | void 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 | |
| 124 | void 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 | |
| 156 | void test07() |
| 157 | { |
| 158 | int arr[3]; |
| 159 | any a(arr); |
| 160 | #if __cpp_rtti |
| 161 | VERIFY( a.type() == typeid(int*) ); // contained value is decayed |
| 162 | #endif |
| 163 | |
| 164 | int (*p1)[3] = any_cast<int[3]>(&a); |
| 165 | #if __cpp_rtti |
| 166 | VERIFY( a.type() != typeid(int[3]) ); // so any_cast should return nullptr |
| 167 | #endif |
| 168 | VERIFY( p1 == nullptr ); |
| 169 | int (*p2)[] = any_cast<int[]>(&a); |
| 170 | #if __cpp_rtti |
| 171 | VERIFY( a.type() != typeid(int[]) ); // so any_cast should return nullptr |
| 172 | #endif |
| 173 | VERIFY( p2 == nullptr ); |
| 174 | const int (*p3)[] = any_cast<int[]>(&std::as_const(a)); |
| 175 | VERIFY( p3 == nullptr ); |
| 176 | } |
| 177 | |
| 178 | int main() |
| 179 | { |
| 180 | test01(); |
| 181 | test02(); |
| 182 | test03(); |
| 183 | test04(); |
| 184 | test05(); |
| 185 | test06(); |
| 186 | test07(); |
| 187 | } |