From a14682316e1789f7c844c1898e905cc5d16c4a94 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Wed, 3 Sep 2025 10:08:22 -0700 Subject: [PATCH] [ADT] Use "constexpr if" in get_hashable_data (NFC) This patch combines two implementations of get_hashable_data into one with "constexpr if". I'm retaining the original return type of the second variant, size_t, with static_cast. Moving away from template metaprogramming should improve readability. --- llvm/include/llvm/ADT/Hashing.h | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/llvm/include/llvm/ADT/Hashing.h b/llvm/include/llvm/ADT/Hashing.h index ad131015a7d99..ec22fe3a28cf9 100644 --- a/llvm/include/llvm/ADT/Hashing.h +++ b/llvm/include/llvm/ADT/Hashing.h @@ -349,20 +349,16 @@ template struct is_hashable_data > sizeof(std::pair))> {}; /// Helper to get the hashable data representation for a type. -/// This variant is enabled when the type itself can be used. -template -std::enable_if_t::value, T> -get_hashable_data(const T &value) { - return value; -} -/// Helper to get the hashable data representation for a type. -/// This variant is enabled when we must first call hash_value and use the -/// result as our data. -template -std::enable_if_t::value, size_t> -get_hashable_data(const T &value) { - using ::llvm::hash_value; - return hash_value(value); +template auto get_hashable_data(const T &value) { + if constexpr (is_hashable_data::value) { + // This variant is enabled when the type itself can be used. + return value; + } else { + // This variant is enabled when we must first call hash_value and use the + // result as our data. + using ::llvm::hash_value; + return static_cast(hash_value(value)); + } } /// Helper to store data from a value into a buffer and advance the