Skip to content

Conversation

kazutakahirata
Copy link
Contributor

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<size_t>. Moving away from
template metaprogramming should improve readability.

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<size_t>.  Moving away from
template metaprogramming should improve readability.
@llvmbot
Copy link
Member

llvmbot commented Sep 6, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

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<size_t>. Moving away from
template metaprogramming should improve readability.


Full diff: https://github.com/llvm/llvm-project/pull/157309.diff

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/Hashing.h (+10-14)
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 <typename T, typename U> struct is_hashable_data<std::pair<T, U> >
                                    sizeof(std::pair<T, U>))> {};
 
 /// Helper to get the hashable data representation for a type.
-/// This variant is enabled when the type itself can be used.
-template <typename T>
-std::enable_if_t<is_hashable_data<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 <typename T>
-std::enable_if_t<!is_hashable_data<T>::value, size_t>
-get_hashable_data(const T &value) {
-  using ::llvm::hash_value;
-  return hash_value(value);
+template <typename T> auto get_hashable_data(const T &value) {
+  if constexpr (is_hashable_data<T>::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<size_t>(hash_value(value));
+  }
 }
 
 /// Helper to store data from a value into a buffer and advance the

@kazutakahirata kazutakahirata merged commit 456e49e into llvm:main Sep 7, 2025
11 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250906_ADT_Hashing_get_hashable_data_constexpr_if branch September 7, 2025 17:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants