Fix hash compilation issue for gsl::not_null with shared_ptr

Co-authored-by: carsonRadtke <10507970+carsonRadtke@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-06-02 19:54:22 +00:00
parent f883a665ee
commit 94759d5250
2 changed files with 15 additions and 1 deletions

View File

@@ -231,7 +231,7 @@ template <class T>
not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete; not_null<T> operator+(std::ptrdiff_t, const not_null<T>&) = delete;
template <class T, class U = decltype(std::declval<const T&>().get()), bool = std::is_default_constructible<std::hash<U>>::value> template <class T, class U = typename T::element_type, bool = std::is_default_constructible<std::hash<U>>::value>
struct not_null_hash struct not_null_hash
{ {
std::size_t operator()(const T& value) const { return std::hash<U>{}(value.get()); } std::size_t operator()(const T& value) const { return std::hash<U>{}(value.get()); }

View File

@@ -735,4 +735,18 @@ TEST(notnull_tests, TestStdHash)
EXPECT_FALSE(hash_nn(nn) == hash_intptr(&y)); EXPECT_FALSE(hash_nn(nn) == hash_intptr(&y));
EXPECT_FALSE(hash_nn(nn) == hash_intptr(nullptr)); EXPECT_FALSE(hash_nn(nn) == hash_intptr(nullptr));
} }
{
auto x = std::make_shared<int>(42);
auto y = std::make_shared<int>(99);
not_null<std::shared_ptr<int>> nn{x};
const not_null<std::shared_ptr<int>> cnn{x};
std::hash<not_null<std::shared_ptr<int>>> hash_nn;
std::hash<std::shared_ptr<int>> hash_sharedptr;
EXPECT_TRUE(hash_nn(nn) == hash_sharedptr(x));
EXPECT_FALSE(hash_nn(nn) == hash_sharedptr(y));
EXPECT_TRUE(hash_nn(cnn) == hash_sharedptr(x));
}
} }