diff options
author | Guillaume Chatelet <gchatelet@google.com> | 2023-04-07 08:07:51 +0000 |
---|---|---|
committer | Guillaume Chatelet <gchatelet@google.com> | 2023-04-07 08:08:38 +0000 |
commit | b21dafa3ddf5ffef682374a5b2fbc22560089421 (patch) | |
tree | 17a6099d4f87717414660d0bf8c5f990004a8b60 /libc | |
parent | aebe7ce9845453739c5ddd1f2bc2f6c6ac57a944 (diff) | |
download | llvm-b21dafa3ddf5ffef682374a5b2fbc22560089421.zip llvm-b21dafa3ddf5ffef682374a5b2fbc22560089421.tar.gz llvm-b21dafa3ddf5ffef682374a5b2fbc22560089421.tar.bz2 |
[libc][NFC] Simplify type_traits
Diffstat (limited to 'libc')
-rw-r--r-- | libc/src/__support/CPP/type_traits.h | 103 |
1 files changed, 44 insertions, 59 deletions
diff --git a/libc/src/__support/CPP/type_traits.h b/libc/src/__support/CPP/type_traits.h index 0886ad2..afe8799 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; |