Debian Codegraph @

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

raw
1 // P0847R7
2 // { dg-do compile { target c++23 } }
3
4 // user defined copy/move assignment operators
5
6 inline constexpr int add_when_copy = 5;
7 inline constexpr int add_when_move = 10;
8 inline constexpr int poison = -1;
9
10 struct S {
11 int _v;
12 S& operator=(this S& self, S const& rhs) {
13 self._v = rhs._v + add_when_copy;
14 return self;
15 };
16 S& operator=(this S& self, S&& rhs) {
17 self._v = rhs._v + add_when_move;
18 rhs._v = poison;
19 return self;
20 }
21 };
22
23 inline constexpr int init_val = 5;
24
25 int main()
26 {
27 S s0{0};
28 S s1{init_val};
29
30 // Sanity check.
31 if (s0._v != 0
32 || s1._v != init_val)
33 __builtin_abort ();
34
35 s0 = s1;
36 if (s0._v != init_val + add_when_copy)
37 __builtin_abort ();
38 if (s1._v != init_val)
39 __builtin_abort ();
40
41 s0 = S{init_val};
42 if (s0._v != init_val + add_when_move)
43 __builtin_abort ();
44
45 S s2{init_val};
46 s0 = static_cast<S&&>(s2);
47 if (s0._v != init_val + add_when_move)
48 __builtin_abort ();
49 if (s2._v != poison)
50 __builtin_abort ();
51 }