Mark raw_ptr<T> and T* as having a common reference type of T*.
Allows more ranges algorithm use, e.g. finding a T* in a
vector<raw_ptr<T>>.
Bug: 386918226
Change-Id: Ic181cd9b90205e9a3e71abe9eb313eb44e8b1b0c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6135992
Commit-Queue: Benoit Lize <lizeb@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Reviewed-by: Benoit Lize <lizeb@chromium.org>
Auto-Submit: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1403501}
NOKEYCHECK=True
GitOrigin-RevId: 1e13f6dda4d57bc246b33d0ce77eccc889c05305
diff --git a/src/partition_alloc/pointers/raw_ptr.h b/src/partition_alloc/pointers/raw_ptr.h
index 7de8be9..c3ac115 100644
--- a/src/partition_alloc/pointers/raw_ptr.h
+++ b/src/partition_alloc/pointers/raw_ptr.h
@@ -1255,6 +1255,36 @@
}
};
+// Mark `raw_ptr<T>` and `T*` as having a common reference type (the type to
+// which both can be converted or bound) of `T*`. This makes them satisfy
+// `std::equality_comparable`, which allows usage like:
+// ```
+// std::vector<raw_ptr<T>> v;
+// T* e;
+// auto it = std::ranges::find(v, e);
+// ```
+// Without this, the `find()` call above would fail to compile with a cryptic
+// error about being unable to invoke `std::ranges::equal_to()`.
+template <typename T,
+ base::RawPtrTraits Traits,
+ template <typename>
+ typename TQ,
+ template <typename>
+ typename UQ>
+struct std::basic_common_reference<raw_ptr<T, Traits>, T*, TQ, UQ> {
+ using type = T*;
+};
+
+template <typename T,
+ base::RawPtrTraits Traits,
+ template <typename>
+ typename TQ,
+ template <typename>
+ typename UQ>
+struct std::basic_common_reference<T*, raw_ptr<T, Traits>, TQ, UQ> {
+ using type = T*;
+};
+
} // namespace std
#endif // PARTITION_ALLOC_POINTERS_RAW_PTR_H_