From 7d143c98f7f45a258859ab5e27e310ce6ea52620 Mon Sep 17 00:00:00 2001 From: gnusi Date: Sat, 24 May 2025 21:53:59 +0200 Subject: [PATCH] Fix usage of flat_hash_map with move only keys --- absl/container/internal/container_memory.h | 30 +++++++++++----------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/absl/container/internal/container_memory.h b/absl/container/internal/container_memory.h index ed7b90b169a..8f51612851c 100644 --- a/absl/container/internal/container_memory.h +++ b/absl/container/internal/container_memory.h @@ -389,7 +389,7 @@ struct map_slot_policy { template static void construct(Allocator* alloc, slot_type* slot, Args&&... args) { emplace(slot); - if (kMutableKeys::value) { + if constexpr (kMutableKeys::value) { absl::allocator_traits::construct(*alloc, &slot->mutable_value, std::forward(args)...); } else { @@ -402,7 +402,7 @@ struct map_slot_policy { template static void construct(Allocator* alloc, slot_type* slot, slot_type* other) { emplace(slot); - if (kMutableKeys::value) { + if constexpr (kMutableKeys::value) { absl::allocator_traits::construct( *alloc, &slot->mutable_value, std::move(other->mutable_value)); } else { @@ -422,7 +422,7 @@ struct map_slot_policy { template static auto destroy(Allocator* alloc, slot_type* slot) { - if (kMutableKeys::value) { + if constexpr (kMutableKeys::value) { absl::allocator_traits::destroy(*alloc, &slot->mutable_value); } else { absl::allocator_traits::destroy(*alloc, &slot->value); @@ -438,29 +438,29 @@ struct map_slot_policy { // but std::pair is not trivially copyable in C++23 in some standard // library versions. // See https://github.com/llvm/llvm-project/pull/95444 for instance. - auto is_relocatable = typename std::conjunction< + static constexpr auto kIsRelocatable = typename std::conjunction< absl::is_trivially_relocatable, absl::is_trivially_relocatable>:: type(); emplace(new_slot); - if (is_relocatable) { + if constexpr (kIsRelocatable) { // TODO(b/247130232,b/251814870): remove casts after fixing warnings. std::memcpy(static_cast(std::launder(&new_slot->value)), static_cast(&old_slot->value), sizeof(value_type)); - return is_relocatable; - } - - if (kMutableKeys::value) { - absl::allocator_traits::construct( - *alloc, &new_slot->mutable_value, std::move(old_slot->mutable_value)); } else { - absl::allocator_traits::construct(*alloc, &new_slot->value, - std::move(old_slot->value)); + if constexpr (kMutableKeys::value) { + absl::allocator_traits::construct( + *alloc, &new_slot->mutable_value, + std::move(old_slot->mutable_value)); + } else { + absl::allocator_traits::construct( + *alloc, &new_slot->value, std::move(old_slot->value)); + } + destroy(alloc, old_slot); } - destroy(alloc, old_slot); - return is_relocatable; + return kIsRelocatable; } };