Welcome to mirror list, hosted at ThFree Co, Russian Federation.

p2.cpp « class.spaceship « class.compare « class « CXX « test « clang - github.com/llvm/llvm-project.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b6e135e0f8654c3e1db106404f60875e439740d3 (plain)
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
205
206
207
208
209
210
211
212
213
214
// RUN: %clang_cc1 -std=c++2a -verify %s

namespace std {
  class strong_ordering {
    int n;
    constexpr strong_ordering(int n) : n(n) {}
  public:
    static const strong_ordering less, equal, greater;
    bool operator!=(int) { return n != 0; }
  };
  constexpr strong_ordering strong_ordering::less{-1},
      strong_ordering::equal{0}, strong_ordering::greater{1};

  class weak_ordering {
    int n;
    constexpr weak_ordering(int n) : n(n) {}
  public:
    constexpr weak_ordering(strong_ordering o);
    static const weak_ordering less, equivalent, greater;
    bool operator!=(int) { return n != 0; }
  };
  constexpr weak_ordering weak_ordering::less{-1},
      weak_ordering::equivalent{0}, weak_ordering::greater{1};

  class partial_ordering {
    int n;
    constexpr partial_ordering(int n) : n(n) {}
  public:
    constexpr partial_ordering(strong_ordering o);
    constexpr partial_ordering(weak_ordering o);
    static const partial_ordering less, equivalent, greater, unordered;
    bool operator!=(int) { return n != 0; }
  };
  constexpr partial_ordering partial_ordering::less{-1},
      partial_ordering::equivalent{0}, partial_ordering::greater{1},
      partial_ordering::unordered{2};
}

namespace DeducedNotCat {
  struct A {
    A operator<=>(const A&) const; // expected-note {{selected 'operator<=>' for member 'a' declared here}}
  };
  struct B {
    A a; // expected-note {{return type 'A' of three-way comparison for member 'a' is not a standard comparison category type}}
    auto operator<=>(const B&) const = default; // expected-warning {{implicitly deleted}}
  };
}

namespace DeducedVsSynthesized {
  struct A {
    bool operator==(const A&) const;
    bool operator<(const A&) const;
  };
  struct B {
    A a; // expected-note {{no viable three-way comparison function for member 'a'}}
    auto operator<=>(const B&) const = default; // expected-warning {{implicitly deleted}}
  };
}

namespace Deduction {
  template<typename T> struct wrap {
    T t;
    friend auto operator<=>(const wrap&, const wrap&) = default;
  };

  using strong = wrap<int>;
  using strong2 = wrap<int*>;
  struct weak {
    friend std::weak_ordering operator<=>(weak, weak);
  };
  using partial = wrap<float>;

  template<typename ...T> struct A : T... {
    friend auto operator<=>(const A&, const A&) = default;
  };

  template<typename Expected, typename ...Ts> void f() {
    using T = Expected; // expected-note {{previous}}
    using T = decltype(A<Ts...>() <=> A<Ts...>()); // expected-error {{different type}}
    void(A<Ts...>() <=> A<Ts...>()); // trigger synthesis of body
  }

  template void f<std::strong_ordering>();
  template void f<std::strong_ordering, strong>();
  template void f<std::strong_ordering, strong, strong2>();

  template void f<std::weak_ordering, weak>();
  template void f<std::weak_ordering, weak, strong>();
  template void f<std::weak_ordering, strong, weak>();

  template void f<std::partial_ordering, partial>();
  template void f<std::partial_ordering, weak, partial>();
  template void f<std::partial_ordering, strong, partial>();
  template void f<std::partial_ordering, partial, weak>();
  template void f<std::partial_ordering, partial, strong>();
  template void f<std::partial_ordering, weak, partial, strong>();

  // Check that the above mechanism works.
  template void f<std::strong_ordering, weak>(); // expected-note {{instantiation of}}

  std::strong_ordering x = A<strong>() <=> A<strong>();
}

namespace PR44723 {
  // Make sure we trigger return type deduction for a callee 'operator<=>'
  // before inspecting its return type.
  template<int> struct a {
    friend constexpr auto operator<=>(a const &lhs, a const &rhs) {
      return std::strong_ordering::equal;
    }
  };
  struct b {
    friend constexpr auto operator<=>(b const &, b const &) = default;
    a<0> m_value;
  };
  std::strong_ordering cmp_b = b() <=> b();

  struct c {
    auto operator<=>(const c&) const&; // expected-note {{selected 'operator<=>' for base class 'c' declared here}}
  };
  struct d : c { // expected-note {{base class 'c' declared here}}
    friend auto operator<=>(const d&, const d&) = default; // #d
    // expected-error@#d {{return type of defaulted 'operator<=>' cannot be deduced because three-way comparison for base class 'c' has a deduced return type and is not yet defined}}
    // expected-warning@#d {{implicitly deleted}}
  };
  auto c::operator<=>(const c&) const& { // #c
    return std::strong_ordering::equal;
  }
  // expected-error@+1 {{overload resolution selected deleted operator '<=>'}}
  std::strong_ordering cmp_d = d() <=> d();
  // expected-note@#c 2{{candidate}}
  // expected-note@#d {{candidate function has been implicitly deleted}}
}

namespace BadDeducedType {
  struct A {
    // expected-error@+1 {{deduced return type for defaulted three-way comparison operator must be 'auto', not 'auto &'}}
    friend auto &operator<=>(const A&, const A&) = default;
  };

  struct B {
    // expected-error@+1 {{deduced return type for defaulted three-way comparison operator must be 'auto', not 'const auto'}}
    friend const auto operator<=>(const B&, const B&) = default;
  };

  template<typename T> struct X {}; // expected-note {{here}}
  struct C {
    // expected-error@+1 {{deduction not allowed in function return type}}
    friend X operator<=>(const C&, const C&) = default;
  };

  template<typename T> concept CmpCat = true;
  struct D {
    // expected-error@+1 {{deduced return type for defaulted three-way comparison operator must be 'auto', not 'CmpCat auto'}}
    friend CmpCat auto operator<=>(const D&, const D&) = default;
  };
}

namespace PR48856 {
  struct A {
    auto operator<=>(const A &) const = default; // expected-warning {{implicitly deleted}}
    void (*x)();                                 // expected-note {{because there is no viable three-way comparison function for member 'x'}}
  };

  struct B {
    auto operator<=>(const B &) const = default; // expected-warning {{implicitly deleted}}
    void (B::*x)();                              // expected-note {{because there is no viable three-way comparison function for member 'x'}}
  };

  struct C {
    auto operator<=>(const C &) const = default; // expected-warning {{implicitly deleted}}
    int C::*x;                                   // expected-note {{because there is no viable three-way comparison function for member 'x'}}
  };
}

namespace PR50591 {
  struct a1 {
    operator int() const;
  };
  struct b1 {
    auto operator<=>(b1 const &) const = default;
    a1 f;
  };
  std::strong_ordering cmp_b1 = b1() <=> b1();

  struct a2 {
    operator float() const;
  };
  struct b2 {
    auto operator<=>(b2 const &) const = default;
    a2 f;
  };
  std::partial_ordering cmp_b2 = b2() <=> b2();

  using fp = void (*)();

  struct a3 {
    operator fp() const;
  };
  struct b3 {
    auto operator<=>(b3 const &) const = default; // expected-warning {{implicitly deleted}}
    a3 f;                                         // expected-note {{because there is no viable three-way comparison function}}
  };

  struct a4 { // Test that function pointer conversion operator here is ignored for this overload resolution.
    operator int() const;
    operator fp() const;
  };
  struct b4 {
    auto operator<=>(b4 const &) const = default;
    a4 f;
  };
  std::strong_ordering cmp_b4 = b4() <=> b4();
}