68 lines · cpp
1// RUN: %clang_cc1 %s -std=c++23 -verify -Wfloat-equal2 3#include "Inputs/std-compare.h"4 5struct 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.13bool 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.16bool 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.19bool 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 21namespace GH102588 {22struct A {23 int i = 0;24 constexpr operator int() const { return i; }25 constexpr operator int&() { return ++i; }26};27 28struct B : A {29 bool operator==(const B &) const = default;30};31 32constexpr bool f() {33 B x;34 return x == x;35}36 37static_assert(f());38 39struct 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 46struct 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 53struct 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 rejected61struct 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}68