Skip to content

Commit 6097759

Browse files
[libc] fix -Wshift-count-overflow in UInt.h
Not that I'm very good at SFINAE, but it seems that conversion operators are perhaps difficult to compose with SFINAE. I saw an example that used one layer of indirection to have an explicit return type that could then be used with enable_if_t. Link: https://stackoverflow.com/a/7604580 Fixes: llvm#74623
1 parent 1f283a6 commit 6097759

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

libc/src/__support/UInt.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,20 @@ template <size_t Bits, bool Signed> struct BigInt {
103103
val[i] = words[i];
104104
}
105105

106-
template <typename T, typename = cpp::enable_if_t<cpp::is_integral_v<T> &&
107-
sizeof(T) <= 16 &&
108-
!cpp::is_same_v<T, bool>>>
109-
LIBC_INLINE constexpr explicit operator T() const {
110-
if constexpr (sizeof(T) <= 8)
111-
return static_cast<T>(val[0]);
106+
template <typename T> LIBC_INLINE constexpr explicit operator T() const {
107+
return to<T>();
108+
}
112109

110+
template <typename T>
111+
LIBC_INLINE constexpr cpp::enable_if_t<
112+
cpp::is_integral_v<T> && sizeof(T) <= 8 && !cpp::is_same_v<T, bool>, T>
113+
to() const {
114+
return static_cast<T>(val[0]);
115+
}
116+
template <typename T>
117+
LIBC_INLINE constexpr cpp::enable_if_t<
118+
cpp::is_integral_v<T> && sizeof(T) == 16, T>
119+
to() const {
113120
// T is 128-bit.
114121
T lo = static_cast<T>(val[0]);
115122

@@ -121,7 +128,6 @@ template <size_t Bits, bool Signed> struct BigInt {
121128
return lo;
122129
}
123130
} else {
124-
// TODO: silence shift warning
125131
return static_cast<T>((static_cast<T>(val[1]) << 64) + lo);
126132
}
127133
}

0 commit comments

Comments
 (0)