Debian Codegraph @

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

raw
1 // PR c++/96299
2 // P1186R3
3 // { dg-do compile { target c++20 } }
4
5 #include <compare>
6
7 struct H {
8 bool operator==(H const &) const;
9 bool operator<(H const &) const;
10 };
11
12 // Can't deduce category from op< and op==.
13 struct A {
14 int i;
15 char c;
16 H h; // { dg-error "operator<=>" }
17 auto operator<=>(A const &) const = default;
18 };
19
20 // OK, explicit return type tells us how to interpret op</==.
21 struct B {
22 int i;
23 char c;
24 H h;
25 std::strong_ordering operator<=>(B const &) const = default;
26 };
27
28 struct C {
29 bool operator<(C const &) const;
30 };
31
32 // C's op< isn't enough, need op== as well.
33 struct D {
34 C c; // { dg-error "operator<=>" }
35 std::strong_ordering operator<=>(D const &) const = default;
36 };
37
38 struct E {
39 std::partial_ordering operator<=>(E const &) const;
40 };
41
42 // Can't use a weak op<=> to build a strong op<=>.
43 struct F {
44 E e; // { dg-error "strong_ordering" }
45 H h;
46 std::strong_ordering operator<=>(F const &) const = default;
47 };
48
49 struct G {
50 std::strong_ordering operator<=>(G const &) const;
51 };
52
53 // OK, uses op<=> for g and op</op== for h.
54 struct I {
55 G g;
56 H h;
57 std::partial_ordering operator<=>(I const &) const = default;
58 };
59
60 template <class T>
61 struct J {
62 T t; // { dg-error "no match|ambiguous" }
63 auto operator<=>(J const &) const = default;
64 };
65
66 template <class T>
67 struct K {
68 T t; // { dg-error "no match" }
69 std::partial_ordering operator<=>(K const &) const = default;
70 };
71
72 template <class T>
73 struct M {
74 T t; // { dg-error "no match|strong_ordering" }
75 std::strong_ordering operator<=>(M const &) const = default;
76 };
77
78 // Test that we don't fall back to </== if <=> is ambiguous.
79 struct N {
80 bool operator==(N const &) const;
81 bool operator<(N const &) const;
82 };
83 template <class T>
84 std::partial_ordering operator<=>(N const &, T&&);
85 template <class T>
86 std::partial_ordering operator<=>(T&&, N const &);
87
88 void
89 foo (A &a1, A &a2, B &b1, B &b2, D& d1, D& d2, F& f1, F& f2, I& i1, I& i2)
90 {
91 auto a = a1 < a2; // { dg-error "deleted" }
92 auto b = b1 < b2;
93 auto d = d1 < d2; // { dg-error "deleted" }
94 auto f = f1 < f2; // { dg-error "deleted" }
95 auto i = i1 < i2;
96
97 auto j1 = J<int>() < J<int>();
98 auto j2 = J<H>() < J<H>(); // { dg-error "deleted" }
99 auto j3 = J<C>() < J<C>(); // { dg-error "deleted" }
100 auto j4 = J<E>() < J<E>();
101 auto j5 = J<G>() < J<G>();
102 auto j6 = J<N>() < J<N>(); // { dg-error "deleted" }
103
104 auto k1 = K<int>() < K<int>();
105 auto k2 = K<H>() < K<H>();
106 auto k3 = K<C>() < K<C>(); // { dg-error "deleted" }
107 auto k4 = K<E>() < K<E>();
108 auto k5 = K<G>() < K<G>();
109 auto k6 = K<N>() < K<N>(); // { dg-error "deleted" }
110
111 auto m1 = M<int>() < M<int>();
112 auto m2 = M<H>() < M<H>();
113 auto m3 = M<C>() < M<C>(); // { dg-error "deleted" }
114 auto m4 = M<E>() < M<E>(); // { dg-error "deleted" }
115 auto m5 = M<G>() < M<G>();
116 auto m6 = M<N>() < M<N>(); // { dg-error "deleted" }
117 }