c++: P0847R7 (deducing this) - initial functionality. [PR102609]
[official-gcc.git] / gcc / testsuite / g++.dg / cpp23 / explicit-obj-by-value1.C
blob5ea5bcb7faaceefbee441431793ef68b5c9f0489
1 // P0847R7
2 // { dg-do run { target c++23 } }
4 // conversion of the implicit object argument to an xobj parameter
5 // when calling by value xobj member functions
7 // The initial implementation of xobj member functions incorrectly did not
8 // convert the implicit object argument when binding to the xobj
9 // parameter. In spite of this, it did correctly check to see if such a
10 // conversion would be valid, thus no diagnostic would be emitted when a
11 // conversion was valid, but instead of applying the conversion, the
12 // argument would silently be reinterpreted as the type of the parameter. 
14 // This is why we use uintptr_t for the value in S and compare the result
15 // of f to &s, we want to test for simple reinterpretation of the
16 // argument. To accurately test for this we make sure to use an object
17 // that has a different address than the value of our magic number. It's
18 // an impossibly improbable edge case but it's trivial to work around. We
19 // still compare against both the address of s and the magic number so we
20 // can additionally test for bugged conversions, while also
21 // differentiating that case from reinterpretation of the argument.
23 using uintptr_t = __UINTPTR_TYPE__;
24 inline constexpr uintptr_t magic = 42;
26 struct S {
27     uintptr_t _v;
28     uintptr_t f(this S self) {
29         return self._v;
30     }
33 int main() 
35   S s0{magic};
36   S s1{magic};
37   // prevent (absurdly improbable) bogus failures
38   S& s = magic != (uintptr_t)(&s0) ? s0 : s1;
40   uintptr_t const ret = s.f();
41   // check for reinterpretation of the object argument
42   if (ret == (uintptr_t)(&s))
43     __builtin_abort ();
44   // check for a bugged conversion
45   if (ret != magic)
46     __builtin_abort ();