Commit a39d71a8 by Marcin Kowalczyk Committed by Copybara-Service

Remove `absl::weak_equality` and `absl::strong_equality`.

The corresponding `std` types were removed before C++20 was finalized:
https://wg21.link/P1959R0.

They are unused. The language mechanisms and recommendations changed since they
were originally proposed. In particular:

* An explicitly defaulted `operator==` is defined in terms of memberwise
  `operator==` rather than sibling `operator<=>`.

* An `operator!=` can be implicitly added to an overload set in terms of
  `operator==` rather than `operator<=>`.

* A class which has equality but not ordering defined should provide
  `operator==` rather than `operator<=>`.

PiperOrigin-RevId: 587834267
Change-Id: I2c2513c13f3485b9edc6a345dca4a577d8e65167
parent 8028a87c
......@@ -16,15 +16,15 @@
// compare.h
// -----------------------------------------------------------------------------
//
// This header file defines the `absl::weak_equality`, `absl::strong_equality`,
// `absl::partial_ordering`, `absl::weak_ordering`, and `absl::strong_ordering`
// types for storing the results of three way comparisons.
// This header file defines the `absl::partial_ordering`, `absl::weak_ordering`,
// and `absl::strong_ordering` types for storing the results of three way
// comparisons.
//
// Example:
// absl::weak_ordering compare(const std::string& a, const std::string& b);
//
// These are C++11 compatible versions of the C++20 corresponding types
// (`std::weak_equality`, etc.) and are designed to be drop-in replacements
// (`std::partial_ordering`, etc.) and are designed to be drop-in replacements
// for code compliant with C++20.
#ifndef ABSL_TYPES_COMPARE_H_
......@@ -122,20 +122,6 @@ enum class ncmp : value_type { unordered = -127 };
// in the header file (for performance) without using inline variables (which
// aren't available in C++11).
template <typename T>
struct weak_equality_base {
ABSL_COMPARE_INLINE_BASECLASS_DECL(equivalent);
ABSL_COMPARE_INLINE_BASECLASS_DECL(nonequivalent);
};
template <typename T>
struct strong_equality_base {
ABSL_COMPARE_INLINE_BASECLASS_DECL(equal);
ABSL_COMPARE_INLINE_BASECLASS_DECL(nonequal);
ABSL_COMPARE_INLINE_BASECLASS_DECL(equivalent);
ABSL_COMPARE_INLINE_BASECLASS_DECL(nonequivalent);
};
template <typename T>
struct partial_ordering_base {
ABSL_COMPARE_INLINE_BASECLASS_DECL(less);
ABSL_COMPARE_INLINE_BASECLASS_DECL(equivalent);
......@@ -160,104 +146,6 @@ struct strong_ordering_base {
} // namespace compare_internal
class weak_equality
: public compare_internal::weak_equality_base<weak_equality> {
explicit constexpr weak_equality(compare_internal::eq v) noexcept
: value_(static_cast<compare_internal::value_type>(v)) {}
friend struct compare_internal::weak_equality_base<weak_equality>;
public:
ABSL_COMPARE_INLINE_SUBCLASS_DECL(weak_equality, equivalent);
ABSL_COMPARE_INLINE_SUBCLASS_DECL(weak_equality, nonequivalent);
// Comparisons
friend constexpr bool operator==(
weak_equality v, compare_internal::OnlyLiteralZero) noexcept {
return v.value_ == 0;
}
friend constexpr bool operator!=(
weak_equality v, compare_internal::OnlyLiteralZero) noexcept {
return v.value_ != 0;
}
friend constexpr bool operator==(compare_internal::OnlyLiteralZero,
weak_equality v) noexcept {
return 0 == v.value_;
}
friend constexpr bool operator!=(compare_internal::OnlyLiteralZero,
weak_equality v) noexcept {
return 0 != v.value_;
}
friend constexpr bool operator==(weak_equality v1,
weak_equality v2) noexcept {
return v1.value_ == v2.value_;
}
friend constexpr bool operator!=(weak_equality v1,
weak_equality v2) noexcept {
return v1.value_ != v2.value_;
}
private:
compare_internal::value_type value_;
};
ABSL_COMPARE_INLINE_INIT(weak_equality, equivalent,
compare_internal::eq::equivalent);
ABSL_COMPARE_INLINE_INIT(weak_equality, nonequivalent,
compare_internal::eq::nonequivalent);
class strong_equality
: public compare_internal::strong_equality_base<strong_equality> {
explicit constexpr strong_equality(compare_internal::eq v) noexcept
: value_(static_cast<compare_internal::value_type>(v)) {}
friend struct compare_internal::strong_equality_base<strong_equality>;
public:
ABSL_COMPARE_INLINE_SUBCLASS_DECL(strong_equality, equal);
ABSL_COMPARE_INLINE_SUBCLASS_DECL(strong_equality, nonequal);
ABSL_COMPARE_INLINE_SUBCLASS_DECL(strong_equality, equivalent);
ABSL_COMPARE_INLINE_SUBCLASS_DECL(strong_equality, nonequivalent);
// Conversion
constexpr operator weak_equality() const noexcept { // NOLINT
return value_ == 0 ? weak_equality::equivalent
: weak_equality::nonequivalent;
}
// Comparisons
friend constexpr bool operator==(
strong_equality v, compare_internal::OnlyLiteralZero) noexcept {
return v.value_ == 0;
}
friend constexpr bool operator!=(
strong_equality v, compare_internal::OnlyLiteralZero) noexcept {
return v.value_ != 0;
}
friend constexpr bool operator==(compare_internal::OnlyLiteralZero,
strong_equality v) noexcept {
return 0 == v.value_;
}
friend constexpr bool operator!=(compare_internal::OnlyLiteralZero,
strong_equality v) noexcept {
return 0 != v.value_;
}
friend constexpr bool operator==(strong_equality v1,
strong_equality v2) noexcept {
return v1.value_ == v2.value_;
}
friend constexpr bool operator!=(strong_equality v1,
strong_equality v2) noexcept {
return v1.value_ != v2.value_;
}
private:
compare_internal::value_type value_;
};
ABSL_COMPARE_INLINE_INIT(strong_equality, equal, compare_internal::eq::equal);
ABSL_COMPARE_INLINE_INIT(strong_equality, nonequal,
compare_internal::eq::nonequal);
ABSL_COMPARE_INLINE_INIT(strong_equality, equivalent,
compare_internal::eq::equivalent);
ABSL_COMPARE_INLINE_INIT(strong_equality, nonequivalent,
compare_internal::eq::nonequivalent);
class partial_ordering
: public compare_internal::partial_ordering_base<partial_ordering> {
explicit constexpr partial_ordering(compare_internal::eq v) noexcept
......@@ -279,11 +167,6 @@ class partial_ordering
ABSL_COMPARE_INLINE_SUBCLASS_DECL(partial_ordering, greater);
ABSL_COMPARE_INLINE_SUBCLASS_DECL(partial_ordering, unordered);
// Conversion
constexpr operator weak_equality() const noexcept { // NOLINT
return value_ == 0 ? weak_equality::equivalent
: weak_equality::nonequivalent;
}
// Comparisons
friend constexpr bool operator==(
partial_ordering v, compare_internal::OnlyLiteralZero) noexcept {
......@@ -367,10 +250,6 @@ class weak_ordering
ABSL_COMPARE_INLINE_SUBCLASS_DECL(weak_ordering, greater);
// Conversions
constexpr operator weak_equality() const noexcept { // NOLINT
return value_ == 0 ? weak_equality::equivalent
: weak_equality::nonequivalent;
}
constexpr operator partial_ordering() const noexcept { // NOLINT
return value_ == 0 ? partial_ordering::equivalent
: (value_ < 0 ? partial_ordering::less
......@@ -458,13 +337,6 @@ class strong_ordering
ABSL_COMPARE_INLINE_SUBCLASS_DECL(strong_ordering, greater);
// Conversions
constexpr operator weak_equality() const noexcept { // NOLINT
return value_ == 0 ? weak_equality::equivalent
: weak_equality::nonequivalent;
}
constexpr operator strong_equality() const noexcept { // NOLINT
return value_ == 0 ? strong_equality::equal : strong_equality::nonequal;
}
constexpr operator partial_ordering() const noexcept { // NOLINT
return value_ == 0 ? partial_ordering::equivalent
: (value_ < 0 ? partial_ordering::less
......
......@@ -26,45 +26,6 @@ namespace {
// to an int, which can't be converted to the unspecified zero type.
bool Identity(bool b) { return b; }
TEST(Compare, WeakEquality) {
EXPECT_TRUE(Identity(weak_equality::equivalent == 0));
EXPECT_TRUE(Identity(0 == weak_equality::equivalent));
EXPECT_TRUE(Identity(weak_equality::nonequivalent != 0));
EXPECT_TRUE(Identity(0 != weak_equality::nonequivalent));
const weak_equality values[] = {weak_equality::equivalent,
weak_equality::nonequivalent};
for (const auto& lhs : values) {
for (const auto& rhs : values) {
const bool are_equal = &lhs == &rhs;
EXPECT_EQ(lhs == rhs, are_equal);
EXPECT_EQ(lhs != rhs, !are_equal);
}
}
}
TEST(Compare, StrongEquality) {
EXPECT_TRUE(Identity(strong_equality::equal == 0));
EXPECT_TRUE(Identity(0 == strong_equality::equal));
EXPECT_TRUE(Identity(strong_equality::nonequal != 0));
EXPECT_TRUE(Identity(0 != strong_equality::nonequal));
EXPECT_TRUE(Identity(strong_equality::equivalent == 0));
EXPECT_TRUE(Identity(0 == strong_equality::equivalent));
EXPECT_TRUE(Identity(strong_equality::nonequivalent != 0));
EXPECT_TRUE(Identity(0 != strong_equality::nonequivalent));
const strong_equality values[] = {strong_equality::equal,
strong_equality::nonequal};
for (const auto& lhs : values) {
for (const auto& rhs : values) {
const bool are_equal = &lhs == &rhs;
EXPECT_EQ(lhs == rhs, are_equal);
EXPECT_EQ(lhs != rhs, !are_equal);
}
}
EXPECT_TRUE(Identity(strong_equality::equivalent == strong_equality::equal));
EXPECT_TRUE(
Identity(strong_equality::nonequivalent == strong_equality::nonequal));
}
TEST(Compare, PartialOrdering) {
EXPECT_TRUE(Identity(partial_ordering::less < 0));
EXPECT_TRUE(Identity(0 > partial_ordering::less));
......@@ -147,30 +108,6 @@ TEST(Compare, StrongOrdering) {
TEST(Compare, Conversions) {
EXPECT_TRUE(
Identity(implicit_cast<weak_equality>(strong_equality::equal) == 0));
EXPECT_TRUE(
Identity(implicit_cast<weak_equality>(strong_equality::nonequal) != 0));
EXPECT_TRUE(
Identity(implicit_cast<weak_equality>(strong_equality::equivalent) == 0));
EXPECT_TRUE(Identity(
implicit_cast<weak_equality>(strong_equality::nonequivalent) != 0));
EXPECT_TRUE(
Identity(implicit_cast<weak_equality>(partial_ordering::less) != 0));
EXPECT_TRUE(Identity(
implicit_cast<weak_equality>(partial_ordering::equivalent) == 0));
EXPECT_TRUE(
Identity(implicit_cast<weak_equality>(partial_ordering::greater) != 0));
EXPECT_TRUE(
Identity(implicit_cast<weak_equality>(partial_ordering::unordered) != 0));
EXPECT_TRUE(implicit_cast<weak_equality>(weak_ordering::less) != 0);
EXPECT_TRUE(
Identity(implicit_cast<weak_equality>(weak_ordering::equivalent) == 0));
EXPECT_TRUE(
Identity(implicit_cast<weak_equality>(weak_ordering::greater) != 0));
EXPECT_TRUE(
Identity(implicit_cast<partial_ordering>(weak_ordering::less) != 0));
EXPECT_TRUE(
Identity(implicit_cast<partial_ordering>(weak_ordering::less) < 0));
......@@ -186,24 +123,6 @@ TEST(Compare, Conversions) {
Identity(implicit_cast<partial_ordering>(weak_ordering::greater) >= 0));
EXPECT_TRUE(
Identity(implicit_cast<weak_equality>(strong_ordering::less) != 0));
EXPECT_TRUE(
Identity(implicit_cast<weak_equality>(strong_ordering::equal) == 0));
EXPECT_TRUE(
Identity(implicit_cast<weak_equality>(strong_ordering::equivalent) == 0));
EXPECT_TRUE(
Identity(implicit_cast<weak_equality>(strong_ordering::greater) != 0));
EXPECT_TRUE(
Identity(implicit_cast<strong_equality>(strong_ordering::less) != 0));
EXPECT_TRUE(
Identity(implicit_cast<strong_equality>(strong_ordering::equal) == 0));
EXPECT_TRUE(Identity(
implicit_cast<strong_equality>(strong_ordering::equivalent) == 0));
EXPECT_TRUE(
Identity(implicit_cast<strong_equality>(strong_ordering::greater) != 0));
EXPECT_TRUE(
Identity(implicit_cast<partial_ordering>(strong_ordering::less) != 0));
EXPECT_TRUE(
Identity(implicit_cast<partial_ordering>(strong_ordering::less) < 0));
......@@ -360,14 +279,6 @@ TEST(DoThreeWayComparison, SanityTest) {
#ifdef __cpp_inline_variables
TEST(Compare, StaticAsserts) {
static_assert(weak_equality::equivalent == 0, "");
static_assert(weak_equality::nonequivalent != 0, "");
static_assert(strong_equality::equal == 0, "");
static_assert(strong_equality::nonequal != 0, "");
static_assert(strong_equality::equivalent == 0, "");
static_assert(strong_equality::nonequivalent != 0, "");
static_assert(partial_ordering::less < 0, "");
static_assert(partial_ordering::equivalent == 0, "");
static_assert(partial_ordering::greater > 0, "");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment