forked from microsoft/GSL
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:
@@ -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()); }
|
||||||
|
@@ -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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user