| 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, c++20, c++23 |
| 10 | |
| 11 | // <functional> |
| 12 | |
| 13 | // class reference_wrapper |
| 14 | |
| 15 | // [refwrap.comparisons], comparisons |
| 16 | |
| 17 | // friend constexpr auto operator<=>(reference_wrapper, reference_wrapper<const T>); // Since C++26 |
| 18 | |
| 19 | #include <cassert> |
| 20 | #include <concepts> |
| 21 | #include <functional> |
| 22 | |
| 23 | #include "test_comparisons.h" |
| 24 | #include "test_macros.h" |
| 25 | |
| 26 | #include "helper_concepts.h" |
| 27 | #include "helper_types.h" |
| 28 | |
| 29 | // Test SFINAE. |
| 30 | |
| 31 | static_assert(std::three_way_comparable_with<std::reference_wrapper<StrongOrder>, const StrongOrder>); |
| 32 | static_assert(std::three_way_comparable_with<std::reference_wrapper<WeakOrder>, const WeakOrder>); |
| 33 | static_assert(std::three_way_comparable_with<std::reference_wrapper<PartialOrder>, const PartialOrder>); |
| 34 | |
| 35 | static_assert(!std::three_way_comparable_with<std::reference_wrapper<StrongOrder>, const NonComparable>); |
| 36 | static_assert(!std::three_way_comparable_with<std::reference_wrapper<WeakOrder>, const NonComparable>); |
| 37 | static_assert(!std::three_way_comparable_with<std::reference_wrapper<PartialOrder>, const NonComparable>); |
| 38 | |
| 39 | // Test comparisons. |
| 40 | |
| 41 | template <typename T, typename Order> |
| 42 | constexpr void test() { |
| 43 | T t{47}; |
| 44 | |
| 45 | T bigger{94}; |
| 46 | T smaller{82}; |
| 47 | |
| 48 | T unordered{std::numeric_limits<int>::min()}; |
| 49 | |
| 50 | // Identical contents |
| 51 | { |
| 52 | std::reference_wrapper<T> rw1{t}; |
| 53 | std::reference_wrapper<const T> rw2{t}; |
| 54 | assert(testOrder(rw1, rw2, Order::equivalent)); |
| 55 | } |
| 56 | // Less |
| 57 | { |
| 58 | std::reference_wrapper<T> rw1{smaller}; |
| 59 | std::reference_wrapper<const T> rw2{bigger}; |
| 60 | assert(testOrder(rw1, rw2, Order::less)); |
| 61 | } |
| 62 | // Greater |
| 63 | { |
| 64 | std::reference_wrapper<T> rw1{bigger}; |
| 65 | std::reference_wrapper<const T> rw2{smaller}; |
| 66 | assert(testOrder(rw1, rw2, Order::greater)); |
| 67 | } |
| 68 | // Unordered |
| 69 | if constexpr (std::same_as<T, PartialOrder>) { |
| 70 | std::reference_wrapper<T> rw1{bigger}; |
| 71 | std::reference_wrapper<const T> rw2{unordered}; |
| 72 | assert(testOrder(rw1, rw2, Order::unordered)); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | constexpr bool test() { |
| 77 | test<int, std::strong_ordering>(); |
| 78 | test<StrongOrder, std::strong_ordering>(); |
| 79 | test<int, std::weak_ordering>(); |
| 80 | test<WeakOrder, std::weak_ordering>(); |
| 81 | test<int, std::partial_ordering>(); |
| 82 | test<PartialOrder, std::partial_ordering>(); |
| 83 | |
| 84 | // `LessAndEqComp` does not have `operator<=>`. Ordering is synthesized based on `operator<` |
| 85 | test<LessAndEqComp, std::weak_ordering>(); |
| 86 | |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | int main(int, char**) { |
| 91 | test(); |
| 92 | static_assert(test()); |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |