| 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 |
void |
| 23 |
test01() |
| 24 |
{ |
| 25 |
using O = std::optional<int>; |
| 26 |
|
| 27 |
static_assert( std::is_eq(O{} <=> O{}) ); |
| 28 |
static_assert( std::is_lt(O{} <=> O{1}) ); |
| 29 |
static_assert( std::is_gt(O{1} <=> O{}) ); |
| 30 |
static_assert( std::is_eq(O{1} <=> O{1}) ); |
| 31 |
static_assert( std::is_lt(O{1} <=> O{2}) ); |
| 32 |
|
| 33 |
static_assert( O{} == O{} ); |
| 34 |
static_assert( O{} < O{1} ); |
| 35 |
static_assert( O{1} > O{} ); |
| 36 |
static_assert( O{1} == O{1} ); |
| 37 |
static_assert( O{1} != O{2} ); |
| 38 |
static_assert( O{1} < O{2} ); |
| 39 |
|
| 40 |
using Os = std::optional<short>; |
| 41 |
static_assert( std::is_eq(O{} <=> Os{}) ); |
| 42 |
static_assert( std::is_lt(O{} <=> Os{1}) ); |
| 43 |
static_assert( std::is_gt(O{1} <=> Os{}) ); |
| 44 |
static_assert( std::is_eq(O{1} <=> Os{1}) ); |
| 45 |
static_assert( std::is_lt(O{1} <=> Os{2}) ); |
| 46 |
|
| 47 |
static_assert( O{} == Os{} ); |
| 48 |
static_assert( O{} < Os{1} ); |
| 49 |
static_assert( O{1} > Os{} ); |
| 50 |
static_assert( O{1} == Os{1} ); |
| 51 |
static_assert( O{1} != Os{2} ); |
| 52 |
static_assert( O{1} < Os{2} ); |
| 53 |
|
| 54 |
// Would requires narrowing conversion to make operands the same type: |
| 55 |
static_assert( !std::three_way_comparable_with<O, std::optional<unsigned>> ); |
| 56 |
} |
| 57 |
|
| 58 |
void |
| 59 |
test02() |
| 60 |
{ |
| 61 |
using O = std::optional<int>; |
| 62 |
using std::nullopt; |
| 63 |
|
| 64 |
static_assert( std::is_eq(O{} <=> nullopt) ); |
| 65 |
static_assert( std::is_gt(O{1} <=> nullopt) ); |
| 66 |
static_assert( std::is_lt(nullopt <=> O{1}) ); |
| 67 |
|
| 68 |
static_assert( O{} == nullopt ); |
| 69 |
static_assert( O{1} != nullopt ); |
| 70 |
static_assert( nullopt != O{1} ); |
| 71 |
static_assert( O{1} > nullopt ); |
| 72 |
static_assert( nullopt < O{1} ); |
| 73 |
static_assert( nullopt <= O{} ); |
| 74 |
static_assert( nullopt <= O{1} ); |
| 75 |
} |
| 76 |
|
| 77 |
template<typename T> |
| 78 |
concept has_spaceship = requires (const T& t) { t <=> t; }; |
| 79 |
|
| 80 |
void |
| 81 |
test03() |
| 82 |
{ |
| 83 |
struct E |
| 84 |
{ |
| 85 |
auto operator<=>(const E&) const { return std::strong_ordering::equal; } |
| 86 |
}; |
| 87 |
static_assert( !std::three_way_comparable<E> ); // not equality comparable |
| 88 |
using O = std::optional<E>; |
| 89 |
static_assert( !std::three_way_comparable<O> ); |
| 90 |
static_assert( ! has_spaceship<O> ); // PR libstdc++/98842 |
| 91 |
struct U : O { }; |
| 92 |
static_assert( ! has_spaceship<U> ); |
| 93 |
} |