aboutsummaryrefslogtreecommitdiff
path: root/libc
diff options
context:
space:
mode:
authorGuillaume Chatelet <gchatelet@google.com>2023-04-07 11:57:39 +0000
committerGuillaume Chatelet <gchatelet@google.com>2023-04-07 11:58:38 +0000
commitd2a32d7be01ff94f77b26d7c86b854f09677167a (patch)
tree03b7ee45d4ceaa3a5a629e02196b4678fd9fb8f1 /libc
parent40d859154c1692e6e2d6a7ad91efd610c9b7e5be (diff)
downloadllvm-d2a32d7be01ff94f77b26d7c86b854f09677167a.zip
llvm-d2a32d7be01ff94f77b26d7c86b854f09677167a.tar.gz
llvm-d2a32d7be01ff94f77b26d7c86b854f09677167a.tar.bz2
[libc] Use SFINAE to implement is_signed/is_unsigned
This allows to use the type traits with types that are non constructible from integers.
Diffstat (limited to 'libc')
-rw-r--r--libc/src/__support/CPP/type_traits.h14
1 files changed, 12 insertions, 2 deletions
diff --git a/libc/src/__support/CPP/type_traits.h b/libc/src/__support/CPP/type_traits.h
index afe8799..7457bbe 100644
--- a/libc/src/__support/CPP/type_traits.h
+++ b/libc/src/__support/CPP/type_traits.h
@@ -86,15 +86,25 @@ template <typename T> struct is_arithmetic {
template <typename T>
inline constexpr bool is_arithmetic_v = is_arithmetic<T>::value;
+namespace details {
+template <typename T, bool = is_arithmetic<T>::value>
+struct is_signed : integral_constant<bool, (T(-1) < T(0))> {};
+template <typename T> struct is_signed<T, false> : false_type {};
+
+template <typename T, bool = is_arithmetic<T>::value>
+struct is_unsigned : integral_constant<bool, (T(-1) > T(0))> {};
+template <typename T> struct is_unsigned<T, false> : false_type {};
+} // namespace details
+
template <typename T> struct is_signed {
- static constexpr bool value = is_arithmetic<T>::value && (T(-1) < T(0));
+ static constexpr bool value = details::is_signed<T>::value;
constexpr operator bool() const { return value; }
constexpr bool operator()() const { return value; }
};
template <typename T> inline constexpr bool is_signed_v = is_signed<T>::value;
template <typename T> struct is_unsigned {
- static constexpr bool value = is_arithmetic<T>::value && (T(-1) > T(0));
+ static constexpr bool value = details::is_unsigned<T>::value;
constexpr operator bool() const { return value; }
constexpr bool operator()() const { return value; }
};