p3.cpp
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// This test is for the [class.compare.default]p3 added by P2002R0
// RUN: %clang_cc1 -std=c++2a -verify %s
namespace std {
struct strong_ordering {
int n;
constexpr operator int() const { return n; }
static const strong_ordering less, equal, greater;
};
constexpr strong_ordering strong_ordering::less = {-1};
constexpr strong_ordering strong_ordering::equal = {0};
constexpr strong_ordering strong_ordering::greater = {1};
}
struct A {
friend bool operator==(const A&, const A&) = default;
friend bool operator!=(const A&, const A&) = default;
friend std::strong_ordering operator<=>(const A&, const A&) = default;
friend bool operator<(const A&, const A&) = default;
friend bool operator<=(const A&, const A&) = default;
friend bool operator>(const A&, const A&) = default;
friend bool operator>=(const A&, const A&) = default;
};
struct TestA {
friend constexpr bool operator==(const A&, const A&) noexcept;
friend constexpr bool operator!=(const A&, const A&) noexcept;
friend constexpr std::strong_ordering operator<=>(const A&, const A&) noexcept;
friend constexpr bool operator<(const A&, const A&);
friend constexpr bool operator<=(const A&, const A&);
friend constexpr bool operator>(const A&, const A&);
friend constexpr bool operator>=(const A&, const A&);
};
// Declaration order doesn't matter, even though the secondary operators need
// to know whether the primary ones are constexpr.
struct ReversedA {
friend bool operator>=(const ReversedA&, const ReversedA&) = default;
friend bool operator>(const ReversedA&, const ReversedA&) = default;
friend bool operator<=(const ReversedA&, const ReversedA&) = default;
friend bool operator<(const ReversedA&, const ReversedA&) = default;
friend std::strong_ordering operator<=>(const ReversedA&, const ReversedA&) = default;
friend bool operator!=(const ReversedA&, const ReversedA&) = default;
friend bool operator==(const ReversedA&, const ReversedA&) = default;
};
struct TestReversedA {
friend constexpr bool operator>=(const ReversedA&, const ReversedA&);
friend constexpr bool operator>(const ReversedA&, const ReversedA&);
friend constexpr bool operator<=(const ReversedA&, const ReversedA&);
friend constexpr bool operator<(const ReversedA&, const ReversedA&);
friend constexpr std::strong_ordering operator<=>(const ReversedA&, const ReversedA&) noexcept;
friend constexpr bool operator!=(const ReversedA&, const ReversedA&) noexcept;
friend constexpr bool operator==(const ReversedA&, const ReversedA&) noexcept;
};
struct B {
A a;
friend bool operator==(const B&, const B&) = default;
friend bool operator!=(const B&, const B&) = default;
friend std::strong_ordering operator<=>(const B&, const B&) = default;
friend bool operator<(const B&, const B&) = default;
friend bool operator<=(const B&, const B&) = default;
friend bool operator>(const B&, const B&) = default;
friend bool operator>=(const B&, const B&) = default;
};
struct TestB {
friend constexpr bool operator==(const B&, const B&) noexcept;
friend constexpr bool operator!=(const B&, const B&) noexcept;
friend constexpr std::strong_ordering operator<=>(const B&, const B&);
friend constexpr bool operator<(const B&, const B&);
friend constexpr bool operator<=(const B&, const B&);
friend constexpr bool operator>(const B&, const B&);
friend constexpr bool operator>=(const B&, const B&);
};
struct C {
friend bool operator==(const C&, const C&); // expected-note {{previous}} expected-note 2{{here}}
friend bool operator!=(const C&, const C&) = default; // expected-note {{previous}}
friend std::strong_ordering operator<=>(const C&, const C&); // expected-note {{previous}} expected-note 2{{here}}
friend bool operator<(const C&, const C&) = default; // expected-note {{previous}}
friend bool operator<=(const C&, const C&) = default; // expected-note {{previous}}
friend bool operator>(const C&, const C&) = default; // expected-note {{previous}}
friend bool operator>=(const C&, const C&) = default; // expected-note {{previous}}
};
struct TestC {
friend constexpr bool operator==(const C&, const C&); // expected-error {{non-constexpr}}
friend constexpr bool operator!=(const C&, const C&); // expected-error {{non-constexpr}}
friend constexpr std::strong_ordering operator<=>(const C&, const C&); // expected-error {{non-constexpr}}
friend constexpr bool operator<(const C&, const C&); // expected-error {{non-constexpr}}
friend constexpr bool operator<=(const C&, const C&); // expected-error {{non-constexpr}}
friend constexpr bool operator>(const C&, const C&); // expected-error {{non-constexpr}}
friend constexpr bool operator>=(const C&, const C&); // expected-error {{non-constexpr}}
};
struct D {
A a;
C c;
A b;
friend bool operator==(const D&, const D&) = default; // expected-note {{previous}}
friend bool operator!=(const D&, const D&) = default; // expected-note {{previous}}
friend std::strong_ordering operator<=>(const D&, const D&) = default; // expected-note {{previous}}
friend bool operator<(const D&, const D&) = default; // expected-note {{previous}}
friend bool operator<=(const D&, const D&) = default; // expected-note {{previous}}
friend bool operator>(const D&, const D&) = default; // expected-note {{previous}}
friend bool operator>=(const D&, const D&) = default; // expected-note {{previous}}
};
struct TestD {
friend constexpr bool operator==(const D&, const D&); // expected-error {{non-constexpr}}
friend constexpr bool operator!=(const D&, const D&); // expected-error {{non-constexpr}}
friend constexpr std::strong_ordering operator<=>(const D&, const D&); // expected-error {{non-constexpr}}
friend constexpr bool operator<(const D&, const D&); // expected-error {{non-constexpr}}
friend constexpr bool operator<=(const D&, const D&); // expected-error {{non-constexpr}}
friend constexpr bool operator>(const D&, const D&); // expected-error {{non-constexpr}}
friend constexpr bool operator>=(const D&, const D&); // expected-error {{non-constexpr}}
};
struct E {
A a;
C c; // expected-note 2{{non-constexpr comparison function would be used to compare member 'c'}}
A b;
friend constexpr bool operator==(const E&, const E&) = default; // expected-error {{cannot be declared constexpr because it invokes a non-constexpr comparison function}}
friend constexpr bool operator!=(const E&, const E&) = default;
friend constexpr std::strong_ordering operator<=>(const E&, const E&) = default; // expected-error {{cannot be declared constexpr because it invokes a non-constexpr comparison function}}
friend constexpr bool operator<(const E&, const E&) = default;
friend constexpr bool operator<=(const E&, const E&) = default;
friend constexpr bool operator>(const E&, const E&) = default;
friend constexpr bool operator>=(const E&, const E&) = default;
};
struct E2 : A, C { // expected-note 2{{non-constexpr comparison function would be used to compare base class 'C'}}
friend constexpr bool operator==(const E2&, const E2&) = default; // expected-error {{cannot be declared constexpr because it invokes a non-constexpr comparison function}}
friend constexpr bool operator!=(const E2&, const E2&) = default;
friend constexpr std::strong_ordering operator<=>(const E2&, const E2&) = default; // expected-error {{cannot be declared constexpr because it invokes a non-constexpr comparison function}}
friend constexpr bool operator<(const E2&, const E2&) = default;
friend constexpr bool operator<=(const E2&, const E2&) = default;
friend constexpr bool operator>(const E2&, const E2&) = default;
friend constexpr bool operator>=(const E2&, const E2&) = default;
};
struct F {
friend bool operator==(const F&, const F&); // expected-note {{here}}
friend constexpr bool operator!=(const F&, const F&) = default; // expected-error {{cannot be declared constexpr because it invokes a non-constexpr comparison function}}
friend std::strong_ordering operator<=>(const F&, const F&); // expected-note 4{{here}}
friend constexpr bool operator<(const F&, const F&) = default; // expected-error {{cannot be declared constexpr because it invokes a non-constexpr comparison function}}
friend constexpr bool operator<=(const F&, const F&) = default; // expected-error {{cannot be declared constexpr because it invokes a non-constexpr comparison function}}
friend constexpr bool operator>(const F&, const F&) = default; // expected-error {{cannot be declared constexpr because it invokes a non-constexpr comparison function}}
friend constexpr bool operator>=(const F&, const F&) = default; // expected-error {{cannot be declared constexpr because it invokes a non-constexpr comparison function}}
};
// No implicit 'constexpr' if it's not the first declaration.
// FIXME: This rule creates problems for reordering of declarations; is this
// really the right model?
struct G;
bool operator==(const G&, const G&);
bool operator!=(const G&, const G&);
std::strong_ordering operator<=>(const G&, const G&);
bool operator<(const G&, const G&);
bool operator<=(const G&, const G&);
bool operator>(const G&, const G&);
bool operator>=(const G&, const G&);
struct G {
friend bool operator==(const G&, const G&) = default;
friend bool operator!=(const G&, const G&) = default;
friend std::strong_ordering operator<=>(const G&, const G&) = default;
friend bool operator<(const G&, const G&) = default;
friend bool operator<=(const G&, const G&) = default;
friend bool operator>(const G&, const G&) = default;
friend bool operator>=(const G&, const G&) = default;
};
bool operator==(const G&, const G&);
bool operator!=(const G&, const G&);
std::strong_ordering operator<=>(const G&, const G&);
bool operator<(const G&, const G&);
bool operator<=(const G&, const G&);
bool operator>(const G&, const G&);
bool operator>=(const G&, const G&);
namespace PR44721 {
template <typename T> bool operator==(T const &, T const &) { return true; }
template <typename T, typename U> bool operator!=(T const &, U const &) { return true; }
template <typename T> int operator<=>(T const &, T const &) { return 0; }
struct S {
friend bool operator==(const S &, const S &) = default;
friend bool operator<=>(const S &, const S &) = default;
int x;
};
}