| 1 | // Copyright (C) 2018-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do run { target c++17 } } |
| 19 | |
| 20 | #include <any> |
| 21 | #include <new> |
| 22 | #include <testsuite_hooks.h> |
| 23 | |
| 24 | struct E : std::bad_alloc { }; |
| 25 | |
| 26 | struct X |
| 27 | { |
| 28 | X() = default; |
| 29 | X(std::initializer_list<int>) { } |
| 30 | |
| 31 | // Prevents small-object optimization: |
| 32 | X(const X&) noexcept(false) { } |
| 33 | |
| 34 | static void* operator new(std::size_t) { throw E{}; } |
| 35 | static void operator delete(void*, std::size_t) noexcept { } |
| 36 | }; |
| 37 | |
| 38 | void |
| 39 | test01() |
| 40 | { |
| 41 | std::any a; |
| 42 | try |
| 43 | { |
| 44 | a.emplace<X>(); |
| 45 | VERIFY(false); |
| 46 | } |
| 47 | catch (const E&) |
| 48 | { |
| 49 | VERIFY( !a.has_value() ); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | test02() |
| 55 | { |
| 56 | std::any a; |
| 57 | try |
| 58 | { |
| 59 | a.emplace<X>(std::initializer_list<int>{}); |
| 60 | VERIFY(false); |
| 61 | } |
| 62 | catch (const E&) |
| 63 | { |
| 64 | VERIFY( !a.has_value() ); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | int |
| 69 | main() |
| 70 | { |
| 71 | test01(); |
| 72 | test02(); |
| 73 | } |