Skip to content

Commit

Permalink
[libc] Use SFINAE to implement is_signed/is_unsigned
Browse files Browse the repository at this point in the history
This allows to use the type traits with types that are non constructible from integers.
  • Loading branch information
gchatelet committed Apr 7, 2023
1 parent 40d8591 commit d2a32d7
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions libc/src/__support/CPP/type_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
};
Expand Down

0 comments on commit d2a32d7

Please sign in to comment.