Skip to content

Commit b40953d

Browse files
Abseil Teamcopybara-github
authored andcommitted
Enable operator== for StatusOr only if the contained type is equality-comparable
PiperOrigin-RevId: 774693039 Change-Id: I915ce87aa37094d1596618cf2604d0bd98583218
1 parent f60bfd8 commit b40953d

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

absl/status/internal/statusor_internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ template <typename T, typename U>
4646
struct HasConversionOperatorToStatusOr<T, U, decltype(test<T, U>(0))>
4747
: std::true_type {};
4848

49+
// Detects whether `T` is equality-comparable.
50+
template <typename T, typename = void>
51+
struct IsEqualityComparable : std::false_type {};
52+
53+
template <typename T>
54+
struct IsEqualityComparable<
55+
T, std::enable_if_t<std::is_convertible<
56+
decltype(std::declval<T>() == std::declval<T>()),
57+
bool>::value>> : std::true_type {};
58+
4959
// Detects whether `T` is constructible or convertible from `StatusOr<U>`.
5060
template <typename T, typename U>
5161
using IsConstructibleOrConvertibleFromStatusOr =

absl/status/statusor.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,9 @@ class StatusOr : private internal_statusor::StatusOrData<T>,
607607
// operator==()
608608
//
609609
// This operator checks the equality of two `absl::StatusOr<T>` objects.
610-
template <typename T>
610+
template <typename T,
611+
std::enable_if_t<internal_statusor::IsEqualityComparable<T>::value,
612+
int> = 0>
611613
bool operator==(const StatusOr<T>& lhs, const StatusOr<T>& rhs) {
612614
if (lhs.ok() && rhs.ok()) return *lhs == *rhs;
613615
return lhs.status() == rhs.status();
@@ -616,7 +618,9 @@ bool operator==(const StatusOr<T>& lhs, const StatusOr<T>& rhs) {
616618
// operator!=()
617619
//
618620
// This operator checks the inequality of two `absl::StatusOr<T>` objects.
619-
template <typename T>
621+
template <typename T,
622+
std::enable_if_t<internal_statusor::IsEqualityComparable<T>::value,
623+
int> = 0>
620624
bool operator!=(const StatusOr<T>& lhs, const StatusOr<T>& rhs) {
621625
return !(lhs == rhs);
622626
}

0 commit comments

Comments
 (0)