| 1 | // Copyright (C) 2020-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 compile { target c++17 } } |
| 19 | |
| 20 | #include <optional> |
| 21 | |
| 22 | template<bool B> |
| 23 | struct X |
| 24 | { |
| 25 | X() noexcept(B); |
| 26 | |
| 27 | X(const X&) noexcept(B); |
| 28 | X& operator=(const X&) noexcept(B); |
| 29 | |
| 30 | X(int) noexcept(B); |
| 31 | X& operator=(int) noexcept(false); |
| 32 | |
| 33 | X(void*) noexcept(true); |
| 34 | X& operator=(void*) noexcept(B); |
| 35 | |
| 36 | X(const X<!B>&) noexcept(B); |
| 37 | X& operator=(const X<!B>&) noexcept(B); |
| 38 | |
| 39 | X(std::initializer_list<int>, int) noexcept(B); |
| 40 | }; |
| 41 | |
| 42 | using std::is_nothrow_assignable_v; |
| 43 | |
| 44 | using Xyes = X<true>; |
| 45 | using Xno = X<false>; |
| 46 | using Oyes = std::optional<Xyes>; |
| 47 | using Ono = std::optional<Xno>; |
| 48 | |
| 49 | static_assert( is_nothrow_assignable_v<Oyes, std::nullopt_t> ); |
| 50 | static_assert( is_nothrow_assignable_v<Oyes, const Xyes&> ); |
| 51 | static_assert( is_nothrow_assignable_v<Oyes, Xyes> ); |
| 52 | static_assert( ! is_nothrow_assignable_v<Oyes, int> ); |
| 53 | static_assert( is_nothrow_assignable_v<Oyes, void*> ); |
| 54 | static_assert( is_nothrow_assignable_v<Oyes, const Ono&> ); |
| 55 | static_assert( is_nothrow_assignable_v<Oyes, Ono> ); |
| 56 | |
| 57 | static_assert( is_nothrow_assignable_v<Ono, std::nullopt_t> ); |
| 58 | static_assert( ! is_nothrow_assignable_v<Ono, const Xno&> ); |
| 59 | static_assert( ! is_nothrow_assignable_v<Ono, Xno> ); |
| 60 | static_assert( ! is_nothrow_assignable_v<Ono, int> ); |
| 61 | static_assert( ! is_nothrow_assignable_v<Ono, void*> ); |
| 62 | static_assert( ! is_nothrow_assignable_v<Ono, const Xyes&> ); |
| 63 | static_assert( ! is_nothrow_assignable_v<Ono, Xyes> ); |
| 64 | |
| 65 | Xyes xyes; |
| 66 | Xno xno; |
| 67 | Oyes oyes; |
| 68 | Ono ono; |
| 69 | static_assert( noexcept(oyes.emplace()) ); |
| 70 | static_assert( noexcept(oyes.emplace(xyes)) ); |
| 71 | static_assert( noexcept(oyes.emplace(1)) ); |
| 72 | static_assert( noexcept(oyes.emplace(nullptr)) ); |
| 73 | static_assert( noexcept(oyes.emplace(xno)) ); |
| 74 | static_assert( noexcept(oyes.emplace({1,2,3}, 1)) ); |
| 75 | |
| 76 | static_assert( ! noexcept(ono.emplace()) ); |
| 77 | static_assert( ! noexcept(ono.emplace(xno)) ); |
| 78 | static_assert( ! noexcept(ono.emplace(1)) ); |
| 79 | static_assert( noexcept(ono.emplace(nullptr)) ); |
| 80 | static_assert( ! noexcept(ono.emplace(xyes)) ); |
| 81 | static_assert( ! noexcept(ono.emplace({1,2,3}, 1)) ); |