| 1 |
// P0847R7 |
| 2 |
// { dg-do compile { target c++23 } } |
| 3 |
|
| 4 |
// well-formed and ill-formed uses of member only operators in a requires expression |
| 5 |
|
| 6 |
// suppress the warning for Value's arrow operator |
| 7 |
// { dg-options "-Wno-return-local-addr" } |
| 8 |
|
| 9 |
// It's very hard to test for incorrect successes without requires, and by extension a non dependent variable |
| 10 |
// so for the time being, there are no non dependent tests invalid calls. |
| 11 |
|
| 12 |
struct Value { |
| 13 |
int _v; |
| 14 |
Value operator=(this Value self, int) { return self; } |
| 15 |
Value operator()(this Value self) { return self; } |
| 16 |
Value operator[](this Value self) { return self; } |
| 17 |
Value* operator->(this Value self) { return &self; } |
| 18 |
}; |
| 19 |
|
| 20 |
struct LRef { |
| 21 |
int _v; |
| 22 |
LRef& operator=(this LRef& self, int) { return self; } |
| 23 |
LRef& operator()(this LRef& self) { return self; } |
| 24 |
LRef& operator[](this LRef& self) { return self; } |
| 25 |
LRef* operator->(this LRef& self) { return &self; } |
| 26 |
}; |
| 27 |
|
| 28 |
struct RRef { |
| 29 |
int _v; |
| 30 |
RRef&& operator=(this RRef&& self, int) { return static_cast<RRef&&>(self); } |
| 31 |
RRef&& operator()(this RRef&& self) { return static_cast<RRef&&>(self); } |
| 32 |
RRef&& operator[](this RRef&& self) { return static_cast<RRef&&>(self); } |
| 33 |
RRef* operator->(this RRef&& self) { return &self; } |
| 34 |
}; |
| 35 |
|
| 36 |
struct ConstLRef { |
| 37 |
int _v; |
| 38 |
ConstLRef const& operator=(this ConstLRef const& self, int) { return self; } |
| 39 |
ConstLRef const& operator()(this ConstLRef const& self) { return self; } |
| 40 |
ConstLRef const& operator[](this ConstLRef const& self) { return self; } |
| 41 |
ConstLRef const* operator->(this ConstLRef const& self) { return &self; } |
| 42 |
}; |
| 43 |
|
| 44 |
struct ConstRRef { |
| 45 |
int _v; |
| 46 |
ConstRRef const&& operator=(this ConstRRef const&& self, int) { return static_cast<ConstRRef const&&>(self); } |
| 47 |
ConstRRef const&& operator()(this ConstRRef const&& self) { return static_cast<ConstRRef const&&>(self); } |
| 48 |
ConstRRef const&& operator[](this ConstRRef const&& self) { return static_cast<ConstRRef const&&>(self); } |
| 49 |
ConstRRef const* operator->(this ConstRRef const&& self) { return &self; } |
| 50 |
}; |
| 51 |
|
| 52 |
// needed to implement deduced operator-> |
| 53 |
template<typename T> struct remove_ref { using type = T; }; |
| 54 |
template<typename T> struct remove_ref<T&> { using type = T; }; |
| 55 |
template<typename T> struct remove_ref<T&&> { using type = T; }; |
| 56 |
template<typename T> using remove_ref_t = typename remove_ref<T>::type; |
| 57 |
|
| 58 |
struct Deduced { |
| 59 |
int _v; |
| 60 |
template<typename Self> |
| 61 |
Self&& operator=(this Self&& self, int) { return static_cast<Self&&>(self); } |
| 62 |
template<typename Self> |
| 63 |
Self&& operator()(this Self&& self) { return static_cast<Self&&>(self); } |
| 64 |
template<typename Self> |
| 65 |
Self&& operator[](this Self&& self) { return static_cast<Self&&>(self); } |
| 66 |
template<typename Self> |
| 67 |
remove_ref_t<Self>* operator->(this Self&& self) { return &self; } |
| 68 |
}; |
| 69 |
|
| 70 |
#define TEST_INVALID(OPERAND) \ |
| 71 |
static_assert(!requires{ (OPERAND) = 0; }, "Unexpected success calling operator = with " #OPERAND); \ |
| 72 |
static_assert(!requires{ (OPERAND)(); }, "Unexpected success calling operator () with " #OPERAND); \ |
| 73 |
static_assert(!requires{ (OPERAND)[]; }, "Unexpected success calling operator [] with " #OPERAND); \ |
| 74 |
static_assert(!requires{ (OPERAND)->_v; }, "Unexpected success calling operator -> with " #OPERAND); |
| 75 |
|
| 76 |
#define TEST_VALID(OPERAND) \ |
| 77 |
static_assert(requires{ (OPERAND) = 0; }, "Unexpected failure calling operator = with " #OPERAND); \ |
| 78 |
static_assert(requires{ (OPERAND)(); }, "Unexpected failure calling operator () with " #OPERAND); \ |
| 79 |
static_assert(requires{ (OPERAND)[]; }, "Unexpected failure calling operator [] with " #OPERAND); \ |
| 80 |
static_assert(requires{ (OPERAND)->_v; }, "Unexpected failure calling operator -> with " #OPERAND); |
| 81 |
|
| 82 |
template<typename T, typename U> |
| 83 |
concept same_as = __is_same(T, U); |
| 84 |
|
| 85 |
#define TEST_VALID_WITH_RETURN_TYPES(OPERAND, CORRECT_TYPE) \ |
| 86 |
static_assert(requires{ {(OPERAND) = 0} -> same_as<CORRECT_TYPE>; },"Unexpected failure with return type check calling operator = with " #OPERAND " -> expected return type: " #CORRECT_TYPE); \ |
| 87 |
static_assert(requires{ {(OPERAND)()} -> same_as<CORRECT_TYPE>; }, "Unexpected failure with return type check calling operator () with " #OPERAND " -> expected return type: " #CORRECT_TYPE); \ |
| 88 |
static_assert(requires{ {(OPERAND)[]} -> same_as<CORRECT_TYPE>; }, "Unexpected failure with return type check calling operator [] with " #OPERAND " -> expected return type: " #CORRECT_TYPE); |
| 89 |
|
| 90 |
|
| 91 |
template<typename DepValue = Value> |
| 92 |
void test_value() |
| 93 |
{ |
| 94 |
DepValue value{}; |
| 95 |
TEST_VALID(value) |
| 96 |
TEST_VALID(static_cast<DepValue&&>(value)) |
| 97 |
TEST_VALID(static_cast<DepValue const&>(value)) |
| 98 |
TEST_VALID(static_cast<DepValue const&&>(value)) |
| 99 |
} |
| 100 |
|
| 101 |
template<typename DepLRef = LRef> |
| 102 |
void test_l_ref() |
| 103 |
{ |
| 104 |
DepLRef l_ref{}; |
| 105 |
TEST_VALID(l_ref) |
| 106 |
TEST_INVALID(static_cast<DepLRef&&>(l_ref)) |
| 107 |
TEST_INVALID(static_cast<DepLRef const&>(l_ref)) |
| 108 |
TEST_INVALID(static_cast<DepLRef const&&>(l_ref)) |
| 109 |
} |
| 110 |
|
| 111 |
template<typename DepRRef = RRef> |
| 112 |
void test_r_ref() |
| 113 |
{ |
| 114 |
DepRRef r_ref{}; |
| 115 |
TEST_INVALID(r_ref) |
| 116 |
TEST_VALID(static_cast<DepRRef&&>(r_ref)) |
| 117 |
TEST_INVALID(static_cast<DepRRef const&>(r_ref)) |
| 118 |
TEST_INVALID(static_cast<DepRRef const&&>(r_ref)) |
| 119 |
} |
| 120 |
|
| 121 |
template<typename DepConstLRef = ConstLRef> |
| 122 |
void test_const_l_ref() |
| 123 |
{ |
| 124 |
DepConstLRef const_l_ref{}; |
| 125 |
TEST_VALID(const_l_ref) |
| 126 |
TEST_VALID(static_cast<DepConstLRef&&>(const_l_ref)) |
| 127 |
TEST_VALID(static_cast<DepConstLRef const&>(const_l_ref)) |
| 128 |
TEST_VALID(static_cast<DepConstLRef const&&>(const_l_ref)) |
| 129 |
} |
| 130 |
|
| 131 |
template<typename DepConstRRef = ConstRRef> |
| 132 |
void test_const_r_ref() |
| 133 |
{ |
| 134 |
DepConstRRef const_r_ref{}; |
| 135 |
TEST_INVALID(const_r_ref) |
| 136 |
TEST_VALID(static_cast<DepConstRRef&&>(const_r_ref)) |
| 137 |
TEST_INVALID(static_cast<DepConstRRef const&>(const_r_ref)) |
| 138 |
TEST_VALID(static_cast<DepConstRRef const&&>(const_r_ref)) |
| 139 |
} |
| 140 |
|
| 141 |
template<typename DepDeduced = Deduced> |
| 142 |
void test_deduced() |
| 143 |
{ |
| 144 |
DepDeduced deduced{}; |
| 145 |
|
| 146 |
TEST_VALID(deduced) |
| 147 |
TEST_VALID(static_cast<DepDeduced&&>(deduced)) |
| 148 |
TEST_VALID(static_cast<DepDeduced const&>(deduced)) |
| 149 |
TEST_VALID(static_cast<DepDeduced const&&>(deduced)) |
| 150 |
|
| 151 |
TEST_VALID_WITH_RETURN_TYPES(deduced, DepDeduced&) |
| 152 |
TEST_VALID_WITH_RETURN_TYPES(static_cast<DepDeduced&&>(deduced), DepDeduced&&) |
| 153 |
TEST_VALID_WITH_RETURN_TYPES(static_cast<DepDeduced const&>(deduced), DepDeduced const&) |
| 154 |
TEST_VALID_WITH_RETURN_TYPES(static_cast<DepDeduced const&&>(deduced), DepDeduced const&&) |
| 155 |
// arrow operator needs to be seperate to check the type of _v |
| 156 |
static_assert(requires{ {(deduced->_v)} -> same_as<int&>; }, "Unexpected failure with return type check calling operator -> with deduced->_v"); |
| 157 |
static_assert(requires{ {(static_cast<DepDeduced&&>(deduced)->_v)} -> same_as<int&>; }, "Unexpected failure with return type check calling operator -> with static_cast<DepDeduced&&>(deduced)->_v"); |
| 158 |
static_assert(requires{ {(static_cast<DepDeduced const&>(deduced)->_v)} -> same_as<int const&>; }, "Unexpected failure with return type check calling operator -> with static_cast<DepDeduced const&>(deduced)->_v"); |
| 159 |
static_assert(requires{ {(static_cast<DepDeduced const&&>(deduced)->_v)} -> same_as<int const&>; }, "Unexpected failure with return type check calling operator -> with static_cast<DepDeduced const&&>(deduced)->_v"); |
| 160 |
} |
| 161 |
|
| 162 |
void test() |
| 163 |
{ |
| 164 |
test_value(); |
| 165 |
test_l_ref(); |
| 166 |
test_r_ref(); |
| 167 |
test_const_l_ref(); |
| 168 |
test_const_r_ref(); |
| 169 |
test_deduced(); |
| 170 |
} |
| 171 |
|