1 // RUN: %clang_cc1 -std=c++2a -verify %s
2 
3 struct B {};
4 bool operator==(const B&, const B&) = default; // expected-error {{equality comparison operator can only be defaulted in a class definition}}
5 bool operator<=>(const B&, const B&) = default; // expected-error {{three-way comparison operator can only be defaulted in a class definition}}
6 
7 template<typename T = void>
8   bool operator<(const B&, const B&) = default; // expected-error {{comparison operator template cannot be defaulted}}
9 
10 struct A {
11   friend bool operator==(const A&, const A&) = default;
12   friend bool operator!=(const A&, const B&) = default; // expected-error {{invalid parameter type for defaulted equality comparison}}
13   friend bool operator!=(const B&, const B&) = default; // expected-error {{invalid parameter type for defaulted equality comparison}}
14   friend bool operator<(const A&, const A&);
15   friend bool operator<(const B&, const B&) = default; // expected-error {{invalid parameter type for defaulted relational comparison}}
16   friend bool operator>(A, A) = default; // expected-error {{invalid parameter type for defaulted relational comparison}}
17 
18   bool operator<(const A&) const;
19   bool operator<=(const A&) const = default;
20   bool operator==(const A&) const volatile && = default; // surprisingly, OK
21   bool operator<=>(const A&) = default; // expected-error {{defaulted member three-way comparison operator must be const-qualified}}
22   bool operator>=(const B&) const = default; // expected-error {{invalid parameter type for defaulted relational comparison}}
23   static bool operator>(const B&) = default; // expected-error {{overloaded 'operator>' cannot be a static member function}}
24 
25   template<typename T = void>
26     friend bool operator==(const A&, const A&) = default; // expected-error {{comparison operator template cannot be defaulted}}
27   template<typename T = void>
28     bool operator==(const A&) const = default; // expected-error {{comparison operator template cannot be defaulted}}
29 };
30 
31 // FIXME: The wording is not clear as to whether these are valid, but the
32 // intention is that they are not.
33 bool operator<(const A&, const A&) = default; // expected-error {{relational comparison operator can only be defaulted in a class definition}}
34 bool A::operator<(const A&) const = default; // expected-error {{can only be defaulted in a class definition}}
35 
36 template<typename T> struct Dependent {
37   using U = typename T::type;
38   bool operator==(U) const = default; // expected-error {{found 'Dependent<Bad>::U'}}
39   friend bool operator==(U, U) = default; // expected-error {{found 'Dependent<Bad>::U'}}
40 };
41 
42 struct Good { using type = const Dependent<Good>&; };
43 template struct Dependent<Good>;
44 
45 struct Bad { using type = Dependent<Bad>&; };
46 template struct Dependent<Bad>; // expected-note {{in instantiation of}}
47 
48 
49 namespace std {
50   struct strong_ordering {
51     int n;
52     constexpr operator int() const { return n; }
53     static const strong_ordering equal, greater, less;
54   };
55   constexpr strong_ordering strong_ordering::equal = {0};
56   constexpr strong_ordering strong_ordering::greater = {1};
57   constexpr strong_ordering strong_ordering::less = {-1};
58 }
59 
60 namespace LookupContext {
61   struct A {};
62 
63   namespace N {
64     template <typename T> auto f() {
65       bool operator==(const T &, const T &);
66       bool operator<(const T &, const T &);
67       struct B {
68         T a;
69         std::strong_ordering operator<=>(const B &) const = default;
70       };
71       return B();
72     }
73 
74     auto g() {
75       struct Cmp { Cmp(std::strong_ordering); };
76       Cmp operator<=>(const A&, const A&);
77       bool operator!=(const Cmp&, int);
78       struct B {
79         A a;
80         Cmp operator<=>(const B &) const = default;
81       };
82       return B();
83     }
84 
85     auto h() {
86       struct B;
87       bool operator==(const B&, const B&);
88       bool operator!=(const B&, const B&); // expected-note 2{{best match}}
89       std::strong_ordering operator<=>(const B&, const B&);
90       bool operator<(const B&, const B&); // expected-note 2{{best match}}
91       bool operator<=(const B&, const B&); // expected-note 2{{best match}}
92       bool operator>(const B&, const B&); // expected-note 2{{best match}}
93       bool operator>=(const B&, const B&); // expected-note 2{{best match}}
94 
95       struct B {
96         bool operator!=(const B&) const = default; // expected-warning {{implicitly deleted}} expected-note {{deleted here}}
97         bool operator<(const B&) const = default; // expected-warning {{implicitly deleted}} expected-note {{deleted here}}
98         bool operator<=(const B&) const = default; // expected-warning {{implicitly deleted}} expected-note {{deleted here}}
99         bool operator>(const B&) const = default; // expected-warning {{implicitly deleted}} expected-note {{deleted here}}
100         bool operator>=(const B&) const = default; // expected-warning {{implicitly deleted}} expected-note {{deleted here}}
101       };
102       return B();
103     }
104   }
105 
106   namespace M {
107     bool operator==(const A &, const A &) = delete;
108     bool operator<(const A &, const A &) = delete;
109     bool cmp = N::f<A>() < N::f<A>();
110 
111     void operator<=>(const A &, const A &) = delete;
112     auto cmp2 = N::g() <=> N::g();
113 
114     void use_h() {
115       N::h() != N::h(); // expected-error {{implicitly deleted}}
116       N::h() < N::h(); // expected-error {{implicitly deleted}}
117       N::h() <= N::h(); // expected-error {{implicitly deleted}}
118       N::h() > N::h(); // expected-error {{implicitly deleted}}
119       N::h() >= N::h(); // expected-error {{implicitly deleted}}
120     }
121   }
122 }
123