Skip to content

Commit e2d31c1

Browse files
committed
Fix usage of flat_hash_map with move only keys
1 parent e4c4385 commit e2d31c1

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

absl/container/internal/container_memory.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ struct map_slot_policy {
389389
template <class Allocator, class... Args>
390390
static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
391391
emplace(slot);
392-
if (kMutableKeys::value) {
392+
if constexpr (kMutableKeys::value) {
393393
absl::allocator_traits<Allocator>::construct(*alloc, &slot->mutable_value,
394394
std::forward<Args>(args)...);
395395
} else {
@@ -402,7 +402,7 @@ struct map_slot_policy {
402402
template <class Allocator>
403403
static void construct(Allocator* alloc, slot_type* slot, slot_type* other) {
404404
emplace(slot);
405-
if (kMutableKeys::value) {
405+
if constexpr (kMutableKeys::value) {
406406
absl::allocator_traits<Allocator>::construct(
407407
*alloc, &slot->mutable_value, std::move(other->mutable_value));
408408
} else {
@@ -422,7 +422,7 @@ struct map_slot_policy {
422422

423423
template <class Allocator>
424424
static auto destroy(Allocator* alloc, slot_type* slot) {
425-
if (kMutableKeys::value) {
425+
if constexpr (kMutableKeys::value) {
426426
absl::allocator_traits<Allocator>::destroy(*alloc, &slot->mutable_value);
427427
} else {
428428
absl::allocator_traits<Allocator>::destroy(*alloc, &slot->value);
@@ -438,29 +438,26 @@ struct map_slot_policy {
438438
// but std::pair is not trivially copyable in C++23 in some standard
439439
// library versions.
440440
// See https://github.com/llvm/llvm-project/pull/95444 for instance.
441-
auto is_relocatable = typename std::conjunction<
441+
static constexpr auto kIsRelocatable = typename std::conjunction<
442442
absl::is_trivially_relocatable<typename value_type::first_type>,
443443
absl::is_trivially_relocatable<typename value_type::second_type>>::
444444
type();
445445

446446
emplace(new_slot);
447-
if (is_relocatable) {
447+
if constexpr (kIsRelocatable) {
448448
// TODO(b/247130232,b/251814870): remove casts after fixing warnings.
449449
std::memcpy(static_cast<void*>(std::launder(&new_slot->value)),
450450
static_cast<const void*>(&old_slot->value),
451451
sizeof(value_type));
452-
return is_relocatable;
453-
}
454-
455-
if (kMutableKeys::value) {
452+
} else if constexpr (kMutableKeys::value) {
456453
absl::allocator_traits<Allocator>::construct(
457454
*alloc, &new_slot->mutable_value, std::move(old_slot->mutable_value));
458455
} else {
459456
absl::allocator_traits<Allocator>::construct(*alloc, &new_slot->value,
460457
std::move(old_slot->value));
461458
}
462459
destroy(alloc, old_slot);
463-
return is_relocatable;
460+
return kIsRelocatable;
464461
}
465462
};
466463

0 commit comments

Comments
 (0)