Debian Codegraph @

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

raw
1 // P0847R7
2 // { dg-do run { target c++23 } }
3
4 // defaulted 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 A {
11 int _v;
12 A(int v) : _v(v) {}
13 A& operator=(A const& rhs) {
14 if (&rhs == this)
15 return *this;
16 _v = rhs._v + add_when_copy;
17 return *this;
18 }
19 A& operator=(A&& rhs) {
20 if (&rhs == this)
21 return *this;
22 _v = rhs._v + add_when_move;
23 rhs._v = poison;
24 return *this;
25 }
26 };
27
28 struct S {
29 A _a;
30 S& operator=(this S&, S const&) = default;
31 S& operator=(this S&, S&&) = default;
32
33 int v() const { return _a._v; }
34 };
35
36 inline constexpr int init_val = 5;
37
38 int main()
39 {
40 S s0{0};
41 S s1{init_val};
42
43 // Sanity check.
44 if (s0.v () != 0
45 || s1.v () != init_val)
46 __builtin_abort ();
47
48 s0 = s1;
49 if (s0.v () != init_val + add_when_copy)
50 __builtin_abort ();
51 if (s1.v () != init_val)
52 __builtin_abort ();
53
54 s0 = S{init_val};
55 if (s0.v () != init_val + add_when_move)
56 __builtin_abort ();
57
58 S s2{init_val};
59 s0 = static_cast<S&&>(s2);
60 if (s0.v () != init_val + add_when_move)
61 __builtin_abort ();
62 if (s2.v () != poison)
63 __builtin_abort ();
64 }
65