//===----------------------------------------------------------------------===// // // 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.comp.with.t], comparison with T // template // requires (!is-derived-from-optional) && three_way_comparable_with // constexpr compare_three_way_result_t // operator<=>(const optional&, const U&); #include #include #include #include "test_comparisons.h" struct SomeInt { int value_; constexpr explicit SomeInt(int value = 0) : value_(value) {} auto operator<=>(const SomeInt&) const = default; }; template concept HasSpaceship = requires(T t, U u) { t <=> u; }; // SFINAE tests. static_assert(std::three_way_comparable_with, std::optional>); static_assert(HasSpaceship, std::optional>); static_assert(std::three_way_comparable_with, std::optional>); static_assert(HasSpaceship, std::optional>); static_assert(!HasSpaceship, std::optional>); // Runtime and static tests. constexpr void test_custom_integral() { { SomeInt t{3}; std::optional op{3}; assert((t <=> op) == std::strong_ordering::equal); assert(testOrder(t, op, std::strong_ordering::equal)); } { SomeInt t{2}; std::optional op{3}; assert((t <=> op) == std::strong_ordering::less); assert(testOrder(t, op, std::strong_ordering::less)); } { SomeInt t{3}; std::optional 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 op{3}; assert((t <=> op) == std::strong_ordering::equal); assert(testOrder(t, op, std::strong_ordering::equal)); } { int t{2}; std::optional op{3}; assert((t <=> op) == std::strong_ordering::less); assert(testOrder(t, op, std::strong_ordering::less)); } { int t{3}; std::optional 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; }