1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// <optional>
// [optional.comp.with.t], comparison with T
// template<class T, class U>
// requires (!is-derived-from-optional<U>) && three_way_comparable_with<T, U>
// constexpr compare_three_way_result_t<T, U>
// operator<=>(const optional<T>&, const U&);
#include <cassert>
#include <compare>
#include <optional>
#include "test_comparisons.h"
struct SomeInt {
int value_;
constexpr explicit SomeInt(int value = 0) : value_(value) {}
auto operator<=>(const SomeInt&) const = default;
};
template <class T, class U>
concept HasSpaceship = requires(T t, U u) { t <=> u; };
// SFINAE tests.
static_assert(std::three_way_comparable_with<std::optional<int>, std::optional<int>>);
static_assert(HasSpaceship<std::optional<int>, std::optional<int>>);
static_assert(std::three_way_comparable_with<std::optional<SomeInt>, std::optional<SomeInt>>);
static_assert(HasSpaceship<std::optional<SomeInt>, std::optional<SomeInt>>);
static_assert(!HasSpaceship<std::optional<int>, std::optional<SomeInt>>);
// Runtime and static tests.
constexpr void test_custom_integral() {
{
SomeInt t{3};
std::optional<SomeInt> op{3};
assert((t <=> op) == std::strong_ordering::equal);
assert(testOrder(t, op, std::strong_ordering::equal));
}
{
SomeInt t{2};
std::optional<SomeInt> op{3};
assert((t <=> op) == std::strong_ordering::less);
assert(testOrder(t, op, std::strong_ordering::less));
}
{
SomeInt t{3};
std::optional<SomeInt> op{2};
assert((t <=> op) == std::strong_ordering::greater);
assert(testOrder(t, op, std::strong_ordering::greater));
}
}
constexpr void test_int() {
{
int t{3};
std::optional<int> op{3};
assert((t <=> op) == std::strong_ordering::equal);
assert(testOrder(t, op, std::strong_ordering::equal));
}
{
int t{2};
std::optional<int> op{3};
assert((t <=> op) == std::strong_ordering::less);
assert(testOrder(t, op, std::strong_ordering::less));
}
{
int t{3};
std::optional<int> op{2};
assert((t <=> op) == std::strong_ordering::greater);
assert(testOrder(t, op, std::strong_ordering::greater));
}
}
constexpr bool test() {
test_custom_integral();
test_int();
return true;
}
int main(int, char**) {
assert(test());
static_assert(test());
return 0;
}
|