[libc-commits] [libc] d2a32d7 - [libc] Use SFINAE to implement is_signed/is_unsigned

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Fri Apr 7 04:58:50 PDT 2023


Author: Guillaume Chatelet
Date: 2023-04-07T11:58:38Z
New Revision: d2a32d7be01ff94f77b26d7c86b854f09677167a

URL: https://github.com/llvm/llvm-project/commit/d2a32d7be01ff94f77b26d7c86b854f09677167a
DIFF: https://github.com/llvm/llvm-project/commit/d2a32d7be01ff94f77b26d7c86b854f09677167a.diff

LOG: [libc] Use SFINAE to implement is_signed/is_unsigned

This allows to use the type traits with types that are non constructible from integers.

Added: 
    

Modified: 
    libc/src/__support/CPP/type_traits.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CPP/type_traits.h b/libc/src/__support/CPP/type_traits.h
index afe8799c4b5a..7457bbefed67 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; }
 };


        


More information about the libc-commits mailing list