| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 10 | |
| 11 | // template<class T, class Cat = partial_ordering> |
| 12 | // concept three_way_comparable = // see below |
| 13 | |
| 14 | #include <compare> |
| 15 | #include <cstddef> |
| 16 | |
| 17 | #include "compare_types.h" |
| 18 | #include "test_macros.h" |
| 19 | |
| 20 | namespace fundamentals { |
| 21 | // with default ordering |
| 22 | static_assert(std::three_way_comparable<int>); |
| 23 | static_assert(std::three_way_comparable<double>); |
| 24 | static_assert(std::three_way_comparable<void*>); |
| 25 | static_assert(std::three_way_comparable<char*>); |
| 26 | static_assert(std::three_way_comparable<char const*>); |
| 27 | static_assert(std::three_way_comparable<char volatile*>); |
| 28 | static_assert(std::three_way_comparable<char const volatile*>); |
| 29 | static_assert(std::three_way_comparable<wchar_t&>); |
| 30 | #ifndef TEST_HAS_NO_CHAR8_T |
| 31 | static_assert(std::three_way_comparable<char8_t const&>); |
| 32 | #endif |
| 33 | static_assert(std::three_way_comparable<char16_t volatile&>); |
| 34 | static_assert(std::three_way_comparable<char32_t const volatile&>); |
| 35 | #ifndef TEST_HAS_NO_INT128 |
| 36 | static_assert(std::three_way_comparable<__int128_t const&>); |
| 37 | static_assert(std::three_way_comparable<__uint128_t const&>); |
| 38 | #endif |
| 39 | static_assert(std::three_way_comparable<unsigned char&&>); |
| 40 | static_assert(std::three_way_comparable<unsigned short const&&>); |
| 41 | static_assert(std::three_way_comparable<unsigned int volatile&&>); |
| 42 | static_assert(std::three_way_comparable<unsigned long const volatile&&>); |
| 43 | |
| 44 | // with explicit ordering |
| 45 | static_assert(std::three_way_comparable<int, std::strong_ordering>); |
| 46 | static_assert(std::three_way_comparable<int, std::weak_ordering>); |
| 47 | static_assert(std::three_way_comparable<double, std::partial_ordering>); |
| 48 | static_assert(!std::three_way_comparable<double, std::weak_ordering>); |
| 49 | static_assert(std::three_way_comparable<void*, std::strong_ordering>); |
| 50 | static_assert(std::three_way_comparable<void*, std::weak_ordering>); |
| 51 | static_assert(std::three_way_comparable<char*, std::strong_ordering>); |
| 52 | static_assert(std::three_way_comparable<char*, std::weak_ordering>); |
| 53 | static_assert(std::three_way_comparable<char const*, std::strong_ordering>); |
| 54 | static_assert(std::three_way_comparable<char const*, std::weak_ordering>); |
| 55 | static_assert(std::three_way_comparable<char volatile*, std::strong_ordering>); |
| 56 | static_assert(std::three_way_comparable<char volatile*, std::weak_ordering>); |
| 57 | static_assert(std::three_way_comparable<char const volatile*, std::strong_ordering>); |
| 58 | static_assert(std::three_way_comparable<char const volatile*, std::weak_ordering>); |
| 59 | static_assert(std::three_way_comparable<wchar_t&, std::strong_ordering>); |
| 60 | static_assert(std::three_way_comparable<wchar_t&, std::weak_ordering>); |
| 61 | static_assert(std::three_way_comparable<char8_t const&, std::strong_ordering>); |
| 62 | static_assert(std::three_way_comparable<char8_t const&, std::weak_ordering>); |
| 63 | static_assert(std::three_way_comparable<char16_t volatile&, std::strong_ordering>); |
| 64 | static_assert(std::three_way_comparable<char16_t volatile&, std::weak_ordering>); |
| 65 | static_assert(std::three_way_comparable<char32_t const volatile&, std::strong_ordering>); |
| 66 | static_assert(std::three_way_comparable<char32_t const volatile&, std::weak_ordering>); |
| 67 | static_assert(std::three_way_comparable<unsigned char&&, std::strong_ordering>); |
| 68 | static_assert(std::three_way_comparable<unsigned char&&, std::weak_ordering>); |
| 69 | static_assert(std::three_way_comparable<unsigned short const&&, std::strong_ordering>); |
| 70 | static_assert(std::three_way_comparable<unsigned short const&&, std::weak_ordering>); |
| 71 | static_assert(std::three_way_comparable<unsigned int volatile&&, std::strong_ordering>); |
| 72 | static_assert(std::three_way_comparable<unsigned int volatile&&, std::weak_ordering>); |
| 73 | static_assert(std::three_way_comparable<unsigned long const volatile&&, std::strong_ordering>); |
| 74 | static_assert(std::three_way_comparable<unsigned long const volatile&&, std::weak_ordering>); |
| 75 | |
| 76 | static_assert(!std::three_way_comparable<int[5]>); |
| 77 | static_assert(!std::three_way_comparable<int (*)(int)>); |
| 78 | static_assert(!std::three_way_comparable<int (&)(int)>); |
| 79 | static_assert(!std::three_way_comparable<int (*)(int) noexcept>); |
| 80 | static_assert(!std::three_way_comparable<int (&)(int) noexcept>); |
| 81 | static_assert(!std::three_way_comparable<std::nullptr_t>); |
| 82 | static_assert(!std::three_way_comparable<void>); |
| 83 | |
| 84 | struct S {}; |
| 85 | static_assert(!std::three_way_comparable<int S::*>); |
| 86 | static_assert(!std::three_way_comparable<int (S::*)()>); |
| 87 | static_assert(!std::three_way_comparable<int (S::*)() noexcept>); |
| 88 | static_assert(!std::three_way_comparable<int (S::*)() &>); |
| 89 | static_assert(!std::three_way_comparable<int (S::*)() & noexcept>); |
| 90 | static_assert(!std::three_way_comparable<int (S::*)() &&>); |
| 91 | static_assert(!std::three_way_comparable<int (S::*)() && noexcept>); |
| 92 | static_assert(!std::three_way_comparable<int (S::*)() const>); |
| 93 | static_assert(!std::three_way_comparable<int (S::*)() const noexcept>); |
| 94 | static_assert(!std::three_way_comparable<int (S::*)() const&>); |
| 95 | static_assert(!std::three_way_comparable<int (S::*)() const & noexcept>); |
| 96 | static_assert(!std::three_way_comparable<int (S::*)() const&&>); |
| 97 | static_assert(!std::three_way_comparable<int (S::*)() const && noexcept>); |
| 98 | static_assert(!std::three_way_comparable<int (S::*)() volatile>); |
| 99 | static_assert(!std::three_way_comparable<int (S::*)() volatile noexcept>); |
| 100 | static_assert(!std::three_way_comparable<int (S::*)() volatile&>); |
| 101 | static_assert(!std::three_way_comparable<int (S::*)() volatile & noexcept>); |
| 102 | static_assert(!std::three_way_comparable<int (S::*)() volatile&&>); |
| 103 | static_assert(!std::three_way_comparable<int (S::*)() volatile && noexcept>); |
| 104 | static_assert(!std::three_way_comparable<int (S::*)() const volatile>); |
| 105 | static_assert(!std::three_way_comparable<int (S::*)() const volatile noexcept>); |
| 106 | static_assert(!std::three_way_comparable<int (S::*)() const volatile&>); |
| 107 | static_assert(!std::three_way_comparable<int (S::*)() const volatile & noexcept>); |
| 108 | static_assert(!std::three_way_comparable<int (S::*)() const volatile&&>); |
| 109 | static_assert(!std::three_way_comparable<int (S::*)() const volatile && noexcept>); |
| 110 | } // namespace fundamentals |
| 111 | |
| 112 | namespace user_defined { |
| 113 | |
| 114 | struct S { |
| 115 | auto operator<=>(const S&) const = default; |
| 116 | }; |
| 117 | |
| 118 | static_assert(std::three_way_comparable<S>); |
| 119 | static_assert(std::three_way_comparable<S, std::strong_ordering>); |
| 120 | static_assert(std::three_way_comparable<S, std::partial_ordering>); |
| 121 | |
| 122 | struct SpaceshipNotDeclared { |
| 123 | }; |
| 124 | |
| 125 | static_assert(!std::three_way_comparable<SpaceshipNotDeclared>); |
| 126 | |
| 127 | struct SpaceshipDeleted { |
| 128 | auto operator<=>(const SpaceshipDeleted&) const = delete; |
| 129 | }; |
| 130 | |
| 131 | static_assert(!std::three_way_comparable<SpaceshipDeleted>); |
| 132 | |
| 133 | struct SpaceshipWithoutEqualityOperator { |
| 134 | auto operator<=>(const SpaceshipWithoutEqualityOperator&) const; |
| 135 | }; |
| 136 | |
| 137 | static_assert(!std::three_way_comparable<SpaceshipWithoutEqualityOperator>); |
| 138 | |
| 139 | struct EqualityOperatorDeleted { |
| 140 | bool operator==(const EqualityOperatorDeleted&) const = delete; |
| 141 | }; |
| 142 | |
| 143 | static_assert(!std::three_way_comparable<EqualityOperatorDeleted>); |
| 144 | |
| 145 | struct EqualityOperatorOnly { |
| 146 | bool operator==(const EqualityOperatorOnly&) const = default; |
| 147 | }; |
| 148 | |
| 149 | static_assert(!std::three_way_comparable<EqualityOperatorOnly>); |
| 150 | |
| 151 | struct SpaceshipDeclaredEqualityOperatorDeleted { |
| 152 | bool operator==(const SpaceshipDeclaredEqualityOperatorDeleted&) const = delete; |
| 153 | auto operator<=>(const SpaceshipDeclaredEqualityOperatorDeleted&) const = default; |
| 154 | }; |
| 155 | |
| 156 | static_assert(!std::three_way_comparable<SpaceshipDeclaredEqualityOperatorDeleted>); |
| 157 | |
| 158 | struct AllInequalityOperators { |
| 159 | bool operator<(const AllInequalityOperators&) const; |
| 160 | bool operator<=(const AllInequalityOperators&) const; |
| 161 | bool operator>(const AllInequalityOperators&) const; |
| 162 | bool operator>=(const AllInequalityOperators&) const; |
| 163 | bool operator!=(const AllInequalityOperators&) const; |
| 164 | }; |
| 165 | |
| 166 | static_assert(!std::three_way_comparable<AllInequalityOperators>); |
| 167 | |
| 168 | struct AllComparisonOperators { |
| 169 | bool operator<(const AllComparisonOperators&) const; |
| 170 | bool operator<=(const AllComparisonOperators&) const; |
| 171 | bool operator>(const AllComparisonOperators&) const; |
| 172 | bool operator>=(const AllComparisonOperators&) const; |
| 173 | bool operator!=(const AllComparisonOperators&) const; |
| 174 | bool operator==(const AllComparisonOperators&) const; |
| 175 | }; |
| 176 | |
| 177 | static_assert(!std::three_way_comparable<AllComparisonOperators>); |
| 178 | |
| 179 | struct AllButOneInequalityOperators { |
| 180 | bool operator<(const AllButOneInequalityOperators&) const; |
| 181 | bool operator<=(const AllButOneInequalityOperators&) const; |
| 182 | bool operator>(const AllButOneInequalityOperators&) const; |
| 183 | bool operator!=(const AllButOneInequalityOperators&) const; |
| 184 | }; |
| 185 | |
| 186 | static_assert(!std::three_way_comparable<AllButOneInequalityOperators>); |
| 187 | |
| 188 | struct AllInequalityOperatorsOneDeleted { |
| 189 | bool operator<(const AllInequalityOperatorsOneDeleted&) const; |
| 190 | bool operator<=(const AllInequalityOperatorsOneDeleted&) const; |
| 191 | bool operator>(const AllInequalityOperatorsOneDeleted&) const; |
| 192 | bool operator>=(const AllInequalityOperatorsOneDeleted&) const = delete; |
| 193 | bool operator!=(const AllInequalityOperatorsOneDeleted&) const; |
| 194 | }; |
| 195 | |
| 196 | static_assert(!std::three_way_comparable<AllInequalityOperatorsOneDeleted>); |
| 197 | |
| 198 | struct EqualityOperatorWrongReturnType { |
| 199 | int operator==(const EqualityOperatorWrongReturnType&); |
| 200 | auto operator<=>(const EqualityOperatorWrongReturnType&) const = default; |
| 201 | }; |
| 202 | |
| 203 | static_assert(!std::three_way_comparable<EqualityOperatorWrongReturnType>); |
| 204 | |
| 205 | struct SpaceshipWrongReturnType { |
| 206 | bool operator==(const SpaceshipWrongReturnType&) const = default; |
| 207 | int operator<=>(const SpaceshipWrongReturnType&); |
| 208 | }; |
| 209 | |
| 210 | static_assert(!std::three_way_comparable<SpaceshipWrongReturnType>); |
| 211 | |
| 212 | struct EqualityOperatorNonConstArgument { |
| 213 | bool operator==(EqualityOperatorNonConstArgument&); |
| 214 | auto operator<=>(const EqualityOperatorNonConstArgument&) const = default; |
| 215 | }; |
| 216 | |
| 217 | static_assert(!std::three_way_comparable<EqualityOperatorNonConstArgument>); |
| 218 | |
| 219 | struct SpaceshipNonConstArgument { |
| 220 | bool operator==(const SpaceshipNonConstArgument&) const = default; |
| 221 | auto operator<=>(SpaceshipNonConstArgument&); |
| 222 | }; |
| 223 | |
| 224 | static_assert(!std::three_way_comparable<SpaceshipNonConstArgument>); |
| 225 | } // namespace user_defined |
| 226 | |