Skip to content

Commit

Permalink
[libcxx] makes comparison operators for std::*_ordering types hidde…
Browse files Browse the repository at this point in the history
…n friends

The standard leaves it up to the implementation to decide whether or not
these operators are hidden friends. There are several (well-documented)
reasons to prefer hidden friends, as well as an argument for improved
readability.

Depends on D100342.

Differential Revision: https://reviews.llvm.org/D101707
  • Loading branch information
cjdb committed May 11, 2021
1 parent 20506fb commit 9eb0969
Showing 1 changed file with 128 additions and 175 deletions.
303 changes: 128 additions & 175 deletions libcxx/include/compare
Expand Up @@ -182,21 +182,51 @@ public:
static const partial_ordering unordered;

// comparisons
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator==(partial_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator< (partial_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator<=(partial_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator> (partial_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator>=(partial_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator< (_CmpUnspecifiedParam, partial_ordering __v) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator<=(_CmpUnspecifiedParam, partial_ordering __v) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator> (_CmpUnspecifiedParam, partial_ordering __v) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator>=(_CmpUnspecifiedParam, partial_ordering __v) noexcept;

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator==(partial_ordering, partial_ordering) noexcept = default;

_LIBCPP_INLINE_VISIBILITY friend constexpr partial_ordering operator<=>(partial_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr partial_ordering operator<=>(_CmpUnspecifiedParam, partial_ordering __v) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator==(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__is_ordered() && __v.__value_ == 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator< (partial_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__is_ordered() && __v.__value_ < 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator<=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__is_ordered() && __v.__value_ <= 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator> (partial_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__is_ordered() && __v.__value_ > 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator>=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__is_ordered() && __v.__value_ >= 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator< (_CmpUnspecifiedParam, partial_ordering __v) noexcept {
return __v.__is_ordered() && 0 < __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator<=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
return __v.__is_ordered() && 0 <= __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator> (_CmpUnspecifiedParam, partial_ordering __v) noexcept {
return __v.__is_ordered() && 0 > __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator>=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
return __v.__is_ordered() && 0 >= __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr partial_ordering operator<=>(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr partial_ordering operator<=>(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
return __v < 0 ? partial_ordering::greater : (__v > 0 ? partial_ordering::less : __v);
}
private:
_ValueT __value_;
};
Expand All @@ -206,53 +236,6 @@ _LIBCPP_INLINE_VAR constexpr partial_ordering partial_ordering::equivalent(_EqRe
_LIBCPP_INLINE_VAR constexpr partial_ordering partial_ordering::greater(_OrdResult::__greater);
_LIBCPP_INLINE_VAR constexpr partial_ordering partial_ordering::unordered(_NCmpResult ::__unordered);

_LIBCPP_INLINE_VISIBILITY
constexpr bool operator==(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__is_ordered() && __v.__value_ == 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator< (partial_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__is_ordered() && __v.__value_ < 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator<=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__is_ordered() && __v.__value_ <= 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator> (partial_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__is_ordered() && __v.__value_ > 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator>=(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__is_ordered() && __v.__value_ >= 0;
}

_LIBCPP_INLINE_VISIBILITY
constexpr bool operator< (_CmpUnspecifiedParam, partial_ordering __v) noexcept {
return __v.__is_ordered() && 0 < __v.__value_;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator<=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
return __v.__is_ordered() && 0 <= __v.__value_;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator> (_CmpUnspecifiedParam, partial_ordering __v) noexcept {
return __v.__is_ordered() && 0 > __v.__value_;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator>=(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
return __v.__is_ordered() && 0 >= __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY
constexpr partial_ordering operator<=>(partial_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v;
}
_LIBCPP_INLINE_VISIBILITY
constexpr partial_ordering operator<=>(_CmpUnspecifiedParam, partial_ordering __v) noexcept {
return __v < 0 ? partial_ordering::greater : (__v > 0 ? partial_ordering::less : __v);
}

class weak_ordering {
using _ValueT = signed char;

Expand All @@ -273,20 +256,51 @@ public:
}

// comparisons
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator==(weak_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator< (weak_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator<=(weak_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator> (weak_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator>=(weak_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator< (_CmpUnspecifiedParam, weak_ordering __v) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator<=(_CmpUnspecifiedParam, weak_ordering __v) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator> (_CmpUnspecifiedParam, weak_ordering __v) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator>=(_CmpUnspecifiedParam, weak_ordering __v) noexcept;

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator==(weak_ordering, weak_ordering) noexcept = default;

_LIBCPP_INLINE_VISIBILITY friend constexpr weak_ordering operator<=>(weak_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr weak_ordering operator<=>(_CmpUnspecifiedParam, weak_ordering __v) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator==(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ == 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator< (weak_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ < 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator<=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ <= 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator> (weak_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ > 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator>=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ >= 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator< (_CmpUnspecifiedParam, weak_ordering __v) noexcept {
return 0 < __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator<=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
return 0 <= __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator> (_CmpUnspecifiedParam, weak_ordering __v) noexcept {
return 0 > __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator>=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
return 0 >= __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr weak_ordering operator<=>(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr weak_ordering operator<=>(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
return __v < 0 ? weak_ordering::greater : (__v > 0 ? weak_ordering::less : __v);
}

private:
_ValueT __value_;
Expand All @@ -295,52 +309,6 @@ private:
_LIBCPP_INLINE_VAR constexpr weak_ordering weak_ordering::less(_OrdResult::__less);
_LIBCPP_INLINE_VAR constexpr weak_ordering weak_ordering::equivalent(_EqResult::__equiv);
_LIBCPP_INLINE_VAR constexpr weak_ordering weak_ordering::greater(_OrdResult::__greater);

_LIBCPP_INLINE_VISIBILITY
constexpr bool operator==(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ == 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator< (weak_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ < 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator<=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ <= 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator> (weak_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ > 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator>=(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ >= 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator< (_CmpUnspecifiedParam, weak_ordering __v) noexcept {
return 0 < __v.__value_;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator<=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
return 0 <= __v.__value_;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator> (_CmpUnspecifiedParam, weak_ordering __v) noexcept {
return 0 > __v.__value_;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator>=(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
return 0 >= __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY
constexpr weak_ordering operator<=>(weak_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v;
}
_LIBCPP_INLINE_VISIBILITY
constexpr weak_ordering operator<=>(_CmpUnspecifiedParam, weak_ordering __v) noexcept {
return __v < 0 ? weak_ordering::greater : (__v > 0 ? weak_ordering::less : __v);
}
class strong_ordering {
using _ValueT = signed char;

Expand Down Expand Up @@ -369,20 +337,51 @@ public:
}

// comparisons
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator==(strong_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator< (strong_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator<=(strong_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator> (strong_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator>=(strong_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator< (_CmpUnspecifiedParam, strong_ordering __v) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator<=(_CmpUnspecifiedParam, strong_ordering __v) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator> (_CmpUnspecifiedParam, strong_ordering __v) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator>=(_CmpUnspecifiedParam, strong_ordering __v) noexcept;

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator==(strong_ordering, strong_ordering) noexcept = default;

_LIBCPP_INLINE_VISIBILITY friend constexpr strong_ordering operator<=>(strong_ordering __v, _CmpUnspecifiedParam) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr strong_ordering operator<=>(_CmpUnspecifiedParam, strong_ordering __v) noexcept;
_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator==(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ == 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator< (strong_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ < 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator<=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ <= 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator> (strong_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ > 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator>=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ >= 0;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator< (_CmpUnspecifiedParam, strong_ordering __v) noexcept {
return 0 < __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator<=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
return 0 <= __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator> (_CmpUnspecifiedParam, strong_ordering __v) noexcept {
return 0 > __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr bool operator>=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
return 0 >= __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr strong_ordering operator<=>(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v;
}

_LIBCPP_INLINE_VISIBILITY friend constexpr strong_ordering operator<=>(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
return __v < 0 ? strong_ordering::greater : (__v > 0 ? strong_ordering::less : __v);
}

private:
_ValueT __value_;
Expand All @@ -393,52 +392,6 @@ _LIBCPP_INLINE_VAR constexpr strong_ordering strong_ordering::equal(_EqResult::_
_LIBCPP_INLINE_VAR constexpr strong_ordering strong_ordering::equivalent(_EqResult::__equiv);
_LIBCPP_INLINE_VAR constexpr strong_ordering strong_ordering::greater(_OrdResult::__greater);

_LIBCPP_INLINE_VISIBILITY
constexpr bool operator==(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ == 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator< (strong_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ < 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator<=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ <= 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator> (strong_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ > 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator>=(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v.__value_ >= 0;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator< (_CmpUnspecifiedParam, strong_ordering __v) noexcept {
return 0 < __v.__value_;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator<=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
return 0 <= __v.__value_;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator> (_CmpUnspecifiedParam, strong_ordering __v) noexcept {
return 0 > __v.__value_;
}
_LIBCPP_INLINE_VISIBILITY
constexpr bool operator>=(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
return 0 >= __v.__value_;
}

_LIBCPP_INLINE_VISIBILITY
constexpr strong_ordering operator<=>(strong_ordering __v, _CmpUnspecifiedParam) noexcept {
return __v;
}
_LIBCPP_INLINE_VISIBILITY
constexpr strong_ordering operator<=>(_CmpUnspecifiedParam, strong_ordering __v) noexcept {
return __v < 0 ? strong_ordering::greater : (__v > 0 ? strong_ordering::less : __v);
}

// named comparison functions
_LIBCPP_INLINE_VISIBILITY
constexpr bool is_lt(partial_ordering __cmp) noexcept { return __cmp < 0; }
Expand Down

0 comments on commit 9eb0969

Please sign in to comment.