File: cxx2a-three-way-comparison.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (60 lines) | stat: -rw-r--r-- 2,509 bytes parent folder | download | duplicates (17)
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 -Wzero-as-null-pointer-constant

// Keep this test before any declarations of operator<=>.
namespace PR44786 {
  template<typename T> void f(decltype(T{} <=> T{})) {} // expected-note {{previous}}

  struct S {};
  int operator<=>(S const &, S const &);
  template<typename T> void f(decltype(T{} <=> T{})) {} // expected-error {{redefinition}}
}

struct A {};
constexpr int operator<=>(A a, A b) { return 42; }
static_assert(operator<=>(A(), A()) == 42);

int operator<=>(); // expected-error {{overloaded 'operator<=>' must have at least one parameter of class or enumeration type}}
int operator<=>(A); // expected-error {{overloaded 'operator<=>' must be a binary operator}}
int operator<=>(int, int); // expected-error {{overloaded 'operator<=>' must have at least one parameter of class or enumeration type}}
int operator<=>(A, A, A); // expected-error {{overloaded 'operator<=>' must be a binary operator}}
int operator<=>(A, A, ...); // expected-error {{overloaded 'operator<=>' cannot be variadic}}
int operator<=>(int, A = {}); // expected-error {{parameter of overloaded 'operator<=>' cannot have a default argument}}

struct B {
  int &operator<=>(int);
  friend int operator<=>(A, B);

  friend int operator<=>(int, int); // expected-error {{overloaded 'operator<=>' must have at least one parameter of class or enumeration type}}
  void operator<=>(); // expected-error {{overloaded 'operator<=>' must be a binary operator}};
  void operator<=>(A, ...); // expected-error {{overloaded 'operator<=>' cannot be variadic}}
  void operator<=>(A, A); // expected-error {{overloaded 'operator<=>' must be a binary operator}};
};

int &r = B().operator<=>(0);

namespace PR47893 {
  struct A {
    void operator<=>(const A&) const;
  };
  template<typename T> auto f(T a, T b) -> decltype(a < b) = delete;
  int &f(...);
  int &r = f(A(), A());
}

namespace PR44325 {
  struct cmp_cat {};
  bool operator<(cmp_cat, void*);
  bool operator>(cmp_cat, int cmp_cat::*);

  struct X {};
  cmp_cat operator<=>(X, X);

  bool b1 = X() < X(); // no warning
  bool b2 = X() > X(); // no warning

  // FIXME: It's not clear whether warning here is useful, but we can't really
  // tell that this is a comparison category in general. This is probably OK,
  // as comparisons against zero are only really intended for use in the
  // implicit rewrite rules, not for explicit use by programs.
  bool c = cmp_cat() < 0; // expected-warning {{zero as null pointer constant}}
}