Debian Codegraph @

gcc/testsuite/g++.dg/cpp2a/spaceship-eq15.C

raw
1 // P2468R2 - The Equality Operator You Are Looking For
2 // { dg-do compile { target c++20 } }
3
4 struct A {
5 bool operator== (const A &) { return true; }
6 bool operator!= (const A &) { return false; }
7 };
8 bool a = A{} != A{};
9
10 template <typename T>
11 struct B {
12 bool operator== (const T &) const;
13 bool operator!= (const T &) const;
14 };
15 struct B1 : B<B1> { };
16 bool b1 = B1{} == B1{};
17 bool b2 = B1{} != B1{};
18
19 template <bool>
20 struct C {
21 using C1 = C<true>;
22 using C2 = C<false>;
23 C () = default;
24 C (const C2 &);
25 bool operator== (C1) const;
26 bool operator!= (C1) const;
27 };
28 using C3 = C<false>;
29 bool c = C3{} == C3{};
30
31 struct D {
32 D ();
33 D (int *);
34 bool operator== (const D &) const; // { dg-message "candidate 1: 'bool D::operator==\\\(const D&\\\) const' \\\(reversed\\\)" }
35 operator int * () const;
36 };
37 bool d = nullptr != D{}; // { dg-error "ambiguous overload for 'operator!=' in 'nullptr != D\\\(\\\)' \\\(operand types are 'std::nullptr_t' and 'D'\\\)" }
38 // { dg-message "candidate 2: 'operator!=\\\(int\\\*, int\\\*\\\)' \\\(built-in\\\)" "" { target *-*-* } .-1 }
39
40 using ubool = unsigned char;
41
42 struct E {
43 operator bool () const;
44 };
45 unsigned char operator== (E, E);// { dg-message "candidate 2: 'unsigned char operator==\\\(E, E\\\)'" }
46 // { dg-message "no known conversion for argument 1 from 'int' to 'E'" "" { target *-*-* } .-1 }
47 unsigned char e = E{} != E{}; // { dg-error "return type of 'unsigned char operator==\\\(E, E\\\)' is not 'bool'" }
48 // { dg-message "used as rewritten candidate for comparison of 'E' and 'E'" "" { target *-*-* } .-1 }
49
50 // F-H are the testcase from [over.match.oper]
51 struct F {};
52 template <typename T>
53 bool operator== (F, T); // { dg-message "candidate 1: 'template<class T> bool operator==\\\(F, T\\\)'" }
54 // { dg-message "template argument deduction/substitution failed:" "" { target *-*-* } .-1 }
55 bool f1 = 0 == F (); // OK, calls reversed ==
56 template <typename T>
57 bool operator!= (F, T);
58 bool f2 = 0 == F (); // { dg-error "no match for 'operator==' in '0 == F\\\(\\\)' \\\(operand types are 'int' and 'F'\\\)" }
59 // { dg-message "cannot convert '0' \\\(type 'int'\\\) to type 'F'" "" { target *-*-* } .-1 }
60
61 struct G {
62 bool operator== (const G &);
63 };
64 struct G1 : G {
65 G1 ();
66 G1 (G);
67 bool operator!= (const G &);
68 };
69 bool g1 = G () == G1 (); // OK, != prevents rewrite
70 bool g2 = G1 () == G (); // { dg-error "ambiguous, even though the second is reversed" }
71
72 struct H {};
73 template <typename T>
74 bool operator== (H, T);
75 inline namespace H1 {
76 template <typename T>
77 bool operator!= (H, T);
78 }
79 // [over.match.oper] currently says that this is OK because the inline
80 // namespace isn't searched, but that seems wrong to me, so I'm going to go
81 // ahead and search it for now. Remove the "0 &&" in add_candidates to
82 // change this to the currently specified behavior.
83 // { dg-error "no match" "" { target *-*-* } .+1 }
84 bool h = 0 == H (); // OK, calls reversed ==
85
86 template <class T>
87 struct I {
88 int operator== (const double &) const;
89 friend inline int operator== (const double &, const T &) { return 1; }
90 };
91 struct I1 : I<I1> { };
92 bool i = I1{} == 0.; // { dg-error "return type of 'int operator==\\\(const double&, const I1&\\\)' is not 'bool'" }
93 // { dg-message "used as rewritten candidate for comparison of 'I1' and 'double'" "" { target *-*-* } .-1 }
94
95 struct J {
96 bool operator== (const J &) const;
97 bool operator!= (const J &) const;
98 };
99 struct J1 : J {
100 J1 (const J &);
101 bool operator== (const J1 &x) const {
102 return static_cast<const J &> (*this) == x; // { dg-error "ambiguous overload for 'operator==' in '\\\*\\\(const J\\\*\\\)\\\(\\\(const J1\\\*\\\)this\\\) == x' \\\(operand types are 'const J' and 'const J1'\\\)" }
103 }
104 };
105
106 struct K {
107 bool operator== (const K &);
108 };
109 bool k = K{} == K{}; // { dg-error "ambiguous, even though the second is reversed" }
110
111 struct L {
112 bool operator== (const L &) const;
113 };
114 bool l = L{} == L{};
115
116 struct M {
117 bool operator== (M);
118 };
119 bool m = M () == M ();
120
121 struct N {
122 virtual bool operator== (const N &) const;
123 };
124 struct N1 : N {
125 bool operator== (const N &) const override;
126 };
127 bool n = N1 () == N1 (); // { dg-error "ambiguous, even though the second is reversed" }
128
129 struct O {
130 virtual signed char operator== (const O &) const;
131 signed char operator!= (const O &x) const { return !operator== (x); }
132 };
133 struct O1 : O {
134 signed char operator== (const O &) const override;
135 };
136 bool o = O1 () != O1 ();
137
138 template <class T>
139 bool
140 foo (T x, T y)
141 requires requires { x == y; }
142 {
143 return x == y;
144 }
145 bool b3 = foo (B1 (), B1 ());
146
147 struct P {};
148 template <typename T, class U = int>
149 bool operator== (P, T);
150 template <class T>
151 bool operator!= (P, T);
152 bool p = 0 == P ();
153
154 struct Q {};
155 template <typename T>
156 bool operator== (Q, T);
157 template <typename U>
158 bool operator!= (Q, U);
159 bool q = 0 == Q (); // { dg-error "" }
160
161 struct R {
162 template <typename T>
163 bool operator== (const T &);
164 };
165 bool r = R () == R (); // { dg-error "ambiguous, even though the second is reversed" }
166
167 struct S {
168 template <typename T>
169 bool operator== (const T &) const;
170 bool operator!= (const S &);
171 };
172 bool s = S () == S ();
173
174 struct T {};
175 template <class U = int>
176 bool operator== (T, int);
177 bool operator!= (T, int);
178 bool t = 0 == T ();
179
180 struct U {};
181 bool operator== (U, int);
182 bool u1 = 0 == U ();
183 namespace U1 { bool operator!= (U, int); }
184 bool u2 = 0 == U ();
185 using U1::operator!=;
186 bool u3 = 0 == U (); // { dg-error "" }
187
188 struct V {};
189 template <typename T>
190 bool operator== (V, T);
191 bool v1 = 0 == V ();
192 namespace V1 { template <typename T> bool operator!= (V, T); }
193 bool v2 = 0 == V ();
194 using V1::operator!=;
195 bool v3 = 0 == V (); // { dg-error "" }
196
197 template <int N>
198 struct W {
199 bool operator== (int) requires (N == 1);
200 bool operator!= (int) requires (N == 2);
201 };
202 int w = 0 == W<1> ();
203
204 struct X {
205 bool operator== (X const &);
206 static bool operator!= (X const &, X const &); // { dg-error "'static bool X::operator!=\\\(const X&, const X&\\\)' must be either a non-static member function or a non-member function" }
207 };
208 bool x = X () == X (); // { dg-error "ambiguous, even though the second is reversed" }