| 1 | // { dg-do compile { target c++26 } } |
| 2 | |
| 3 | |
| 4 | #include <functional> |
| 5 | |
| 6 | #ifndef __cpp_lib_reference_wrapper |
| 7 | # error "Feature-test macro for reference_wrapper missing" |
| 8 | #elif __cpp_lib_reference_wrapper != 202403 |
| 9 | # error "Feature-test macro for reference_wrapper has wrong value" |
| 10 | #endif |
| 11 | |
| 12 | // P2944R3 Comparisons for reference_wrapper |
| 13 | |
| 14 | auto check(int i, std::reference_wrapper<int> r) -> bool { |
| 15 | return i == r; |
| 16 | } |
| 17 | |
| 18 | template <class T> using Ref = std::reference_wrapper<T>; |
| 19 | |
| 20 | template <class T> |
| 21 | concept ref_equality_comparable |
| 22 | = requires (T a, T const ca, Ref<T> r, Ref<T const> cr) { |
| 23 | // the usual T is equality-comparable with itself |
| 24 | a == a; |
| 25 | a == ca; |
| 26 | ca == ca; |
| 27 | |
| 28 | // Ref<T> is equality-comparable with itself |
| 29 | r == r; |
| 30 | r == cr; |
| 31 | cr == cr; |
| 32 | |
| 33 | // T and Ref<T> are equality-comparable |
| 34 | a == r; |
| 35 | a == cr; |
| 36 | ca == r; |
| 37 | ca == cr; |
| 38 | }; |
| 39 | |
| 40 | static_assert( ref_equality_comparable<int> ); |
| 41 | |
| 42 | struct A { |
| 43 | auto operator==(A const&) const -> bool { return true; } |
| 44 | }; |
| 45 | |
| 46 | struct B { |
| 47 | friend auto operator==(B const&, B const&) -> bool { return true; } |
| 48 | }; |
| 49 | |
| 50 | template <class T> |
| 51 | struct C { |
| 52 | friend auto operator==(C const&, C const&) -> bool { return true; } |
| 53 | }; |
| 54 | |
| 55 | template <class T> |
| 56 | struct D { }; |
| 57 | template <class T> |
| 58 | auto operator==(D<T> const&, D<T> const&) -> bool { return true; } |
| 59 | |
| 60 | static_assert(ref_equality_comparable<int>); |
| 61 | static_assert(ref_equality_comparable<A>); |
| 62 | static_assert(ref_equality_comparable<B>); |
| 63 | static_assert(ref_equality_comparable<C<int>>); |
| 64 | static_assert(ref_equality_comparable<D<int>>); |
| 65 | #include <string_view> |
| 66 | static_assert(ref_equality_comparable<std::string_view>); |
| 67 | |
| 68 | template <typename T> |
| 69 | struct ValArray { |
| 70 | friend auto operator==(ValArray const&, ValArray const&) -> ValArray<bool> { |
| 71 | return {}; |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | void f(ValArray<int> v) { |
| 76 | // this is valid and has type ValArray<bool> |
| 77 | v == v; |
| 78 | |
| 79 | // this is also valid today and has the same type |
| 80 | std::ref(v) == std::ref(v); |
| 81 | } |
| 82 | |
| 83 | struct ComparesAsInt { |
| 84 | friend auto operator==(ComparesAsInt, ComparesAsInt) -> int; |
| 85 | }; |
| 86 | |
| 87 | auto f(std::reference_wrapper<ComparesAsInt> a, |
| 88 | std::reference_wrapper<ComparesAsInt> b) { |
| 89 | // today: compiles and returns int |
| 90 | // proposed: compiles and returns bool |
| 91 | return a == b; |
| 92 | } |
| 93 | |
| 94 | ComparesAsInt& c(); |
| 95 | static_assert( std::is_same_v<decltype(f(c(), c())), bool> ); |