Reimplement and extend using implementation suggestions from cppref
This commit is contained in:
parent
5e318d1a42
commit
c6e9534b52
184
type.hpp
184
type.hpp
@ -7,141 +7,223 @@
|
||||
|
||||
#include <float.h>
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
|
||||
namespace type {
|
||||
|
||||
// <type_traits>
|
||||
|
||||
// clang-format off
|
||||
template <bool Val> struct set_bool { static constexpr auto value = Val; };
|
||||
template <typename T> struct remove_reference { using type = T; };
|
||||
template <typename T> struct remove_reference<T &> { using type = T; };
|
||||
template <typename T> struct remove_reference<T &&> { using type = T; };
|
||||
template <typename T> using remove_reference_t = typename remove_reference<T>::type;
|
||||
|
||||
struct true_type : set_bool<true> {};
|
||||
struct false_type : set_bool<false> {};
|
||||
namespace detail {
|
||||
|
||||
template <typename...> struct always_false : false_type {};
|
||||
template <typename T> struct type_identity { using type = T; };
|
||||
|
||||
template <typename... Ts> static constexpr auto always_false_v = always_false<Ts...>::value;
|
||||
template <typename T> auto try_add_pointer(int) -> type_identity<remove_reference_t<T> *>;
|
||||
template <typename T> auto try_add_pointer(...) -> type_identity<T>;
|
||||
|
||||
template <typename T> struct is_integral : false_type {};
|
||||
template <> struct is_integral<bool> : true_type {};
|
||||
template <> struct is_integral<char> : true_type {};
|
||||
template <> struct is_integral<signed char> : true_type {};
|
||||
template <> struct is_integral<unsigned char> : true_type {};
|
||||
template <> struct is_integral<short> : true_type {};
|
||||
template <> struct is_integral<int> : true_type {};
|
||||
template <> struct is_integral<long int> : true_type {};
|
||||
template <> struct is_integral<long long int> : true_type {};
|
||||
template <> struct is_integral<unsigned short> : true_type {};
|
||||
template <> struct is_integral<unsigned int> : true_type {};
|
||||
template <> struct is_integral<unsigned long int> : true_type {};
|
||||
template <> struct is_integral<unsigned long long int> : true_type {};
|
||||
} // namespace detail
|
||||
|
||||
template <typename T> static constexpr auto is_integral_v = is_integral<T>::value;
|
||||
template <typename T, T v>
|
||||
struct integral_constant {
|
||||
static constexpr T value = v;
|
||||
using value_type = T;
|
||||
using type = integral_constant; // using injected-class-name
|
||||
constexpr operator value_type() const noexcept { return value; }
|
||||
constexpr value_type operator()() const noexcept { return value; } // since c++14
|
||||
};
|
||||
template <bool B> using bool_constant = integral_constant<bool, B>;
|
||||
using false_type = bool_constant<false>;
|
||||
using true_type = bool_constant<true>;
|
||||
|
||||
template <typename T> struct is_floating_point : false_type {};
|
||||
template <> struct is_floating_point<float> : true_type {};
|
||||
template <> struct is_floating_point<double> : true_type {};
|
||||
template <> struct is_floating_point<long double> : true_type {};
|
||||
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, class T, class F> using conditional_t = typename conditional<B, T, F>::type;
|
||||
|
||||
template <typename T> static constexpr auto is_floating_point_v = is_floating_point<T>::value;
|
||||
template <typename T> struct is_array : false_type {};
|
||||
template <typename T> struct is_array<T[]> : true_type {};
|
||||
template <typename T, size_t N> struct is_array<T[N]> : true_type {};
|
||||
template <typename T> inline constexpr bool is_array_v = is_array<T>::value;
|
||||
|
||||
template <typename T> struct remove_extent { using type = T; };
|
||||
template <typename T> struct remove_extent<T[]> { using type = T; };
|
||||
template <typename T, size_t N> struct remove_extent<T[N]> { using type = T; };
|
||||
template <typename T> using remove_extent_t = typename remove_extent<T>::type;
|
||||
|
||||
template <typename T> struct is_const : false_type {};
|
||||
template <typename T> struct is_const<const T> : true_type {};
|
||||
template <typename T> inline constexpr bool is_const_v = is_const<T>::value;
|
||||
|
||||
template <typename T> struct is_reference : false_type {};
|
||||
template <typename T> struct is_reference<T &> : true_type {};
|
||||
template <typename T> struct is_reference<T &&> : true_type {};
|
||||
template <typename T> inline constexpr bool is_reference_v = is_reference<T>::value;
|
||||
|
||||
template <typename T> struct is_function : integral_constant<bool, !is_const_v<const T> && !is_reference_v<T>> {};
|
||||
template <typename T> inline constexpr bool is_function_v = is_function<T>::value;
|
||||
|
||||
template <typename T> struct add_pointer : decltype(detail::try_add_pointer<T>(0)) {};
|
||||
template <typename T> using add_pointer_t = typename add_pointer<T>::type;
|
||||
|
||||
template <typename T> struct remove_cv { using type = T; };
|
||||
template <typename T> struct remove_cv<const T> { using type = T; };
|
||||
template <typename T> struct remove_cv<volatile T> { using type = T; };
|
||||
template <typename T> struct remove_cv<const volatile T> { using type = T; };
|
||||
template <typename T> struct remove_const { using type = T; };
|
||||
template <typename T> struct remove_const<const T> { using type = T; };
|
||||
template <typename T> struct remove_volatile { using type = T; };
|
||||
template <typename T> struct remove_volatile<volatile T> { using type = T; };
|
||||
template <typename T> using remove_cv_t = typename remove_cv<T>::type;
|
||||
template <typename T> using remove_const_t = typename remove_const<T>::type;
|
||||
template <typename T> using remove_volatile_t = typename remove_volatile<T>::type;
|
||||
|
||||
template <typename T>
|
||||
struct decay {
|
||||
private:
|
||||
using U = remove_reference_t<T>;
|
||||
public:
|
||||
using type = conditional_t<is_array_v<U>,
|
||||
remove_extent_t<U> *,
|
||||
conditional_t<is_function<U>::value,
|
||||
add_pointer_t<U>,
|
||||
remove_cv_t<U>
|
||||
>
|
||||
>;
|
||||
};
|
||||
template <typename T> using decay_t = typename decay<T>::type;
|
||||
|
||||
template <typename T, typename U> struct is_same : false_type {};
|
||||
template <typename T> struct is_same<T, T> : true_type {};
|
||||
|
||||
template <typename T, typename U> static constexpr auto is_same_v = is_same<T, U>::value;
|
||||
template <typename T, typename U> inline constexpr auto is_same_v = is_same<T, U>::value;
|
||||
|
||||
template <typename T>
|
||||
struct NumericLimits {
|
||||
static constexpr T min() { return T(); }
|
||||
static constexpr T max() { return T(); }
|
||||
};
|
||||
struct is_floating_point : integral_constant<bool,
|
||||
is_same_v<float, remove_cv_t<T>> ||
|
||||
is_same_v<double, remove_cv_t<T>> ||
|
||||
is_same_v<long double, remove_cv_t<T>>>
|
||||
{};
|
||||
template <typename T> inline constexpr auto is_floating_point_v = is_floating_point<T>::value;
|
||||
|
||||
template <typename T>
|
||||
struct is_integral : integral_constant<bool,
|
||||
is_same_v<bool, remove_cv_t<T>> ||
|
||||
is_same_v<char, remove_cv_t<T>> ||
|
||||
is_same_v<signed char, remove_cv_t<T>> ||
|
||||
is_same_v<unsigned char, remove_cv_t<T>> ||
|
||||
is_same_v<wchar_t, remove_cv_t<T>> ||
|
||||
is_same_v<char16_t, remove_cv_t<T>> ||
|
||||
is_same_v<char32_t, remove_cv_t<T>> ||
|
||||
is_same_v<short int, remove_cv_t<T>> ||
|
||||
is_same_v<unsigned short int, remove_cv_t<T>> ||
|
||||
is_same_v<int, remove_cv_t<T>> ||
|
||||
is_same_v<unsigned int, remove_cv_t<T>> ||
|
||||
is_same_v<long int, remove_cv_t<T>> ||
|
||||
is_same_v<unsigned long int, remove_cv_t<T>> ||
|
||||
is_same_v<long long int, remove_cv_t<T>> ||
|
||||
is_same_v<unsigned long long int, remove_cv_t<T>>>
|
||||
{};
|
||||
template <typename T> inline constexpr auto is_integral_v = is_integral<T>::value;
|
||||
|
||||
// Not part of <type_traits>, but very useful
|
||||
template <typename...> struct always_false : false_type {};
|
||||
template <typename... Ts> inline constexpr auto always_false_v = always_false<Ts...>::value;
|
||||
|
||||
// <limits>
|
||||
|
||||
template <typename T> struct numeric_limits {};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<bool> {
|
||||
struct numeric_limits<bool> {
|
||||
static constexpr bool min() { return false; }
|
||||
static constexpr bool max() { return true; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<char> {
|
||||
struct numeric_limits<char> {
|
||||
static constexpr char min() { return CHAR_MIN; }
|
||||
static constexpr char max() { return CHAR_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<signed char> {
|
||||
struct numeric_limits<signed char> {
|
||||
static constexpr signed char min() { return SCHAR_MIN; }
|
||||
static constexpr signed char max() { return SCHAR_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<unsigned char> {
|
||||
struct numeric_limits<unsigned char> {
|
||||
static constexpr unsigned char min() { return 0; }
|
||||
static constexpr unsigned char max() { return UCHAR_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<short> {
|
||||
struct numeric_limits<short> {
|
||||
static constexpr short min() { return SHRT_MIN; }
|
||||
static constexpr short max() { return SHRT_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<int> {
|
||||
struct numeric_limits<int> {
|
||||
static constexpr int min() { return INT_MIN; }
|
||||
static constexpr int max() { return INT_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<long> {
|
||||
struct numeric_limits<long> {
|
||||
static constexpr long int min() { return LONG_MIN; }
|
||||
static constexpr long int max() { return LONG_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<long long int> {
|
||||
struct numeric_limits<long long int> {
|
||||
static constexpr long long int min() { return LLONG_MIN; }
|
||||
static constexpr long long int max() { return LLONG_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<unsigned short> {
|
||||
struct numeric_limits<unsigned short> {
|
||||
static constexpr unsigned short min() { return 0; }
|
||||
static constexpr unsigned short max() { return USHRT_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<unsigned int> {
|
||||
struct numeric_limits<unsigned int> {
|
||||
static constexpr unsigned int min() { return 0; }
|
||||
static constexpr unsigned int max() { return UINT_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<unsigned long int> {
|
||||
struct numeric_limits<unsigned long int> {
|
||||
static constexpr unsigned long int min() { return 0; }
|
||||
static constexpr unsigned long int max() { return ULONG_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<unsigned long long int> {
|
||||
struct numeric_limits<unsigned long long int> {
|
||||
static constexpr unsigned long long int min() { return 0; }
|
||||
static constexpr unsigned long long int max() { return ULLONG_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<float> {
|
||||
template <typename... Ts> static constexpr float min() { return FLT_MIN; }
|
||||
template <typename... Ts> static constexpr float max() { return FLT_MAX; }
|
||||
struct numeric_limits<float> {
|
||||
static constexpr float min() { return FLT_MIN; }
|
||||
static constexpr float max() { return FLT_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<double> {
|
||||
template <typename... Ts> static constexpr double min() { return DBL_MIN; }
|
||||
template <typename... Ts> static constexpr double max() { return DBL_MAX; }
|
||||
struct numeric_limits<double> {
|
||||
static constexpr double min() { return DBL_MIN; }
|
||||
static constexpr double max() { return DBL_MAX; }
|
||||
};
|
||||
|
||||
template <>
|
||||
struct NumericLimits<long double> {
|
||||
template <typename... Ts> static constexpr long double min() { return LDBL_MIN; }
|
||||
template <typename... Ts> static constexpr long double max() { return LDBL_MAX; }
|
||||
struct numeric_limits<long double> {
|
||||
static constexpr long double min() { return LDBL_MIN; }
|
||||
static constexpr long double max() { return LDBL_MAX; }
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user