| 1 |
// P0847R7 |
| 2 |
// { dg-do run { target c++23 } } |
| 3 |
|
| 4 |
// overload resolution of static/xobj and iobj/xobj member functions |
| 5 |
// with constraints |
| 6 |
|
| 7 |
template<typename T> |
| 8 |
concept Constrain = true; |
| 9 |
|
| 10 |
inline constexpr int iobj_fn = 5; |
| 11 |
inline constexpr int xobj_fn = 10; |
| 12 |
inline constexpr int static_fn = 20; |
| 13 |
|
| 14 |
// first 2 letters are the order of the definitions |
| 15 |
// the constraint applies to the first definition, |
| 16 |
// for *_r cases the constraint applies to the second |
| 17 |
|
| 18 |
struct S { |
| 19 |
// xobj/static |
| 20 |
int f_xs_v(this S, Constrain auto) { return xobj_fn; }; |
| 21 |
static int f_xs_v(auto) { return static_fn; }; |
| 22 |
|
| 23 |
int f_xs_ref(this S&, Constrain auto) { return xobj_fn; }; |
| 24 |
static int f_xs_ref(auto) { return static_fn; }; |
| 25 |
|
| 26 |
int f_xs_cref(this S const&, Constrain auto) { return xobj_fn; }; |
| 27 |
static int f_xs_cref(auto) { return static_fn; }; |
| 28 |
|
| 29 |
int f_xs_rref(this S&&, Constrain auto) { return xobj_fn; }; |
| 30 |
static int f_xs_rref(auto) { return static_fn; }; |
| 31 |
|
| 32 |
int f_xs_crref(this S const&&, Constrain auto) { return xobj_fn; }; |
| 33 |
static int f_xs_crref(auto) { return static_fn; }; |
| 34 |
|
| 35 |
// _r |
| 36 |
int f_xs_v_r(this S, auto) { return xobj_fn; }; |
| 37 |
static int f_xs_v_r(Constrain auto) { return static_fn; }; |
| 38 |
|
| 39 |
int f_xs_ref_r(this S&, auto) { return xobj_fn; }; |
| 40 |
static int f_xs_ref_r(Constrain auto) { return static_fn; }; |
| 41 |
|
| 42 |
int f_xs_cref_r(this S const&, auto) { return xobj_fn; }; |
| 43 |
static int f_xs_cref_r(Constrain auto) { return static_fn; }; |
| 44 |
|
| 45 |
int f_xs_rref_r(this S&&, auto) { return xobj_fn; }; |
| 46 |
static int f_xs_rref_r(Constrain auto) { return static_fn; }; |
| 47 |
|
| 48 |
int f_xs_crref_r(this S const&&, auto) { return xobj_fn; }; |
| 49 |
static int f_xs_crref_r(Constrain auto) { return static_fn; }; |
| 50 |
|
| 51 |
// static/xobj |
| 52 |
static int f_sx_v(Constrain auto) { return static_fn; }; |
| 53 |
int f_sx_v(this S, auto) { return xobj_fn; }; |
| 54 |
|
| 55 |
static int f_sx_ref(Constrain auto) { return static_fn; }; |
| 56 |
int f_sx_ref(this S&, auto) { return xobj_fn; }; |
| 57 |
|
| 58 |
static int f_sx_cref(Constrain auto) { return static_fn; }; |
| 59 |
int f_sx_cref(this S const&, auto) { return xobj_fn; }; |
| 60 |
|
| 61 |
static int f_sx_rref(Constrain auto) { return static_fn; }; |
| 62 |
int f_sx_rref(this S&&, auto) { return xobj_fn; }; |
| 63 |
|
| 64 |
static int f_sx_crref(Constrain auto) { return static_fn; }; |
| 65 |
int f_sx_crref(this S const&&, auto) { return xobj_fn; }; |
| 66 |
|
| 67 |
// _r |
| 68 |
static int f_sx_v_r(auto) { return static_fn; }; |
| 69 |
int f_sx_v_r(this S, Constrain auto) { return xobj_fn; }; |
| 70 |
|
| 71 |
static int f_sx_ref_r(auto) { return static_fn; }; |
| 72 |
int f_sx_ref_r(this S&, Constrain auto) { return xobj_fn; }; |
| 73 |
|
| 74 |
static int f_sx_cref_r(auto) { return static_fn; }; |
| 75 |
int f_sx_cref_r(this S const&, Constrain auto) { return xobj_fn; }; |
| 76 |
|
| 77 |
static int f_sx_rref_r(auto) { return static_fn; }; |
| 78 |
int f_sx_rref_r(this S&&, Constrain auto) { return xobj_fn; }; |
| 79 |
|
| 80 |
static int f_sx_crref_r(auto) { return static_fn; }; |
| 81 |
int f_sx_crref_r(this S const&&, Constrain auto) { return xobj_fn; }; |
| 82 |
|
| 83 |
// xobj/iobj with matching object parameters |
| 84 |
|
| 85 |
// We are only testing constraints here, so we need parameter lists |
| 86 |
// to match, which means we need corresponding object parameters. |
| 87 |
// Remember, the rules for object parameter correspondence are weird. |
| 88 |
// ([basic.scope.scope-3.1]) |
| 89 |
|
| 90 |
// *_refqual the iobj member function has a reference qualifier |
| 91 |
// *_r the constraint applies to the second definition |
| 92 |
|
| 93 |
// ix |
| 94 |
int f_ix_m0(Constrain auto) { return iobj_fn; }; |
| 95 |
int f_ix_m0(this S&, auto) { return xobj_fn; }; |
| 96 |
// See note |
| 97 |
int f_ix_m1(Constrain auto) { return iobj_fn; }; |
| 98 |
int f_ix_m1(this S&&, auto) { return xobj_fn; }; |
| 99 |
|
| 100 |
int f_ix_c0(Constrain auto) const { return iobj_fn; }; |
| 101 |
int f_ix_c0(this S const&, auto) { return xobj_fn; }; |
| 102 |
// See note |
| 103 |
int f_ix_c1(Constrain auto) const { return iobj_fn; }; |
| 104 |
int f_ix_c1(this S const&&, auto) { return xobj_fn; }; |
| 105 |
|
| 106 |
// xi |
| 107 |
int f_xi_m0(this S&, Constrain auto) { return xobj_fn; }; |
| 108 |
int f_xi_m0(auto) { return iobj_fn; }; |
| 109 |
// See note |
| 110 |
int f_xi_m1(this S&&, Constrain auto) { return xobj_fn; }; |
| 111 |
int f_xi_m1(auto) { return iobj_fn; }; |
| 112 |
|
| 113 |
int f_xi_c0(this S const&, Constrain auto) { return xobj_fn; }; |
| 114 |
int f_xi_c0(auto) const { return iobj_fn; }; |
| 115 |
// See note |
| 116 |
int f_xi_c1(this S const&&, Constrain auto) { return xobj_fn; }; |
| 117 |
int f_xi_c1(auto) const { return iobj_fn; }; |
| 118 |
|
| 119 |
// with ref qualifier |
| 120 |
|
| 121 |
// ix |
| 122 |
int f_ix_m0_refqual(Constrain auto) & { return iobj_fn; }; |
| 123 |
int f_ix_m0_refqual(this S&, auto) { return xobj_fn; }; |
| 124 |
|
| 125 |
int f_ix_m1_refqual(Constrain auto) && { return iobj_fn; }; |
| 126 |
int f_ix_m1_refqual(this S&&, auto) { return xobj_fn; }; |
| 127 |
|
| 128 |
int f_ix_c0_refqual(Constrain auto) const& { return iobj_fn; }; |
| 129 |
int f_ix_c0_refqual(this S const&, auto) { return xobj_fn; }; |
| 130 |
|
| 131 |
int f_ix_c1_refqual(Constrain auto) const&& { return iobj_fn; }; |
| 132 |
int f_ix_c1_refqual(this S const&&, auto) { return xobj_fn; }; |
| 133 |
|
| 134 |
// xi |
| 135 |
int f_xi_m0_refqual(this S&, Constrain auto) { return xobj_fn; }; |
| 136 |
int f_xi_m0_refqual(auto) & { return iobj_fn; }; |
| 137 |
|
| 138 |
int f_xi_m1_refqual(this S&&, Constrain auto) { return xobj_fn; }; |
| 139 |
int f_xi_m1_refqual(auto) && { return iobj_fn; }; |
| 140 |
|
| 141 |
int f_xi_c0_refqual(this S const&, Constrain auto) { return xobj_fn; }; |
| 142 |
int f_xi_c0_refqual(auto) const& { return iobj_fn; }; |
| 143 |
|
| 144 |
int f_xi_c1_refqual(this S const&&, Constrain auto) { return xobj_fn; }; |
| 145 |
int f_xi_c1_refqual(auto) const&& { return iobj_fn; }; |
| 146 |
|
| 147 |
// _r without ref qualifier |
| 148 |
|
| 149 |
// ix |
| 150 |
int f_ix_m0_r(auto) { return iobj_fn; }; |
| 151 |
int f_ix_m0_r(this S&, Constrain auto) { return xobj_fn; }; |
| 152 |
// See note |
| 153 |
int f_ix_m1_r(auto) { return iobj_fn; }; |
| 154 |
int f_ix_m1_r(this S&&, Constrain auto) { return xobj_fn; }; |
| 155 |
|
| 156 |
int f_ix_c0_r(auto) const { return iobj_fn; }; |
| 157 |
int f_ix_c0_r(this S const&, Constrain auto) { return xobj_fn; }; |
| 158 |
// See note |
| 159 |
int f_ix_c1_r(auto) const { return iobj_fn; }; |
| 160 |
int f_ix_c1_r(this S const&&, Constrain auto) { return xobj_fn; }; |
| 161 |
|
| 162 |
// xi |
| 163 |
int f_xi_m0_r(this S&, auto) { return xobj_fn; }; |
| 164 |
int f_xi_m0_r(Constrain auto) { return iobj_fn; }; |
| 165 |
// See note |
| 166 |
int f_xi_m1_r(this S&&, auto) { return xobj_fn; }; |
| 167 |
int f_xi_m1_r(Constrain auto) { return iobj_fn; }; |
| 168 |
|
| 169 |
int f_xi_c0_r(this S const&, auto) { return xobj_fn; }; |
| 170 |
int f_xi_c0_r(Constrain auto) const { return iobj_fn; }; |
| 171 |
// See note |
| 172 |
int f_xi_c1_r(this S const&&, auto) { return xobj_fn; }; |
| 173 |
int f_xi_c1_r(Constrain auto) const { return iobj_fn; }; |
| 174 |
|
| 175 |
// _r with ref qualifier |
| 176 |
// ix |
| 177 |
int f_ix_m0_refqual_r(auto) & { return iobj_fn; }; |
| 178 |
int f_ix_m0_refqual_r(this S&, Constrain auto) { return xobj_fn; }; |
| 179 |
|
| 180 |
int f_ix_m1_refqual_r(auto) && { return iobj_fn; }; |
| 181 |
int f_ix_m1_refqual_r(this S&&, Constrain auto) { return xobj_fn; }; |
| 182 |
|
| 183 |
int f_ix_c0_refqual_r(auto) const& { return iobj_fn; }; |
| 184 |
int f_ix_c0_refqual_r(this S const&, Constrain auto) { return xobj_fn; }; |
| 185 |
|
| 186 |
int f_ix_c1_refqual_r(auto) const&& { return iobj_fn; }; |
| 187 |
int f_ix_c1_refqual_r(this S const&&, Constrain auto) { return xobj_fn; }; |
| 188 |
|
| 189 |
// xi |
| 190 |
int f_xi_m0_refqual_r(this S&, auto) { return xobj_fn; }; |
| 191 |
int f_xi_m0_refqual_r(Constrain auto) & { return iobj_fn; }; |
| 192 |
|
| 193 |
int f_xi_m1_refqual_r(this S&&, auto) { return xobj_fn; }; |
| 194 |
int f_xi_m1_refqual_r(Constrain auto) && { return iobj_fn; }; |
| 195 |
|
| 196 |
int f_xi_c0_refqual_r(this S const&, auto) { return xobj_fn; }; |
| 197 |
int f_xi_c0_refqual_r(Constrain auto) const& { return iobj_fn; }; |
| 198 |
|
| 199 |
int f_xi_c1_refqual_r(this S const&&, auto) { return xobj_fn; }; |
| 200 |
int f_xi_c1_refqual_r(Constrain auto) const&& { return iobj_fn; }; |
| 201 |
}; |
| 202 |
|
| 203 |
|
| 204 |
int main() |
| 205 |
{ |
| 206 |
// The commented out cases are ambiguous, which is most likely the correct |
| 207 |
// behavior. It is something that I want to propose to change, and I want |
| 208 |
// to leave them in as they are a little weird. |
| 209 |
// |
| 210 |
// Furthermore, as the comment at the top of this file indicates, I am not |
| 211 |
// clear on the correct behavior of the static/xobj cases in general. |
| 212 |
|
| 213 |
S s{}; |
| 214 |
if (s.f_xs_v (0) != xobj_fn) |
| 215 |
__builtin_abort (); |
| 216 |
if (s.f_xs_ref (0) != xobj_fn) |
| 217 |
__builtin_abort (); |
| 218 |
if (s.f_xs_cref (0) != xobj_fn) |
| 219 |
__builtin_abort (); |
| 220 |
if (static_cast<S&&>(s).f_xs_rref (0) != xobj_fn) |
| 221 |
__builtin_abort (); |
| 222 |
if (static_cast<S&&>(s).f_xs_crref (0) != xobj_fn) |
| 223 |
__builtin_abort (); |
| 224 |
// if (s.f_xs_dv (0) != xobj_fn) |
| 225 |
// __builtin_abort (); |
| 226 |
// if (s.f_xs_dcref (0) != xobj_fn) |
| 227 |
// __builtin_abort (); |
| 228 |
// if (s.f_xs_dfwdref (0) != xobj_fn) |
| 229 |
// __builtin_abort (); |
| 230 |
|
| 231 |
if (s.f_xs_v_r (0) != static_fn) |
| 232 |
__builtin_abort (); |
| 233 |
if (s.f_xs_ref_r (0) != static_fn) |
| 234 |
__builtin_abort (); |
| 235 |
if (s.f_xs_cref_r (0) != static_fn) |
| 236 |
__builtin_abort (); |
| 237 |
if (static_cast<S&&>(s).f_xs_rref_r (0) != static_fn) |
| 238 |
__builtin_abort (); |
| 239 |
if (static_cast<S&&>(s).f_xs_crref_r (0) != static_fn) |
| 240 |
__builtin_abort (); |
| 241 |
// if (s.f_xs_dv_r (0) != static_fn) |
| 242 |
// __builtin_abort (); |
| 243 |
// if (s.f_xs_dcref_r (0) != static_fn) |
| 244 |
// __builtin_abort (); |
| 245 |
// if (s.f_xs_dfwdref_r (0) != static_fn) |
| 246 |
// __builtin_abort (); |
| 247 |
|
| 248 |
if (s.f_sx_v (0) != static_fn) |
| 249 |
__builtin_abort (); |
| 250 |
if (s.f_sx_ref (0) != static_fn) |
| 251 |
__builtin_abort (); |
| 252 |
if (s.f_sx_cref (0) != static_fn) |
| 253 |
__builtin_abort (); |
| 254 |
if (static_cast<S&&>(s).f_sx_rref (0) != static_fn) |
| 255 |
__builtin_abort (); |
| 256 |
if (static_cast<S&&>(s).f_sx_crref (0) != static_fn) |
| 257 |
__builtin_abort (); |
| 258 |
// if (s.f_sx_dv (0) != static_fn) |
| 259 |
// __builtin_abort (); |
| 260 |
// if (s.f_sx_dcref (0) != static_fn) |
| 261 |
// __builtin_abort (); |
| 262 |
// if (s.f_sx_dfwdref (0) != static_fn) |
| 263 |
// __builtin_abort (); |
| 264 |
|
| 265 |
if (s.f_sx_v_r (0) != xobj_fn) |
| 266 |
__builtin_abort (); |
| 267 |
if (s.f_sx_ref_r (0) != xobj_fn) |
| 268 |
__builtin_abort (); |
| 269 |
if (s.f_sx_cref_r (0) != xobj_fn) |
| 270 |
__builtin_abort (); |
| 271 |
if (static_cast<S&&>(s).f_sx_rref_r (0) != xobj_fn) |
| 272 |
__builtin_abort (); |
| 273 |
if (static_cast<S&&>(s).f_sx_crref_r (0) != xobj_fn) |
| 274 |
__builtin_abort (); |
| 275 |
// if (s.f_sx_dv_r (0) != xobj_fn) |
| 276 |
// __builtin_abort (); |
| 277 |
// if (s.f_sx_dcref_r (0) != xobj_fn) |
| 278 |
// __builtin_abort (); |
| 279 |
// if (s.f_sx_dfwdref_r (0) != xobj_fn) |
| 280 |
// __builtin_abort (); |
| 281 |
|
| 282 |
// iobj/xobj |
| 283 |
|
| 284 |
// The commented out cases are tested below as their correct behavior is |
| 285 |
// unintuitive, see the note below for details. |
| 286 |
|
| 287 |
if (s.f_ix_m0 (0) != iobj_fn) |
| 288 |
__builtin_abort (); |
| 289 |
// s.f_ix_m1 |
| 290 |
if (s.f_ix_c0 (0) != iobj_fn) |
| 291 |
__builtin_abort (); |
| 292 |
// s.f_ix_c1 |
| 293 |
if (s.f_xi_m0 (0) != xobj_fn) |
| 294 |
__builtin_abort (); |
| 295 |
// s.f_xi_m1 |
| 296 |
if (s.f_xi_c0 (0) != xobj_fn) |
| 297 |
__builtin_abort (); |
| 298 |
// s.f_xi_c1 |
| 299 |
if (s.f_ix_m0_refqual (0) != iobj_fn) |
| 300 |
__builtin_abort (); |
| 301 |
if (static_cast<S&&>(s).f_ix_m1_refqual (0) != iobj_fn) |
| 302 |
__builtin_abort (); |
| 303 |
if (s.f_ix_c0_refqual (0) != iobj_fn) |
| 304 |
__builtin_abort (); |
| 305 |
if (static_cast<S&&>(s).f_ix_c1_refqual (0) != iobj_fn) |
| 306 |
__builtin_abort (); |
| 307 |
if (s.f_xi_m0_refqual (0) != xobj_fn) |
| 308 |
__builtin_abort (); |
| 309 |
if (static_cast<S&&>(s).f_xi_m1_refqual (0) != xobj_fn) |
| 310 |
__builtin_abort (); |
| 311 |
if (s.f_xi_c0_refqual (0) != xobj_fn) |
| 312 |
__builtin_abort (); |
| 313 |
if (static_cast<S&&>(s).f_xi_c1_refqual (0) != xobj_fn) |
| 314 |
__builtin_abort (); |
| 315 |
if (s.f_ix_m0_r (0) != xobj_fn) |
| 316 |
__builtin_abort (); |
| 317 |
// s.f_ix_m1_r |
| 318 |
if (s.f_ix_c0_r (0) != xobj_fn) |
| 319 |
__builtin_abort (); |
| 320 |
// s.f_ix_c1_r |
| 321 |
if (s.f_xi_m0_r (0) != iobj_fn) |
| 322 |
__builtin_abort (); |
| 323 |
// s.f_xi_m1_r |
| 324 |
if (s.f_xi_c0_r (0) != iobj_fn) |
| 325 |
__builtin_abort (); |
| 326 |
// s.f_xi_c1_r |
| 327 |
if (s.f_ix_m0_refqual_r (0) != xobj_fn) |
| 328 |
__builtin_abort (); |
| 329 |
if (static_cast<S&&>(s).f_ix_m1_refqual_r (0) != xobj_fn) |
| 330 |
__builtin_abort (); |
| 331 |
if (s.f_ix_c0_refqual_r (0) != xobj_fn) |
| 332 |
__builtin_abort (); |
| 333 |
if (static_cast<S&&>(s).f_ix_c1_refqual_r (0) != xobj_fn) |
| 334 |
__builtin_abort (); |
| 335 |
if (s.f_xi_m0_refqual_r (0) != iobj_fn) |
| 336 |
__builtin_abort (); |
| 337 |
if (static_cast<S&&>(s).f_xi_m1_refqual_r (0) != iobj_fn) |
| 338 |
__builtin_abort (); |
| 339 |
if (s.f_xi_c0_refqual_r (0) != iobj_fn) |
| 340 |
__builtin_abort (); |
| 341 |
if (static_cast<S&&>(s).f_xi_c1_refqual_r (0) != iobj_fn) |
| 342 |
__builtin_abort (); |
| 343 |
|
| 344 |
|
| 345 |
/* Note: |
| 346 |
These cases are weird, the object argument correspond, but are not the same |
| 347 |
type ([basic.scope.scope-3.1]), so we get this funny edge case where the |
| 348 |
constraint stops them from being considered redeclarations, but isn't taken |
| 349 |
into account for the lvalue case. You can't bind an lvalue to an rvalue |
| 350 |
reference so the iobj member function is always taken regardless of which |
| 351 |
overload is constrained. |
| 352 |
|
| 353 |
[over.match.funcs.general-4] |
| 354 |
For implicit object member functions, the type of the implicit object |
| 355 |
parameter is |
| 356 |
(4.1) “lvalue reference to cv X” for functions declared without a |
| 357 |
ref-qualifier or with the & ref-qualifier |
| 358 |
|
| 359 |
You would think that calling these functions with an rvalue would be the |
| 360 |
same then, always taking the xobj member function. However, for backwards |
| 361 |
compatibility reasons, an unqualified member function can be called on an |
| 362 |
object that is an rvalue. |
| 363 |
|
| 364 |
[over.match.funcs.general-5] |
| 365 |
For implicit object member functions declared without a ref-qualifier, even |
| 366 |
if the implicit object parameter is not const-qualified, an rvalue can be |
| 367 |
bound to the parameter as long as in all other respects the argument can be |
| 368 |
converted to the type of the implicit object parameter. |
| 369 |
|
| 370 |
And finally, since the object parameters correspond ([basic.scope.scope-3.1]) |
| 371 |
the constraints are taken into account. |
| 372 |
|
| 373 |
So in conclusion, calling these functions with an lvalue always resolves to |
| 374 |
the iobj member function, and calling them with rvalues take the constraints |
| 375 |
into account. |
| 376 |
|
| 377 |
As wacky as this is, this is the correct behavior. */ |
| 378 |
|
| 379 |
// Always takes the iobj member function, can't bind an lvalue to an rvalue |
| 380 |
// reference. |
| 381 |
if (s.f_ix_m1 (0) != iobj_fn) |
| 382 |
__builtin_abort (); |
| 383 |
if (s.f_ix_c1 (0) != iobj_fn) |
| 384 |
__builtin_abort (); |
| 385 |
if (s.f_xi_m1 (0) != iobj_fn) |
| 386 |
__builtin_abort (); |
| 387 |
if (s.f_xi_c1 (0) != iobj_fn) |
| 388 |
__builtin_abort (); |
| 389 |
|
| 390 |
if (s.f_ix_m1_r (0) != iobj_fn) |
| 391 |
__builtin_abort (); |
| 392 |
if (s.f_ix_c1_r (0) != iobj_fn) |
| 393 |
__builtin_abort (); |
| 394 |
if (s.f_xi_m1_r (0) != iobj_fn) |
| 395 |
__builtin_abort (); |
| 396 |
if (s.f_xi_c1_r (0) != iobj_fn) |
| 397 |
__builtin_abort (); |
| 398 |
|
| 399 |
// Constraints are taken into account here, see note for more information. |
| 400 |
if (static_cast<S&&>(s).f_ix_m1 (0) != iobj_fn) |
| 401 |
__builtin_abort (); |
| 402 |
if (static_cast<S&&>(s).f_ix_c1 (0) != iobj_fn) |
| 403 |
__builtin_abort (); |
| 404 |
if (static_cast<S&&>(s).f_xi_m1 (0) != xobj_fn) |
| 405 |
__builtin_abort (); |
| 406 |
if (static_cast<S&&>(s).f_xi_c1 (0) != xobj_fn) |
| 407 |
__builtin_abort (); |
| 408 |
|
| 409 |
if (static_cast<S&&>(s).f_ix_m1_r (0) != xobj_fn) |
| 410 |
__builtin_abort (); |
| 411 |
if (static_cast<S&&>(s).f_ix_c1_r (0) != xobj_fn) |
| 412 |
__builtin_abort (); |
| 413 |
if (static_cast<S&&>(s).f_xi_m1_r (0) != iobj_fn) |
| 414 |
__builtin_abort (); |
| 415 |
if (static_cast<S&&>(s).f_xi_c1_r (0) != iobj_fn) |
| 416 |
__builtin_abort (); |
| 417 |
} |
| 418 |
|