| 1 | // { dg-do compile { target c++17 } } |
| 2 | |
| 3 | // Copyright (C) 2020-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 | |
| 22 | int i; |
| 23 | |
| 24 | struct Cont |
| 25 | { |
| 26 | Cont() noexcept; |
| 27 | Cont(Cont&&) noexcept; |
| 28 | Cont(const Cont&); |
| 29 | Cont(int); |
| 30 | Cont(std::initializer_list<int>, int) noexcept; |
| 31 | Cont(std::initializer_list<int>, const char*); |
| 32 | }; |
| 33 | const Cont c{}; |
| 34 | |
| 35 | template<typename T, typename = void> |
| 36 | struct can_make_optional1 |
| 37 | : std::false_type |
| 38 | { }; |
| 39 | |
| 40 | template<typename T> |
| 41 | struct can_make_optional1<T, |
| 42 | std::void_t<decltype(std::make_optional(std::declval<T>()))>> |
| 43 | : std::true_type |
| 44 | { }; |
| 45 | |
| 46 | static_assert( can_make_optional1<int>::value ); |
| 47 | static_assert( noexcept(std::make_optional(1)) ); |
| 48 | static_assert( can_make_optional1<int&>::value ); |
| 49 | static_assert( noexcept(std::make_optional(i)) ); |
| 50 | static_assert( ! can_make_optional1<void>::value ); |
| 51 | static_assert( can_make_optional1<Cont>::value ); |
| 52 | static_assert( noexcept(std::make_optional(Cont{})) ); |
| 53 | static_assert( can_make_optional1<Cont>::value ); |
| 54 | static_assert( ! noexcept(std::make_optional(c)) ); |
| 55 | |
| 56 | template<typename T, typename Arg, typename = void> |
| 57 | struct can_make_optional2 |
| 58 | : std::false_type |
| 59 | { }; |
| 60 | |
| 61 | template<typename T, typename Arg> |
| 62 | struct can_make_optional2<T, Arg, |
| 63 | std::void_t<decltype(std::make_optional<T>(std::declval<Arg>()))>> |
| 64 | : std::true_type |
| 65 | { }; |
| 66 | |
| 67 | static_assert( can_make_optional2<int, int>::value ); |
| 68 | static_assert( noexcept(std::make_optional<int>(1)) ); |
| 69 | static_assert( can_make_optional2<int, int&>::value ); |
| 70 | static_assert( noexcept(std::make_optional(i)) ); |
| 71 | static_assert( ! can_make_optional2<void, void>::value ); |
| 72 | static_assert( can_make_optional2<Cont, Cont>::value ); |
| 73 | static_assert( noexcept(std::make_optional<Cont>({})) ); |
| 74 | static_assert( can_make_optional2<Cont, const Cont&>::value ); |
| 75 | static_assert( ! noexcept(std::make_optional(c)) ); |
| 76 | static_assert( can_make_optional2<Cont, int>::value ); |
| 77 | static_assert( ! noexcept(std::make_optional<Cont>(1)) ); |
| 78 | |
| 79 | template<typename T, typename Arg, typename = void> |
| 80 | struct can_make_optional3 |
| 81 | : std::false_type |
| 82 | { }; |
| 83 | |
| 84 | template<typename T, typename Arg> |
| 85 | struct can_make_optional3<T, Arg, |
| 86 | std::void_t<decltype(std::make_optional<T>({1,2}, std::declval<Arg>()))>> |
| 87 | : std::true_type |
| 88 | { }; |
| 89 | |
| 90 | static_assert( can_make_optional3<Cont, int>::value ); |
| 91 | static_assert( noexcept(std::make_optional<Cont>({1,2}, 1)) ); |
| 92 | static_assert( can_make_optional3<Cont, char*>::value ); |
| 93 | static_assert( ! noexcept(std::make_optional<Cont>({1,2}, "")) ); |
| 94 | static_assert( !can_make_optional3<Cont, int*>::value ); |