diff --git a/include/gsl/pointers b/include/gsl/pointers index 5e69f58..2a032ab 100644 --- a/include/gsl/pointers +++ b/include/gsl/pointers @@ -231,7 +231,7 @@ template not_null operator+(std::ptrdiff_t, const not_null&) = delete; -template ().get()), bool = std::is_default_constructible>::value> +template >::value> struct not_null_hash { std::size_t operator()(const T& value) const { return std::hash{}(value.get()); } diff --git a/tests/notnull_tests.cpp b/tests/notnull_tests.cpp index 56b549b..4fa48b0 100644 --- a/tests/notnull_tests.cpp +++ b/tests/notnull_tests.cpp @@ -735,4 +735,18 @@ TEST(notnull_tests, TestStdHash) EXPECT_FALSE(hash_nn(nn) == hash_intptr(&y)); EXPECT_FALSE(hash_nn(nn) == hash_intptr(nullptr)); } + + { + auto x = std::make_shared(42); + auto y = std::make_shared(99); + not_null> nn{x}; + const not_null> cnn{x}; + + std::hash>> hash_nn; + std::hash> 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)); + } }