[libcxx-commits] [libcxx] 9eb0969 - [libcxx] makes comparison operators for `std::*_ordering` types hidden friends

Christopher Di Bella via libcxx-commits libcxx-commits at lists.llvm.org
Mon May 10 23:42:24 PDT 2021


Author: Christopher Di Bella
Date: 2021-05-11T06:41:45Z
New Revision: 9eb0969a767bdc8ed5b28dbcc51b46c2ee088256

URL: https://github.com/llvm/llvm-project/commit/9eb0969a767bdc8ed5b28dbcc51b46c2ee088256
DIFF: https://github.com/llvm/llvm-project/commit/9eb0969a767bdc8ed5b28dbcc51b46c2ee088256.diff

LOG: [libcxx] makes comparison operators for `std::*_ordering` types hidden 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

Added: 
    

Modified: 
    libcxx/include/compare

Removed: 
    


################################################################################
diff  --git a/libcxx/include/compare b/libcxx/include/compare
index 0a2b3d05a1ab..493369045a77 100644
--- a/libcxx/include/compare
+++ b/libcxx/include/compare
@@ -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_;
 };
@@ -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;
 
@@ -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_;
@@ -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;
 
@@ -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_;
@@ -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; }


        


More information about the libcxx-commits mailing list