| 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 | // <functional> |
| 10 | // |
| 11 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
| 12 | // |
| 13 | // common_reference specializations for reference_wrapper |
| 14 | |
| 15 | #include <concepts> |
| 16 | #include <functional> |
| 17 | #include <type_traits> |
| 18 | |
| 19 | template <class T> |
| 20 | concept HasType = requires { typename T::type; }; |
| 21 | |
| 22 | template <class Result, class T1, class T2> |
| 23 | concept check_XY = std::same_as<Result, std::common_reference_t<T1, T2>>; |
| 24 | |
| 25 | template <class Result, class T1, class T2> |
| 26 | concept check_YX = std::same_as<Result, std::common_reference_t<T2, T1>>; |
| 27 | |
| 28 | template <class Result, class T1, class T2> |
| 29 | concept check = check_XY<Result, T1, T2> && check_YX<Result, T1, T2>; |
| 30 | |
| 31 | template <class T1, class T2> |
| 32 | concept check_none_XY = !HasType<std::common_reference<T1, T2>>; |
| 33 | template <class T1, class T2> |
| 34 | concept check_none_YX = !HasType<std::common_reference<T2, T1>>; |
| 35 | |
| 36 | template <class T1, class T2> |
| 37 | concept check_none = check_none_XY<T1, T2> && check_none_YX<T1, T2>; |
| 38 | |
| 39 | // https://eel.is/c++draft/meta.trans#other-2.4 |
| 40 | template <class X, class Y> |
| 41 | using CondRes = decltype(false ? std::declval<X (&)()>()() : std::declval<Y (&)()>()()); |
| 42 | |
| 43 | template <class X, class Y> |
| 44 | struct Ternary {}; |
| 45 | |
| 46 | template <class X, class Y> |
| 47 | requires requires() { typename CondRes<X, Y>; } |
| 48 | struct Ternary<X, Y> { |
| 49 | using type = CondRes<X, Y>; |
| 50 | }; |
| 51 | template <class X, class Y> |
| 52 | using Ternary_t = typename Ternary<X, Y>::type; |
| 53 | |
| 54 | template <class T> |
| 55 | using Ref = std::reference_wrapper<T>; |
| 56 | |
| 57 | using std::common_reference_t; |
| 58 | using std::same_as; |
| 59 | |
| 60 | // clang-format off |
| 61 | static_assert(check<int & , Ref<int >, int & >); |
| 62 | static_assert(check<int const&, Ref<int >, int const& >); |
| 63 | static_assert(check<int const&, Ref<int const>, int & >); |
| 64 | static_assert(check<int const&, Ref<int const>, int const& >); |
| 65 | static_assert(check<int&, Ref<int> const&, int& >); |
| 66 | static_assert(check<const volatile int&, Ref<const volatile int>, const volatile int&>); |
| 67 | |
| 68 | // derived-base and implicit convertibles |
| 69 | struct B {}; |
| 70 | struct D : B {}; |
| 71 | struct C { |
| 72 | operator B&() const; |
| 73 | }; |
| 74 | |
| 75 | static_assert(check<B& , Ref<B>, D & >); |
| 76 | static_assert(check<B const&, Ref<B>, D const&>); |
| 77 | static_assert(check<B const&, Ref<B const>, D const&>); |
| 78 | |
| 79 | static_assert(check<B& , Ref<D>, B & >); |
| 80 | static_assert(check<B const&, Ref<D>, B const&>); |
| 81 | static_assert(check<B const&, Ref<D const>, B const&>); |
| 82 | |
| 83 | static_assert(std::same_as<B&, CondRes<Ref<D>, B&>>); |
| 84 | static_assert(std::same_as<B const&, CondRes<Ref<D>, B const &>>); |
| 85 | static_assert(std::same_as<B const&, CondRes<Ref<D const>, B const&>>); |
| 86 | |
| 87 | static_assert( check<B& , Ref<B> , C& >); |
| 88 | static_assert( check<B& , Ref<B> , C >); |
| 89 | static_assert( check<B const& , Ref<B const>, C >); |
| 90 | static_assert(!check<B& , Ref<C> , B& >); // Ref<C> cannot be converted to B& |
| 91 | static_assert( check<B& , Ref<B> , C const&>); // was const B& before P2655R3 |
| 92 | |
| 93 | |
| 94 | using Ri = Ref<int>; |
| 95 | using RRi = Ref<Ref<int>>; |
| 96 | using RRRi = Ref<Ref<Ref<int>>>; |
| 97 | static_assert(check<Ri&, Ri&, RRi>); |
| 98 | static_assert(check<RRi&, RRi&, RRRi>); |
| 99 | static_assert(check<Ri, Ri, RRi>); |
| 100 | static_assert(check<RRi, RRi, RRRi>); |
| 101 | |
| 102 | static_assert(check_none<int&, RRi>); |
| 103 | static_assert(check_none<int, RRi>); |
| 104 | static_assert(check_none<int&, RRRi>); |
| 105 | static_assert(check_none<int, RRRi>); |
| 106 | |
| 107 | static_assert(check_none<Ri&, RRRi>); |
| 108 | static_assert(check_none<Ri, RRRi>); |
| 109 | |
| 110 | |
| 111 | template <typename T> |
| 112 | struct Test { |
| 113 | // Check that reference_wrapper<T> behaves the same as T& in common_reference. |
| 114 | |
| 115 | using R1 = common_reference_t<T&, T&>; |
| 116 | using R2 = common_reference_t<T&, T const&>; |
| 117 | using R3 = common_reference_t<T&, T&&>; |
| 118 | using R4 = common_reference_t<T&, T const&&>; |
| 119 | using R5 = common_reference_t<T&, T>; |
| 120 | |
| 121 | static_assert(same_as<R1, common_reference_t<Ref<T>, T&>>); |
| 122 | static_assert(same_as<R2, common_reference_t<Ref<T>, T const&>>); |
| 123 | static_assert(same_as<R3, common_reference_t<Ref<T>, T&&>>); |
| 124 | static_assert(same_as<R4, common_reference_t<Ref<T>, T const&&>>); |
| 125 | static_assert(same_as<R5, common_reference_t<Ref<T>, T>>); |
| 126 | |
| 127 | // commute: |
| 128 | static_assert(same_as<R1, common_reference_t<T&, Ref<T>>>); |
| 129 | static_assert(same_as<R2, common_reference_t<T const&, Ref<T>>>); |
| 130 | static_assert(same_as<R3, common_reference_t<T&&, Ref<T>>>); |
| 131 | static_assert(same_as<R4, common_reference_t<T const&&, Ref<T>>>); |
| 132 | static_assert(same_as<R5, common_reference_t<T, Ref<T>>>); |
| 133 | |
| 134 | // reference qualification of reference_wrapper is irrelevant |
| 135 | static_assert(same_as<R1, common_reference_t<Ref<T>&, T&>>); |
| 136 | static_assert(same_as<R1, common_reference_t<Ref<T> , T&>>); |
| 137 | static_assert(same_as<R1, common_reference_t<Ref<T> const&, T&>>); |
| 138 | static_assert(same_as<R1, common_reference_t<Ref<T>&&, T&>>); |
| 139 | static_assert(same_as<R1, common_reference_t<Ref<T> const&&, T&>>); |
| 140 | }; |
| 141 | |
| 142 | // clang-format on |
| 143 | // Instantiate above checks: |
| 144 | template struct Test<int>; |
| 145 | template struct Test<std::reference_wrapper<int>>; |
| 146 | |
| 147 | // reference_wrapper as both args is unaffected. |
| 148 | // subject to simple first rule of |
| 149 | static_assert(check<Ref<int>&, Ref<int>&, Ref<int>&>); |
| 150 | |
| 151 | // double wrap is unaffected. |
| 152 | static_assert(check<Ref<int>&, Ref<Ref<int>>, Ref<int>&>); |
| 153 | |