summaryrefslogtreecommitdiff
path: root/test/CXX/over/over.match/over.match.funcs/over.match.oper/p8-2a.cpp
blob: da0f576f8c98864311dbec449d5d86cf4e5e4704 (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
// RUN: %clang_cc1 -std=c++2a -verify %s

template<typename L, typename R> struct Op { L l; const char *op; R r; };
// FIXME: Remove once we implement P1816R0.
template<typename L, typename R> Op(L, R) -> Op<L, R>;

struct A {};
struct B {};
constexpr Op<A, B> operator<=>(A a, B b) { return {a, "<=>", b}; }

template<typename T, typename U, typename V> constexpr Op<Op<T, U>, V> operator<  (Op<T, U> a, V b) { return {a, "<",   b}; }
template<typename T, typename U, typename V> constexpr Op<Op<T, U>, V> operator<= (Op<T, U> a, V b) { return {a, "<=",  b}; }
template<typename T, typename U, typename V> constexpr Op<Op<T, U>, V> operator>  (Op<T, U> a, V b) { return {a, ">",   b}; }
template<typename T, typename U, typename V> constexpr Op<Op<T, U>, V> operator>= (Op<T, U> a, V b) { return {a, ">=",  b}; }
template<typename T, typename U, typename V> constexpr Op<Op<T, U>, V> operator<=>(Op<T, U> a, V b) { return {a, "<=>", b}; }

template<typename T, typename U, typename V> constexpr Op<T, Op<U, V>> operator<  (T a, Op<U, V> b) { return {a, "<",   b}; }
template<typename T, typename U, typename V> constexpr Op<T, Op<U, V>> operator<= (T a, Op<U, V> b) { return {a, "<=",  b}; }
template<typename T, typename U, typename V> constexpr Op<T, Op<U, V>> operator>  (T a, Op<U, V> b) { return {a, ">",   b}; }
template<typename T, typename U, typename V> constexpr Op<T, Op<U, V>> operator>= (T a, Op<U, V> b) { return {a, ">=",  b}; }
template<typename T, typename U, typename V> constexpr Op<T, Op<U, V>> operator<=>(T a, Op<U, V> b) { return {a, "<=>", b}; }

constexpr bool same(A, A) { return true; }
constexpr bool same(B, B) { return true; }
constexpr bool same(int a, int b) { return a == b; }
template<typename T, typename U>
constexpr bool same(Op<T, U> x, Op<T, U> y) {
  return same(x.l, y.l) && __builtin_strcmp(x.op, y.op) == 0 && same(x.r, y.r);
}

// x @ y is interpreted as:
void f(A x, B y) {
  //   --  (x <=> y) @ 0 if not reversed
  static_assert(same(x < y, (x <=> y) < 0));
  static_assert(same(x <= y, (x <=> y) <= 0));
  static_assert(same(x > y, (x <=> y) > 0));
  static_assert(same(x >= y, (x <=> y) >= 0));
  static_assert(same(x <=> y, x <=> y)); // (not rewritten)
}

void g(B x, A y) {
  //   --  0 @ (y <=> x) if reversed
  static_assert(same(x < y, 0 < (y <=> x)));
  static_assert(same(x <= y, 0 <= (y <=> x)));
  static_assert(same(x > y, 0 > (y <=> x)));
  static_assert(same(x >= y, 0 >= (y <=> x)));
  static_assert(same(x <=> y, 0 <=> (y <=> x)));
}


// We can rewrite into a call involving a builtin operator.
struct X { int result; };
struct Y {};
constexpr int operator<=>(X x, Y) { return x.result; }
static_assert(X{-1} < Y{});
static_assert(X{0} < Y{}); // expected-error {{failed}}
static_assert(X{0} <= Y{});
static_assert(X{1} <= Y{}); // expected-error {{failed}}
static_assert(X{1} > Y{});
static_assert(X{0} > Y{}); // expected-error {{failed}}
static_assert(X{0} >= Y{});
static_assert(X{-1} >= Y{}); // expected-error {{failed}}
static_assert(Y{} < X{1});
static_assert(Y{} < X{0}); // expected-error {{failed}}
static_assert(Y{} <= X{0});
static_assert(Y{} <= X{-1}); // expected-error {{failed}}
static_assert(Y{} > X{-1});
static_assert(Y{} > X{0}); // expected-error {{failed}}
static_assert(Y{} >= X{0});
static_assert(Y{} >= X{1}); // expected-error {{failed}}