diff --git a/src/google/protobuf/BUILD.bazel b/src/google/protobuf/BUILD.bazel index 79c31cdb7489..e2b97f3147ea 100644 --- a/src/google/protobuf/BUILD.bazel +++ b/src/google/protobuf/BUILD.bazel @@ -337,7 +337,6 @@ cc_library( "//src/google/protobuf/stubs:lite", "@com_google_absl//absl/container:btree", "@com_google_absl//absl/container:flat_hash_set", - "@com_google_absl//absl/hash", "@com_google_absl//absl/meta:type_traits", "@com_google_absl//absl/numeric:bits", "@com_google_absl//absl/strings:internal", diff --git a/src/google/protobuf/map.h b/src/google/protobuf/map.h index da61c2b5cd54..78ad72c4dd9f 100644 --- a/src/google/protobuf/map.h +++ b/src/google/protobuf/map.h @@ -46,15 +46,17 @@ #include #include +#if defined(__cpp_lib_string_view) +#include +#endif // defined(__cpp_lib_string_view) + #if !defined(GOOGLE_PROTOBUF_NO_RDTSC) && defined(__APPLE__) #include #endif #include "google/protobuf/stubs/common.h" #include "absl/container/btree_map.h" -#include "absl/hash/hash.h" #include "absl/meta/type_traits.h" -#include "absl/strings/string_view.h" #include "google/protobuf/arena.h" #include "google/protobuf/generated_enum_util.h" #include "google/protobuf/map_type_handler.h" @@ -227,31 +229,33 @@ struct TransparentSupport { using key_arg = key_type; }; -// We add transparent support for std::string keys. We use -// std::hash as it supports the input types we care about. -// The lookup functions accept arbitrary `K`. This will include any key type -// that is convertible to absl::string_view. +#if defined(__cpp_lib_string_view) +// If std::string_view is available, we add transparent support for std::string +// keys. We use std::hash as it supports the input types we +// care about. The lookup functions accept arbitrary `K`. This will include any +// key type that is convertible to std::string_view. template <> struct TransparentSupport { - static absl::string_view ImplicitConvert(absl::string_view str) { - return str; - } - // If the element is not convertible to absl::string_view, try to convert to + static std::string_view ImplicitConvert(std::string_view str) { return str; } + // If the element is not convertible to std::string_view, try to convert to // std::string first. // The template makes this overload lose resolution when both have the same // rank otherwise. template - static absl::string_view ImplicitConvert(const std::string& str) { + static std::string_view ImplicitConvert(const std::string& str) { return str; } - struct hash : public absl::Hash { + struct hash : private std::hash { using is_transparent = void; template size_t operator()(const T& str) const { - return absl::Hash::operator()(ImplicitConvert(str)); + return base()(ImplicitConvert(str)); } + + private: + const std::hash& base() const { return *this; } }; struct less { using is_transparent = void; @@ -270,6 +274,7 @@ struct TransparentSupport { template using key_arg = K; }; +#endif // defined(__cpp_lib_string_view) struct NodeBase { // Align the node to allow KeyNode to predict the location of the key. diff --git a/src/google/protobuf/map_test.inc b/src/google/protobuf/map_test.inc index 4c11ecb6f622..6d1a70cd2b88 100644 --- a/src/google/protobuf/map_test.inc +++ b/src/google/protobuf/map_test.inc @@ -1404,7 +1404,9 @@ void TestTransparent(const Key& key, const Key& miss_key) { TEST_F(MapImplTest, TransparentLookupForString) { TestTransparent("ABC", "LKJ"); TestTransparent(std::string("ABC"), std::string("LKJ")); - TestTransparent(absl::string_view("ABC"), absl::string_view("LKJ")); +#if defined(__cpp_lib_string_view) + TestTransparent(std::string_view("ABC"), std::string_view("LKJ")); +#endif // defined(__cpp_lib_string_view) // std::reference_wrapper std::string abc = "ABC", lkj = "LKJ";