Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions absl/container/internal/container_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ struct map_slot_policy {
template <class Allocator, class... Args>
static void construct(Allocator* alloc, slot_type* slot, Args&&... args) {
emplace(slot);
if (kMutableKeys::value) {
if constexpr (kMutableKeys::value) {
absl::allocator_traits<Allocator>::construct(*alloc, &slot->mutable_value,
std::forward<Args>(args)...);
} else {
Expand All @@ -402,7 +402,7 @@ struct map_slot_policy {
template <class Allocator>
static void construct(Allocator* alloc, slot_type* slot, slot_type* other) {
emplace(slot);
if (kMutableKeys::value) {
if constexpr (kMutableKeys::value) {
absl::allocator_traits<Allocator>::construct(
*alloc, &slot->mutable_value, std::move(other->mutable_value));
} else {
Expand All @@ -422,7 +422,7 @@ struct map_slot_policy {

template <class Allocator>
static auto destroy(Allocator* alloc, slot_type* slot) {
if (kMutableKeys::value) {
if constexpr (kMutableKeys::value) {
absl::allocator_traits<Allocator>::destroy(*alloc, &slot->mutable_value);
} else {
absl::allocator_traits<Allocator>::destroy(*alloc, &slot->value);
Expand All @@ -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<typename value_type::first_type>,
absl::is_trivially_relocatable<typename value_type::second_type>>::
type();

emplace(new_slot);
if (is_relocatable) {
if constexpr (kIsRelocatable) {
Copy link
Member

@derekmauro derekmauro May 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now producing a warning:

./absl/container/internal/container_memory.h:434:35: error: parameter 'alloc' set but not used [-Werror=unused-but-set-parameter]
  434 |   static auto transfer(Allocator* alloc, slot_type* new_slot,

It seems that whatever analysis GCC is doing doesn't understand constexpr branches.

I'm guessing [[maybe_unused]] will fix this. Or perhaps kIsRelocatable could be a bool and the std::conjunction should use ::value instead of ::type(), maybe that would also give GCC a hint?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try, I'm going to also add a test case as requested.

// TODO(b/247130232,b/251814870): remove casts after fixing warnings.
std::memcpy(static_cast<void*>(std::launder(&new_slot->value)),
static_cast<const void*>(&old_slot->value),
sizeof(value_type));
return is_relocatable;
}

if (kMutableKeys::value) {
absl::allocator_traits<Allocator>::construct(
*alloc, &new_slot->mutable_value, std::move(old_slot->mutable_value));
} else {
absl::allocator_traits<Allocator>::construct(*alloc, &new_slot->value,
std::move(old_slot->value));
if constexpr (kMutableKeys::value) {
absl::allocator_traits<Allocator>::construct(
*alloc, &new_slot->mutable_value,
std::move(old_slot->mutable_value));
} else {
absl::allocator_traits<Allocator>::construct(
*alloc, &new_slot->value, std::move(old_slot->value));
}
destroy(alloc, old_slot);
}
destroy(alloc, old_slot);
return is_relocatable;
return kIsRelocatable;
}
};

Expand Down