diff options
author | Abseil Team <absl-team@google.com> | 2020-05-15 08:23:25 -0700 |
---|---|---|
committer | Andy Getz <durandal@google.com> | 2020-05-15 17:57:15 -0400 |
commit | 62cf6a70414e15b356ec4f712ca8758133674f28 (patch) | |
tree | d517fe8c1bb80458f8dda3e7b77ff7f32cf436d8 /absl/hash | |
parent | d118d4bb115387cc52c5338221f2554a940e6d90 (diff) |
Export of internal Abseil changes
--
dc36565216b3022abb5a3c92f46fb4079bda869e by Samuel Benzaquen <sbenza@google.com>:
Add support for std::reference_wrapper to absl::Hash.
PiperOrigin-RevId: 311733171
GitOrigin-RevId: dc36565216b3022abb5a3c92f46fb4079bda869e
Change-Id: I74543390446c2b7f6d856935aa36f28539810eef
Diffstat (limited to 'absl/hash')
-rw-r--r-- | absl/hash/hash_test.cc | 18 | ||||
-rw-r--r-- | absl/hash/internal/hash.h | 7 |
2 files changed, 25 insertions, 0 deletions
diff --git a/absl/hash/hash_test.cc b/absl/hash/hash_test.cc index b38d46d4..39ba24a8 100644 --- a/absl/hash/hash_test.cc +++ b/absl/hash/hash_test.cc @@ -581,6 +581,24 @@ TEST(HashValueTest, Maps) { MM{{1, "foo"}, {1, "foo"}, {43, "bar"}}, MM{{1, "foo"}, {43, "baz"}}))); } +TEST(HashValueTest, ReferenceWrapper) { + EXPECT_TRUE(is_hashable<std::reference_wrapper<Private>>::value); + + Private p1{1}, p10{10}; + EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple( + p1, p10, std::ref(p1), std::ref(p10), std::cref(p1), std::cref(p10)))); + + EXPECT_TRUE(is_hashable<std::reference_wrapper<int>>::value); + int one = 1, ten = 10; + EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly(std::make_tuple( + one, ten, std::ref(one), std::ref(ten), std::cref(one), std::cref(ten)))); + + EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly( + std::make_tuple(std::tuple<std::reference_wrapper<int>>(std::ref(one)), + std::tuple<std::reference_wrapper<int>>(std::ref(ten)), + std::tuple<int>(one), std::tuple<int>(ten)))); +} + template <typename T, typename = void> struct IsHashCallable : std::false_type {}; diff --git a/absl/hash/internal/hash.h b/absl/hash/internal/hash.h index a71bd4a6..9e608f7c 100644 --- a/absl/hash/internal/hash.h +++ b/absl/hash/internal/hash.h @@ -551,6 +551,13 @@ typename std::enable_if<is_hashable<Key>::value, H>::type AbslHashValue( // AbslHashValue for Wrapper Types // ----------------------------------------------------------------------------- +// AbslHashValue for hashing std::reference_wrapper +template <typename H, typename T> +typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue( + H hash_state, std::reference_wrapper<T> opt) { + return H::combine(std::move(hash_state), opt.get()); +} + // AbslHashValue for hashing absl::optional template <typename H, typename T> typename std::enable_if<is_hashable<T>::value, H>::type AbslHashValue( |