Replace ranked-overload and SFINAE dispatch in SwissTable policy traits with if constexpr Simplify transfer_impl in common_policy_traits.h by replacing Rank0/Rank1/Rank2 overload ranking with a HasTransfer void_t check and `if constexpr`, replace ReturnKey::Impl SFINAE overloads in hash_policy_traits.h with `if constexpr` (and remove the unused pointer alias), and delete the unused `ABSL_INTERNAL_IF_*_AND_*` preprocessor macros in common.h. PiperOrigin-RevId: 1025723584
diff --git a/absl/container/internal/common.h b/absl/container/internal/common.h index 89e71b7..713b0d7 100644 --- a/absl/container/internal/common.h +++ b/absl/container/internal/common.h
@@ -32,20 +32,15 @@ #define ABSL_INTERNAL_IF_true(if_satisfied, ...) if_satisfied #define ABSL_INTERNAL_IF_false(if_satisfied, ...) __VA_ARGS__ -#define ABSL_INTERNAL_IF_true_AND_true ABSL_INTERNAL_IF_true -#define ABSL_INTERNAL_IF_false_AND_false ABSL_INTERNAL_IF_false -#define ABSL_INTERNAL_IF_true_AND_false ABSL_INTERNAL_IF_false_AND_false -#define ABSL_INTERNAL_IF_false_AND_true ABSL_INTERNAL_IF_false_AND_false - #define ABSL_INTERNAL_IF_true_OR_true ABSL_INTERNAL_IF_true #define ABSL_INTERNAL_IF_false_OR_false ABSL_INTERNAL_IF_false -#define ABSL_INTERNAL_IF_true_OR_false ABSL_INTERNAL_IF_true_OR_true -#define ABSL_INTERNAL_IF_false_OR_true ABSL_INTERNAL_IF_true_OR_true +#define ABSL_INTERNAL_IF_true_OR_false ABSL_INTERNAL_IF_true +#define ABSL_INTERNAL_IF_false_OR_true ABSL_INTERNAL_IF_true -#define ABSL_INTERNAL_IF_true_NOR_true ABSL_INTERNAL_IF_false_AND_false -#define ABSL_INTERNAL_IF_false_NOR_false ABSL_INTERNAL_IF_true_AND_true -#define ABSL_INTERNAL_IF_true_NOR_false ABSL_INTERNAL_IF_false_AND_true -#define ABSL_INTERNAL_IF_false_NOR_true ABSL_INTERNAL_IF_true_AND_false +#define ABSL_INTERNAL_IF_true_NOR_true ABSL_INTERNAL_IF_false +#define ABSL_INTERNAL_IF_false_NOR_false ABSL_INTERNAL_IF_true +#define ABSL_INTERNAL_IF_true_NOR_false ABSL_INTERNAL_IF_false +#define ABSL_INTERNAL_IF_false_NOR_true ABSL_INTERNAL_IF_false #define ABSL_INTERNAL_COMMA ,
diff --git a/absl/container/internal/common_policy_traits.h b/absl/container/internal/common_policy_traits.h index 2d65fe7..f513011 100644 --- a/absl/container/internal/common_policy_traits.h +++ b/absl/container/internal/common_policy_traits.h
@@ -74,7 +74,7 @@ // UNINITIALIZED template <class Alloc> static void transfer(Alloc* alloc, slot_type* new_slot, slot_type* old_slot) { - transfer_impl(alloc, new_slot, old_slot, Rank2{}); + transfer_impl(alloc, new_slot, old_slot); } // PRECONDITION: `slot` is INITIALIZED @@ -93,7 +93,7 @@ static constexpr bool transfer_uses_memcpy() { return std::is_same_v<decltype(transfer_impl<std::allocator<char>>( - nullptr, nullptr, nullptr, Rank2{})), + nullptr, nullptr, nullptr)), std::true_type>; } @@ -105,43 +105,37 @@ } private: - // Use go/ranked-overloads for dispatching. - struct Rank0 {}; - struct Rank1 : Rank0 {}; - struct Rank2 : Rank1 {}; + template <class Alloc, class P = Policy, class = void> + struct HasTransfer : std::false_type {}; - // Use auto -> decltype as an enabler. - // P::transfer returns std::true_type if transfer uses memcpy (e.g. in - // node_slot_policy). - template <class Alloc, class P = Policy> - static auto transfer_impl(Alloc* alloc, slot_type* new_slot, - slot_type* old_slot, - Rank2) -> decltype(P::transfer(alloc, new_slot, - old_slot)) { - return P::transfer(alloc, new_slot, old_slot); - } + template <class Alloc, class P> + struct HasTransfer<Alloc, P, + std::void_t<decltype(P::transfer( + std::declval<Alloc*>(), std::declval<slot_type*>(), + std::declval<slot_type*>()))>> : std::true_type {}; - // This overload returns true_type for the trait below. - // The conditional_t is to make the enabler type dependent. - template <class Alloc, - typename = std::enable_if_t<absl::is_trivially_relocatable< - std::conditional_t<false, Alloc, value_type>>::value>> - static std::true_type transfer_impl(Alloc*, slot_type* new_slot, - slot_type* old_slot, Rank1) { - // TODO(b/247130232): remove casts after fixing warnings. - // TODO(b/251814870): remove casts after fixing warnings. - std::memcpy( - static_cast<void*>(std::launder( - const_cast<std::remove_const_t<value_type>*>(&element(new_slot)))), - static_cast<const void*>(&element(old_slot)), sizeof(value_type)); - return {}; - } - + // Policy::transfer returns std::true_type if transfer uses memcpy (e.g. in + // node_slot_policy). The conditional_t in the else-if branch keeps value_type + // dependent on Alloc for incomplete mapped types. template <class Alloc> - static void transfer_impl(Alloc* alloc, slot_type* new_slot, - slot_type* old_slot, Rank0) { - construct(alloc, new_slot, std::move(element(old_slot))); - destroy(alloc, old_slot); + static auto transfer_impl(Alloc* alloc, slot_type* new_slot, + slot_type* old_slot) { + if constexpr (HasTransfer<Alloc>::value) { + return Policy::transfer(alloc, new_slot, old_slot); + } else if constexpr (absl::is_trivially_relocatable<std::conditional_t< + false, Alloc, value_type>>::value) { + // TODO(b/247130232): remove casts after fixing warnings. + // TODO(b/251814870): remove casts after fixing warnings. + using MutableValue = std::remove_const_t<value_type>; + std::memcpy(static_cast<void*>(std::launder( + const_cast<MutableValue*>(&element(new_slot)))), + static_cast<const void*>(&element(old_slot)), + sizeof(value_type)); + return std::true_type{}; + } else { + construct(alloc, new_slot, std::move(element(old_slot))); + destroy(alloc, old_slot); + } } };
diff --git a/absl/container/internal/hash_policy_traits.h b/absl/container/internal/hash_policy_traits.h index 3ea4f9b..3f8c4dd 100644 --- a/absl/container/internal/hash_policy_traits.h +++ b/absl/container/internal/hash_policy_traits.h
@@ -51,25 +51,17 @@ private: struct ReturnKey { - template <class Key, - std::enable_if_t<std::is_lvalue_reference_v<Key>, int> = 0> - static key_type& Impl(Key&& k, int) { - return *std::launder( - const_cast<key_type*>(std::addressof(std::forward<Key>(k)))); - } - - template <class Key> - static Key Impl(Key&& k, char) { - return std::forward<Key>(k); - } - // When Key=T&, we forward the lvalue reference. - // When Key=T, we return by value to avoid a dangling reference. - // eg, for string_hash_map. + // When Key=T, we return by value to avoid a dangling reference + // (e.g., for string_hash_map). template <class Key, class... Args> - auto operator()(Key&& k, const Args&...) const - -> decltype(Impl(std::forward<Key>(k), 0)) { - return Impl(std::forward<Key>(k), 0); + std::conditional_t<std::is_lvalue_reference_v<Key>, key_type&, Key> + operator()(Key&& k, const Args&...) const { + if constexpr (std::is_lvalue_reference_v<Key>) { + return *std::launder(const_cast<key_type*>(std::addressof(k))); + } else { + return std::forward<Key>(k); + } } }; @@ -114,7 +106,6 @@ using init_type = typename Policy::init_type; using reference = decltype(Policy::element(std::declval<slot_type*>())); - using pointer = std::remove_reference_t<reference>*; using value_type = std::remove_reference_t<reference>; // Policies can set this variable to tell raw_hash_set that all iterators