Debian Codegraph @

gcc/testsuite/g++.dg/cpp23/explicit-obj-by-value2.C

raw
1 // P0847R7
2 // { dg-do run { target c++23 } }
3
4 // conversion of the implicit object argument to an xobj parameter
5 // using a user defined conversion or converting constructor
6 // when calling by value xobj member functions
7
8 // see explicit-obj-by-value1.C for details on this test
9
10 using uintptr_t = __UINTPTR_TYPE__;
11 inline constexpr uintptr_t magic = 42;
12
13 struct S;
14
15 struct FromS {
16 uintptr_t _v;
17 FromS(S);
18 };
19
20 struct S {
21 operator uintptr_t() const {
22 return magic;
23 }
24 uintptr_t f(this uintptr_t n) {
25 return n;
26 }
27 uintptr_t g(this FromS from_s) {
28 return from_s._v;
29 }
30 };
31
32 FromS::FromS(S) : _v(magic) {}
33
34
35 int main()
36 {
37 S s0{};
38 S s1{};
39 // prevent (absurdly improbable) bogus failures
40 S& s = magic != (uintptr_t)(&s0) ? s0 : s1;
41
42 uintptr_t const ret0 = s.f();
43 // check for reinterpretation of the object argument
44 if (ret0 == (uintptr_t)(&s))
45 __builtin_abort ();
46 // check for a bugged conversion
47 if (ret0 != magic)
48 __builtin_abort ();
49
50 uintptr_t const ret1 = s.g();
51 // check for reinterpretation of the object argument
52 if (ret1 == (uintptr_t)(&s))
53 __builtin_abort ();
54 // check for a bugged conversion
55 if (ret1 != magic)
56 __builtin_abort ();
57 }
58