| 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++20 } } |
| 19 | |
| 20 | #include <optional> |
| 21 | |
| 22 | struct X |
| 23 | { |
| 24 | template <typename T> |
| 25 | bool operator==(const T&) /* not const */ { return false; } |
| 26 | |
| 27 | template <typename T> |
| 28 | bool operator!=(const T&) /* not const */ { return false; } |
| 29 | |
| 30 | template <typename T> |
| 31 | bool operator<(const T&) /* not const */ { return false; } |
| 32 | |
| 33 | template <typename T> |
| 34 | bool operator>(const T&) /* not const */ { return false; } |
| 35 | |
| 36 | template <typename T> |
| 37 | bool operator<=(const T&) /* not const */ { return false; } |
| 38 | |
| 39 | template <typename T> |
| 40 | bool operator>=(const T&) /* not const */ { return false; } |
| 41 | }; |
| 42 | |
| 43 | void test01() |
| 44 | { |
| 45 | // PR 96269 optional comparison with nullopt fails |
| 46 | std::optional<X> x; |
| 47 | bool eq [[maybe_unused]] = std::nullopt == x; |
| 48 | |
| 49 | bool ne [[maybe_unused]] = std::nullopt != x; |
| 50 | bool lt [[maybe_unused]] = std::nullopt < x; |
| 51 | bool gt [[maybe_unused]] = std::nullopt > x; |
| 52 | bool le [[maybe_unused]] = std::nullopt <= x; |
| 53 | bool ge [[maybe_unused]] = std::nullopt >= x; |
| 54 | } |
| 55 | |
| 56 | template<typename T> |
| 57 | concept optional_lt_cmp |
| 58 | = requires(std::optional<T> o, T t) { { o < t } -> std::same_as<bool>; }; |
| 59 | |
| 60 | template<typename T> |
| 61 | concept optional_gt_cmp |
| 62 | = requires(std::optional<T> o, T t) { { o > t } -> std::same_as<bool>; }; |
| 63 | |
| 64 | template<typename T> |
| 65 | concept optional_le_cmp |
| 66 | = requires(std::optional<T> o, T t) { { o <= t } -> std::same_as<bool>; }; |
| 67 | |
| 68 | template<typename T> |
| 69 | concept optional_ge_cmp |
| 70 | = requires(std::optional<T> o, T t) { { o >= t } -> std::same_as<bool>; }; |
| 71 | |
| 72 | static_assert( ! optional_lt_cmp<X> ); |
| 73 | static_assert( ! optional_gt_cmp<X> ); |
| 74 | static_assert( ! optional_le_cmp<X> ); |
| 75 | static_assert( ! optional_ge_cmp<X> ); |