| 1 | // { dg-do run { target c++17 } } |
| 2 | |
| 3 | // Copyright (C) 2016-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 <optional> |
| 21 | #include <any> |
| 22 | |
| 23 | using std::optional; |
| 24 | |
| 25 | #include <testsuite_hooks.h> |
| 26 | |
| 27 | void test01() |
| 28 | { |
| 29 | optional<optional<int>> nested_element; |
| 30 | optional<int> element = {}; |
| 31 | nested_element = element; |
| 32 | VERIFY(nested_element); |
| 33 | } |
| 34 | |
| 35 | template <class T> |
| 36 | struct service_result |
| 37 | { |
| 38 | static optional<T> get_result() |
| 39 | { |
| 40 | T sr; |
| 41 | return sr; |
| 42 | } |
| 43 | |
| 44 | static optional<T> get_result_with_cond(bool cond) |
| 45 | { |
| 46 | if (cond) |
| 47 | return T{}; |
| 48 | return {}; |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | void test02() |
| 53 | { |
| 54 | VERIFY(service_result<int>::get_result()); |
| 55 | VERIFY(service_result<optional<int>>::get_result()); |
| 56 | VERIFY(service_result<int>::get_result_with_cond(true)); |
| 57 | VERIFY(service_result<optional<int>>::get_result_with_cond(true)); |
| 58 | VERIFY(!service_result<int>::get_result_with_cond(false)); |
| 59 | VERIFY(!service_result<optional<int>>::get_result_with_cond(false)); |
| 60 | } |
| 61 | |
| 62 | struct Widget |
| 63 | { |
| 64 | Widget(int) {} |
| 65 | Widget(optional<int>) {} |
| 66 | }; |
| 67 | |
| 68 | |
| 69 | void test03() |
| 70 | { |
| 71 | optional<Widget> w; |
| 72 | w = optional<int>(); |
| 73 | VERIFY(w); |
| 74 | static_assert(!std::is_assignable_v<optional<Widget>&, |
| 75 | optional<short>>);; |
| 76 | w = optional<optional<int>>(); |
| 77 | VERIFY(!w); |
| 78 | static_assert(!std::is_assignable_v<optional<Widget>&, |
| 79 | optional<optional<short>>>);; |
| 80 | |
| 81 | optional<Widget> w2{optional<int>()}; |
| 82 | VERIFY(w2); |
| 83 | optional<Widget> w3 = optional<int>(); |
| 84 | VERIFY(w3); |
| 85 | optional<Widget> w4{optional<short>()}; |
| 86 | VERIFY(w4); |
| 87 | static_assert(!std::is_convertible_v<optional<short>&&, optional<Widget>>); |
| 88 | |
| 89 | optional<Widget> w6{optional<optional<int>>()}; |
| 90 | VERIFY(!w6); |
| 91 | optional<Widget> w7 = optional<optional<int>>(); |
| 92 | VERIFY(!w7); |
| 93 | optional<Widget> w8{optional<optional<short>>()}; |
| 94 | VERIFY(!w8); |
| 95 | static_assert(!std::is_convertible_v<optional<optional<short>>&&, |
| 96 | optional<Widget>>); |
| 97 | optional<Widget> w10{optional<optional<short>>(10)}; |
| 98 | VERIFY(w10); |
| 99 | optional<Widget> w11 = std::nullopt; |
| 100 | VERIFY(!w11); |
| 101 | optional<Widget> w12 = {}; |
| 102 | VERIFY(!w12); |
| 103 | optional<Widget> w13{std::nullopt}; |
| 104 | VERIFY(!w13); |
| 105 | optional<Widget> w14; |
| 106 | w14 = {}; |
| 107 | VERIFY(!w14); |
| 108 | } |
| 109 | |
| 110 | struct Widget2 |
| 111 | { |
| 112 | Widget2(int) {} |
| 113 | Widget2(optional<int>) {} |
| 114 | Widget2& operator=(int) {return *this;} |
| 115 | Widget2& operator=(optional<int>) {return *this;} |
| 116 | }; |
| 117 | |
| 118 | void test04() |
| 119 | { |
| 120 | optional<Widget2> w; |
| 121 | w = optional<int>(); |
| 122 | VERIFY(w); |
| 123 | w = optional<short>(); |
| 124 | VERIFY(w); |
| 125 | w = optional<optional<int>>(); |
| 126 | VERIFY(!w); |
| 127 | w = optional<optional<short>>(); |
| 128 | VERIFY(!w); |
| 129 | w = optional<optional<short>>(10); |
| 130 | optional<Widget2> w2 = std::nullopt; |
| 131 | VERIFY(!w2); |
| 132 | optional<Widget2> w3 = {}; |
| 133 | VERIFY(!w3); |
| 134 | optional<Widget2> w4{std::nullopt}; |
| 135 | VERIFY(!w4); |
| 136 | optional<Widget2> w5; |
| 137 | w5 = {}; |
| 138 | VERIFY(!w5); |
| 139 | } |
| 140 | |
| 141 | struct Thingy |
| 142 | { |
| 143 | Thingy(int) {} |
| 144 | Thingy(Widget) {} |
| 145 | }; |
| 146 | |
| 147 | void test05() |
| 148 | { |
| 149 | optional<Thingy> ot; |
| 150 | |
| 151 | static_assert(!std::is_assignable_v<optional<Thingy>&, |
| 152 | optional<int>>); |
| 153 | static_assert(std::is_assignable_v<optional<Thingy>&, |
| 154 | optional<short>>); |
| 155 | static_assert(!std::is_assignable_v<optional<Thingy>&, |
| 156 | optional<optional<int>>>); |
| 157 | ot = optional<Widget>(); |
| 158 | VERIFY(!ot); |
| 159 | optional<Thingy> ot2{optional<int>()}; |
| 160 | VERIFY(ot2); |
| 161 | static_assert(!std::is_convertible_v<optional<int>&&, |
| 162 | optional<Thingy>>); |
| 163 | optional<Thingy> ot3{optional<short>()}; |
| 164 | VERIFY(!ot3); |
| 165 | optional<Thingy> ot4 = optional<short>(); |
| 166 | VERIFY(!ot4); |
| 167 | |
| 168 | optional<Thingy> ot5{optional<optional<int>>()}; |
| 169 | VERIFY(!ot5); |
| 170 | static_assert(!std::is_convertible_v<optional<optional<int>>&&, |
| 171 | optional<Thingy>>); |
| 172 | |
| 173 | optional<Thingy> ot7{optional<Widget>()}; |
| 174 | VERIFY(!ot7); |
| 175 | optional<Thingy> ot8 = optional<Widget>(); |
| 176 | VERIFY(!ot8); |
| 177 | static_assert(!std::is_constructible_v<optional<Thingy>, |
| 178 | optional<optional<short>>>); |
| 179 | static_assert(!std::is_convertible_v<optional<optional<short>>, |
| 180 | optional<Thingy>>); |
| 181 | static_assert(!std::is_assignable_v<optional<Thingy>&, |
| 182 | optional<optional<short>>>); |
| 183 | optional<Thingy> ot9 = std::nullopt; |
| 184 | VERIFY(!ot9); |
| 185 | optional<Thingy> ot10 = {}; |
| 186 | VERIFY(!ot10); |
| 187 | optional<Thingy> ot11{std::nullopt}; |
| 188 | VERIFY(!ot11); |
| 189 | optional<Thingy> ot12; |
| 190 | ot12 = {}; |
| 191 | VERIFY(!ot12); |
| 192 | } |
| 193 | |
| 194 | struct RvalueConstructible |
| 195 | { |
| 196 | RvalueConstructible(int) {} |
| 197 | RvalueConstructible(optional<int>&&) {} |
| 198 | }; |
| 199 | |
| 200 | void test06() |
| 201 | { |
| 202 | optional<int> oi; |
| 203 | optional<RvalueConstructible> ori; |
| 204 | static_assert(!std::is_assignable_v<optional<RvalueConstructible>&, |
| 205 | optional<int>&>); |
| 206 | ori = std::move(oi); |
| 207 | VERIFY(ori); |
| 208 | |
| 209 | optional<optional<int>> ooi; |
| 210 | static_assert(!std::is_assignable_v<optional<RvalueConstructible>&, |
| 211 | optional<optional<int>>&>); |
| 212 | ori = std::move(ooi); |
| 213 | VERIFY(!ori); |
| 214 | |
| 215 | static_assert(!std::is_constructible_v<optional<RvalueConstructible>, |
| 216 | optional<int>&>); |
| 217 | static_assert(!std::is_convertible_v<optional<int>&, |
| 218 | optional<RvalueConstructible>>); |
| 219 | |
| 220 | optional<RvalueConstructible> ori2(std::move(oi)); |
| 221 | VERIFY(ori2); |
| 222 | optional<RvalueConstructible> ori3 = std::move(oi); |
| 223 | VERIFY(ori3); |
| 224 | |
| 225 | static_assert(!std::is_constructible_v<optional<RvalueConstructible>, |
| 226 | optional<optional<int>>&>); |
| 227 | static_assert(!std::is_convertible_v<optional<optional<int>>&, |
| 228 | optional<RvalueConstructible>>); |
| 229 | optional<RvalueConstructible> ori6(std::move(ooi)); |
| 230 | VERIFY(!ori6); |
| 231 | optional<RvalueConstructible> ori7 = std::move(ooi); |
| 232 | VERIFY(!ori7); |
| 233 | optional<RvalueConstructible> ori8 = std::nullopt; |
| 234 | VERIFY(!ori8); |
| 235 | optional<RvalueConstructible> ori9 = {}; |
| 236 | VERIFY(!ori9); |
| 237 | optional<RvalueConstructible> ori10{std::nullopt}; |
| 238 | VERIFY(!ori10); |
| 239 | optional<RvalueConstructible> ori11; |
| 240 | ori11 = {}; |
| 241 | VERIFY(!ori11); |
| 242 | } |
| 243 | |
| 244 | struct Thingy2 |
| 245 | { |
| 246 | Thingy2(int) {} |
| 247 | explicit Thingy2(optional<int>) {} |
| 248 | Thingy2(Widget) {} |
| 249 | }; |
| 250 | |
| 251 | void test07() |
| 252 | { |
| 253 | optional<Thingy2> ot{optional<int>{}}; |
| 254 | VERIFY(ot); |
| 255 | static_assert(!std::is_convertible_v<optional<int>, |
| 256 | optional<Thingy2>>); |
| 257 | optional<Thingy2> ot2{optional<short>{}}; |
| 258 | VERIFY(ot2); |
| 259 | static_assert(!std::is_convertible_v<optional<short>, |
| 260 | optional<Thingy2>>); |
| 261 | optional<Thingy2> ot3{optional<optional<int>>{}}; |
| 262 | VERIFY(!ot3); |
| 263 | static_assert(!std::is_convertible_v<optional<optional<int>>, |
| 264 | optional<Thingy2>>); |
| 265 | optional<Thingy2> ot4{optional<optional<short>>{}}; |
| 266 | VERIFY(!ot4); |
| 267 | static_assert(!std::is_convertible_v<optional<optional<short>>, |
| 268 | optional<Thingy2>>); |
| 269 | |
| 270 | optional<Thingy2> ot5{optional<Widget>{}}; |
| 271 | VERIFY(!ot5); |
| 272 | optional<Thingy2> ot6 = optional<Widget>(); |
| 273 | VERIFY(!ot6); |
| 274 | |
| 275 | static_assert(!std::is_assignable_v<optional<Thingy2>&, |
| 276 | optional<int>>); |
| 277 | static_assert(!std::is_assignable_v<optional<Thingy2>&, |
| 278 | optional<short>>); |
| 279 | static_assert(!std::is_assignable_v<optional<Thingy2>&, |
| 280 | optional<optional<int>>>); |
| 281 | static_assert(!std::is_assignable_v<optional<Thingy2>&, |
| 282 | optional<optional<short>>>); |
| 283 | optional<Thingy2> ot7; |
| 284 | ot = optional<Widget>(); |
| 285 | VERIFY(!ot7); |
| 286 | optional<Thingy2> ot8 = std::nullopt; |
| 287 | VERIFY(!ot8); |
| 288 | optional<Thingy2> ot9 = {}; |
| 289 | VERIFY(!ot9); |
| 290 | optional<Thingy2> ot10{std::nullopt}; |
| 291 | VERIFY(!ot10); |
| 292 | optional<Thingy2> ot11; |
| 293 | ot11 = {}; |
| 294 | VERIFY(!ot11); |
| 295 | } |
| 296 | |
| 297 | struct Thingy3 |
| 298 | { |
| 299 | Thingy3(int) {} |
| 300 | template<class... Args, |
| 301 | std::enable_if_t<std::is_constructible_v<Widget, Args&&...>, |
| 302 | bool> = true> |
| 303 | explicit Thingy3(Args&&... args) {} |
| 304 | Thingy3(Widget) {} |
| 305 | }; |
| 306 | |
| 307 | void test08() |
| 308 | { |
| 309 | optional<Thingy3> ot{optional<int>{}}; |
| 310 | VERIFY(ot); |
| 311 | static_assert(!std::is_convertible_v<optional<int>, |
| 312 | optional<Thingy3>>); |
| 313 | optional<Thingy3> ot2{optional<short>{}}; |
| 314 | VERIFY(ot2); |
| 315 | static_assert(!std::is_convertible_v<optional<short>, |
| 316 | optional<Thingy3>>); |
| 317 | optional<Thingy3> ot3{optional<optional<int>>{}}; |
| 318 | VERIFY(!ot3); |
| 319 | static_assert(!std::is_convertible_v<optional<optional<int>>, |
| 320 | optional<Thingy3>>); |
| 321 | optional<Thingy3> ot4{optional<optional<short>>{}}; |
| 322 | VERIFY(!ot4); |
| 323 | static_assert(!std::is_convertible_v<optional<optional<short>>, |
| 324 | optional<Thingy3>>); |
| 325 | |
| 326 | optional<Thingy3> ot5{optional<Widget>{}}; |
| 327 | VERIFY(!ot5); |
| 328 | optional<Thingy3> ot6 = optional<Widget>(); |
| 329 | VERIFY(!ot6); |
| 330 | |
| 331 | static_assert(!std::is_assignable_v<optional<Thingy3>&, |
| 332 | optional<int>>); |
| 333 | static_assert(!std::is_assignable_v<optional<Thingy3>&, |
| 334 | optional<short>>); |
| 335 | static_assert(!std::is_assignable_v<optional<Thingy3>&, |
| 336 | optional<optional<int>>>); |
| 337 | static_assert(!std::is_assignable_v<optional<Thingy3>&, |
| 338 | optional<optional<short>>>); |
| 339 | optional<Thingy3> ot7; |
| 340 | ot = optional<Widget>(); |
| 341 | VERIFY(!ot7); |
| 342 | optional<Thingy3> ot8 = std::nullopt; |
| 343 | VERIFY(!ot8); |
| 344 | optional<Thingy3> ot9 = {}; |
| 345 | VERIFY(!ot9); |
| 346 | optional<Thingy3> ot10{std::nullopt}; |
| 347 | VERIFY(!ot10); |
| 348 | optional<Thingy3> ot11; |
| 349 | ot11 = {}; |
| 350 | VERIFY(!ot11); |
| 351 | } |
| 352 | |
| 353 | void test09() |
| 354 | { |
| 355 | std::any a = 42; |
| 356 | optional<std::any> oa2 = a; |
| 357 | VERIFY(oa2); |
| 358 | VERIFY(std::any_cast<int>(*oa2) == 42); |
| 359 | optional<std::any> oa3 = oa2; |
| 360 | VERIFY(oa3); |
| 361 | VERIFY(std::any_cast<int>(*oa3) == 42); |
| 362 | optional<std::any> oa4{oa2}; |
| 363 | VERIFY(oa4); |
| 364 | VERIFY(std::any_cast<int>(*oa4) == 42); |
| 365 | optional<std::any> oa5(oa2); |
| 366 | VERIFY(oa5); |
| 367 | VERIFY(std::any_cast<int>(*oa5) == 42); |
| 368 | optional<std::any> oa6; |
| 369 | VERIFY(!oa6); |
| 370 | optional<std::any> oa7 = oa6; |
| 371 | VERIFY(!oa7); |
| 372 | optional<std::any> oa8{oa6}; |
| 373 | VERIFY(!oa8); |
| 374 | optional<std::any> oa9(oa6); |
| 375 | VERIFY(!oa9); |
| 376 | } |
| 377 | |
| 378 | void test10() |
| 379 | { |
| 380 | struct X {}; |
| 381 | optional<int> oi(std::in_place); |
| 382 | oi = {}; |
| 383 | VERIFY(oi.has_value() == false); |
| 384 | optional<X> ot(std::in_place); |
| 385 | ot = {}; |
| 386 | VERIFY(ot.has_value() == false); |
| 387 | optional<int> oi2(std::in_place); |
| 388 | short int si = 6; |
| 389 | oi2 = si; |
| 390 | } |
| 391 | |
| 392 | int main() |
| 393 | { |
| 394 | test01(); |
| 395 | test02(); |
| 396 | test03(); |
| 397 | test04(); |
| 398 | test05(); |
| 399 | test06(); |
| 400 | test07(); |
| 401 | test08(); |
| 402 | test09(); |
| 403 | test10(); |
| 404 | } |