p5.cpp
1.56 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
// RUN: %clang_cc1 -std=c++2a -verify %s
// expected-no-diagnostics
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}, strong_ordering::equal{0}, strong_ordering::greater{1};
}
// Check that we compare subobjects in the right order.
struct Log {
char buff[10] = {};
int n = 0;
constexpr void add(char c) { buff[n++] = c; }
constexpr bool operator==(const char *p) const { return __builtin_strcmp(p, buff) == 0; }
};
template<char C> struct B {
Log *log;
constexpr bool operator==(const B&) const { log->add(C); return true; }
constexpr std::strong_ordering operator<=>(const B&) const { log->add(C); return {0}; }
};
template<typename T> constexpr bool check(bool which, const char *str) {
Log log;
T c(&log);
(void)(which ? c == c : c <=> c);
return log == str;
}
struct C : B<'a'>, B<'b'> {
B<'r'> r[3];
B<'c'> c;
B<'s'> s[2];
B<'d'> d;
constexpr C(Log *p) : B<'a'>{p}, B<'b'>{p}, r{p, p, p}, c{p}, s{p, p}, d{p} {}
bool operator==(const C&) const = default;
std::strong_ordering operator<=>(const C&) const = default;
};
static_assert(check<C>(false, "abrrrcssd"));
static_assert(check<C>(true, "abrrrcssd"));
struct D {
B<'x'> x;
B<'y'> y[2];
constexpr D(Log *p) : x{p}, y{p, p} {}
bool operator==(const D&) const = default;
std::strong_ordering operator<=>(const D&) const = default;
};
static_assert(check<D>(false, "xyy"));
static_assert(check<D>(true, "xyy"));