Skip to content

Commit 456e49e

Browse files
[ADT] Use "constexpr if" in get_hashable_data (NFC) (#157309)
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.
1 parent b50ad94 commit 456e49e

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

llvm/include/llvm/ADT/Hashing.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -349,20 +349,16 @@ template <typename T, typename U> struct is_hashable_data<std::pair<T, U> >
349349
sizeof(std::pair<T, U>))> {};
350350

351351
/// Helper to get the hashable data representation for a type.
352-
/// This variant is enabled when the type itself can be used.
353-
template <typename T>
354-
std::enable_if_t<is_hashable_data<T>::value, T>
355-
get_hashable_data(const T &value) {
356-
return value;
357-
}
358-
/// Helper to get the hashable data representation for a type.
359-
/// This variant is enabled when we must first call hash_value and use the
360-
/// result as our data.
361-
template <typename T>
362-
std::enable_if_t<!is_hashable_data<T>::value, size_t>
363-
get_hashable_data(const T &value) {
364-
using ::llvm::hash_value;
365-
return hash_value(value);
352+
template <typename T> auto get_hashable_data(const T &value) {
353+
if constexpr (is_hashable_data<T>::value) {
354+
// This variant is enabled when the type itself can be used.
355+
return value;
356+
} else {
357+
// This variant is enabled when we must first call hash_value and use the
358+
// result as our data.
359+
using ::llvm::hash_value;
360+
return static_cast<size_t>(hash_value(value));
361+
}
366362
}
367363

368364
/// Helper to store data from a value into a buffer and advance the

0 commit comments

Comments
 (0)