| 1 |
// RUN: %clang_cc1 %s -std=c++23 -verify -Wfloat-equal |
| 2 |
|
| 3 |
#include "Inputs/std-compare.h" |
| 4 |
|
| 5 |
struct Foo { |
| 6 |
float val; |
| 7 |
bool operator==(const Foo &) const; |
| 8 |
friend bool operator==(const Foo &, const Foo &); |
| 9 |
friend bool operator==(Foo, Foo ); |
| 10 |
}; |
| 11 |
|
| 12 |
// Declare the defaulted comparison function as a member function. |
| 13 |
bool Foo::operator==(const Foo &) const = default; // expected-warning {{comparing floating point with == or != is unsafe}} expected-note {{in defaulted equality comparison operator for 'Foo' first required here}} |
| 14 |
|
| 15 |
// Declare the defaulted comparison function as a non-member function. |
| 16 |
bool operator==(const Foo &, const Foo &) = default; // expected-warning {{comparing floating point with == or != is unsafe}} expected-note {{in defaulted equality comparison operator for 'Foo' first required here}} |
| 17 |
|
| 18 |
// Declare the defaulted comparison function as a non-member function. Arguments are passed by value. |
| 19 |
bool operator==(Foo, Foo) = default; // expected-warning {{comparing floating point with == or != is unsafe}} expected-note {{in defaulted equality comparison operator for 'Foo' first required here}} |
| 20 |
|
| 21 |
namespace GH102588 { |
| 22 |
struct A { |
| 23 |
int i = 0; |
| 24 |
constexpr operator int() const { return i; } |
| 25 |
constexpr operator int&() { return ++i; } |
| 26 |
}; |
| 27 |
|
| 28 |
struct B : A { |
| 29 |
bool operator==(const B &) const = default; |
| 30 |
}; |
| 31 |
|
| 32 |
constexpr bool f() { |
| 33 |
B x; |
| 34 |
return x == x; |
| 35 |
} |
| 36 |
|
| 37 |
static_assert(f()); |
| 38 |
|
| 39 |
struct ConstOnly { |
| 40 |
std::strong_ordering operator<=>(const ConstOnly&) const; |
| 41 |
std::strong_ordering operator<=>(ConstOnly&) = delete; |
| 42 |
friend bool operator==(const ConstOnly&, const ConstOnly&); |
| 43 |
friend bool operator==(ConstOnly&, ConstOnly&) = delete; |
| 44 |
}; |
| 45 |
|
| 46 |
struct MutOnly { |
| 47 |
std::strong_ordering operator<=>(const MutOnly&) const = delete;; |
| 48 |
std::strong_ordering operator<=>(MutOnly&); |
| 49 |
friend bool operator==(const MutOnly&, const MutOnly&) = delete;; |
| 50 |
friend bool operator==(MutOnly&, MutOnly&); |
| 51 |
}; |
| 52 |
|
| 53 |
struct ConstCheck : ConstOnly { |
| 54 |
friend std::strong_ordering operator<=>(const ConstCheck&, const ConstCheck&) = default; |
| 55 |
std::strong_ordering operator<=>(ConstCheck const& __restrict) const __restrict = default; |
| 56 |
friend bool operator==(const ConstCheck&, const ConstCheck&) = default; |
| 57 |
bool operator==(this const ConstCheck&, const ConstCheck&) = default; |
| 58 |
}; |
| 59 |
|
| 60 |
// FIXME: Non-reference explicit object parameter are rejected |
| 61 |
struct MutCheck : MutOnly { |
| 62 |
friend bool operator==(MutCheck, MutCheck) = default; |
| 63 |
// std::strong_ordering operator<=>(this MutCheck, MutCheck) = default; |
| 64 |
friend std::strong_ordering operator<=>(MutCheck, MutCheck) = default; |
| 65 |
// bool operator==(this MutCheck, MutCheck) = default; |
| 66 |
}; |
| 67 |
} |