From bcb4d7569f75bd2da5591b7df218dbbca2109697 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Wed, 1 Aug 2018 18:39:01 +1000 Subject: [PATCH] types/traits: add is_same_basic_type trait --- test/traits.cpp | 4 ++++ types/traits.hpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/test/traits.cpp b/test/traits.cpp index cc7b6c31..e5c97200 100644 --- a/test/traits.cpp +++ b/test/traits.cpp @@ -63,5 +63,9 @@ main (void) tap.expect (is_contiguous_v>, "vector is contiguous"); tap.expect (is_contiguous_v>, "array is contiguous"); tap.expect (!is_contiguous_v>, "list is not contiguous"); + + + tap.expect ( util::is_same_basic_type_v, "float-double same_basic_type"); + tap.expect (!util::is_same_basic_type_v, "float-int same_basic_type"); return tap.status (); } \ No newline at end of file diff --git a/types/traits.hpp b/types/traits.hpp index e407af52..4ba6198f 100644 --- a/types/traits.hpp +++ b/types/traits.hpp @@ -27,7 +27,7 @@ /// modifiers are applied in the order they are provided. /// /// use without any modifiers results in the identity modifier. -namespace util { +namespace util::types { //------------------------------------------------------------------------- template class ...ModifierT> struct compose; @@ -260,7 +260,7 @@ template struct func_traits : public ::detail::func_traits< // we apply as many transforms as possible before palming it off to the // detail class so that we don't have to write as many distinct cases. - ::util::chain_t class TemplateT, typename QueryT> constexpr auto is_same_template_template_v = is_same_template_template::value; + +namespace util::traits { + /// combines an abitrary sequence of type traits into one result + template < + typename ValueT, + template class ...Traits + > struct all : public std::integral_constant< + bool, + (Traits::value && ...) + > { }; +}; + + + +/////////////////////////////////////////////////////////////////////////////// +namespace util { + /// tests if the type is a signed integer type + template + struct is_signed_integral : public traits::all< + ValueT, std::is_signed, std::is_integral + > { }; + + + template + constexpr auto is_signed_integral_v = is_signed_integral::value; + + + /// tests if the type is an unsigned integer type + template + struct is_unsigned_integral : public traits::all< + ValueT, std::is_unsigned, std::is_integral + > { }; + + template + constexpr auto is_unsigned_integral_v = is_unsigned_integral::value; + + + /// tests if the type has the same basic fundamental type. + /// + /// ie, both float, signed, unsigned, or char. + template + struct is_same_basic_type { + static constexpr bool value = + (std::is_floating_point_v && std::is_floating_point_v) || + (is_unsigned_integral_v && is_unsigned_integral_v) || + (is_signed_integral_v && is_signed_integral_v) || + (std::is_same_v && std::is_same_v); + }; + + + template + constexpr auto is_same_basic_type_v = is_same_basic_type::value; +} + #endif