[libc-commits] [libc] b21dafa - [libc][NFC] Simplify type_traits

Guillaume Chatelet via libc-commits libc-commits at lists.llvm.org
Fri Apr 7 01:09:08 PDT 2023


Author: Guillaume Chatelet
Date: 2023-04-07T08:08:38Z
New Revision: b21dafa3ddf5ffef682374a5b2fbc22560089421

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

LOG: [libc][NFC] Simplify type_traits

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 0886ad2f19e17..afe8799c4b5ae 100644
--- a/libc/src/__support/CPP/type_traits.h
+++ b/libc/src/__support/CPP/type_traits.h
@@ -12,8 +12,12 @@
 namespace __llvm_libc {
 namespace cpp {
 
+template <typename T> struct type_identity {
+  using type = T;
+};
+
 template <bool B, typename T> struct enable_if;
-template <typename T> struct enable_if<true, T> { using type = T; };
+template <typename T> struct enable_if<true, T> : type_identity<T> {};
 template <bool B, typename T = void>
 using enable_if_t = typename enable_if<B, T>::type;
 
@@ -24,8 +28,6 @@ template <typename T, T v> struct integral_constant {
 using true_type = cpp::integral_constant<bool, true>;
 using false_type = cpp::integral_constant<bool, false>;
 
-template <typename T> struct type_identity { using type = T; };
-
 template <typename T, typename U> struct is_same : cpp::false_type {};
 template <typename T> struct is_same<T, T> : cpp::true_type {};
 template <typename T, typename U>
@@ -35,35 +37,26 @@ template <class T> struct is_const : cpp::false_type {};
 template <class T> struct is_const<const T> : cpp::true_type {};
 template <class T> inline constexpr bool is_const_v = is_const<T>::value;
 
-template <typename T> struct remove_cv : public type_identity<T> {};
-template <typename T> struct remove_cv<const T> : public type_identity<T> {};
-template <typename T> struct remove_cv<volatile T> : public type_identity<T> {};
-template <typename T>
-struct remove_cv<const volatile T> : public type_identity<T> {};
+template <typename T> struct remove_cv : type_identity<T> {};
+template <typename T> struct remove_cv<const T> : type_identity<T> {};
+template <typename T> struct remove_cv<volatile T> : type_identity<T> {};
+template <typename T> struct remove_cv<const volatile T> : type_identity<T> {};
 template <typename T> using remove_cv_t = typename remove_cv<T>::type;
 
-template <typename T> struct is_integral {
-private:
-  using unqualified_type = remove_cv_t<T>;
+namespace details {
+template <typename T, typename... Args> constexpr bool is_unqualified_any_of() {
+  return (... || is_same_v<remove_cv_t<T>, Args>);
+}
+} // namespace details
 
-public:
-  static constexpr bool value =
+template <typename T> struct is_integral {
+  static constexpr bool value = details::is_unqualified_any_of<
+      T,
 #ifdef __SIZEOF_INT128__
-      is_same_v<__int128_t, unqualified_type> ||
-      is_same_v<__uint128_t, unqualified_type> ||
+      __int128_t, __uint128_t,
 #endif
-      is_same_v<char, unqualified_type> ||
-      is_same_v<signed char, unqualified_type> ||
-      is_same_v<unsigned char, unqualified_type> ||
-      is_same_v<short, unqualified_type> ||
-      is_same_v<unsigned short, unqualified_type> ||
-      is_same_v<int, unqualified_type> ||
-      is_same_v<unsigned int, unqualified_type> ||
-      is_same_v<long, unqualified_type> ||
-      is_same_v<unsigned long, unqualified_type> ||
-      is_same_v<long long, unqualified_type> ||
-      is_same_v<unsigned long long, unqualified_type> ||
-      is_same_v<bool, unqualified_type>;
+      char, signed char, unsigned char, short, unsigned short, int,
+      unsigned int, long, unsigned long, long long, unsigned long long, bool>();
 };
 template <typename T>
 inline constexpr bool is_integral_v = is_integral<T>::value;
@@ -80,13 +73,8 @@ template <typename T> struct is_pointer<T *const volatile> : cpp::true_type {};
 template <typename T> inline constexpr bool is_pointer_v = is_pointer<T>::value;
 
 template <typename T> struct is_floating_point {
-private:
-  using unqualified_type = remove_cv_t<T>;
-
-public:
-  static constexpr bool value = is_same_v<float, unqualified_type> ||
-                                is_same_v<double, unqualified_type> ||
-                                is_same_v<long double, unqualified_type>;
+  static constexpr bool value =
+      details::is_unqualified_any_of<T, float, double, long double>();
 };
 template <typename T>
 inline constexpr bool is_floating_point_v = is_floating_point<T>::value;
@@ -114,36 +102,33 @@ template <typename T>
 inline constexpr bool is_unsigned_v = is_unsigned<T>::value;
 
 template <typename T> struct make_unsigned;
-template <> struct make_unsigned<char> { using type = unsigned char; };
-template <> struct make_unsigned<signed char> { using type = unsigned char; };
-template <> struct make_unsigned<short> { using type = unsigned short; };
-template <> struct make_unsigned<int> { using type = unsigned int; };
-template <> struct make_unsigned<long> { using type = unsigned long; };
-template <> struct make_unsigned<long long> {
-  using type = unsigned long long;
-};
-template <> struct make_unsigned<unsigned char> { using type = unsigned char; };
-template <> struct make_unsigned<unsigned short> {
-  using type = unsigned short;
-};
-template <> struct make_unsigned<unsigned int> { using type = unsigned int; };
-template <> struct make_unsigned<unsigned long> { using type = unsigned long; };
-template <> struct make_unsigned<unsigned long long> {
-  using type = unsigned long long;
-};
+template <> struct make_unsigned<char> : type_identity<unsigned char> {};
+template <> struct make_unsigned<signed char> : type_identity<unsigned char> {};
+template <> struct make_unsigned<short> : type_identity<unsigned short> {};
+template <> struct make_unsigned<int> : type_identity<unsigned int> {};
+template <> struct make_unsigned<long> : type_identity<unsigned long> {};
+template <>
+struct make_unsigned<long long> : type_identity<unsigned long long> {};
+template <>
+struct make_unsigned<unsigned char> : type_identity<unsigned char> {};
+template <>
+struct make_unsigned<unsigned short> : type_identity<unsigned short> {};
+template <> struct make_unsigned<unsigned int> : type_identity<unsigned int> {};
+template <>
+struct make_unsigned<unsigned long> : type_identity<unsigned long> {};
+template <>
+struct make_unsigned<unsigned long long> : type_identity<unsigned long long> {};
 #ifdef __SIZEOF_INT128__
-template <> struct make_unsigned<__int128_t> { using type = __uint128_t; };
-template <> struct make_unsigned<__uint128_t> { using type = __uint128_t; };
+template <> struct make_unsigned<__int128_t> : type_identity<__uint128_t> {};
+template <> struct make_unsigned<__uint128_t> : type_identity<__uint128_t> {};
 #endif
 template <typename T> using make_unsigned_t = typename make_unsigned<T>::type;
 
 // Compile time type selection.
-template <bool B, typename T, typename F> struct conditional {
-  using type = T;
-};
-template <typename T, typename F> struct conditional<false, T, F> {
-  using type = F;
-};
+template <bool B, typename T, typename F>
+struct conditional : type_identity<T> {};
+template <typename T, typename F>
+struct conditional<false, T, F> : type_identity<F> {};
 template <bool B, typename T, typename F>
 using conditional_t = typename conditional<B, T, F>::type;
 


        


More information about the libc-commits mailing list