diff --git a/cast.hpp b/cast.hpp index d4806dcb..d7dc8612 100644 --- a/cast.hpp +++ b/cast.hpp @@ -70,15 +70,26 @@ namespace util::cast { /////////////////////////////////////////////////////////////////////////// - // cast to a smaller type and check that both values are still equal. + // cast to a smaller type of the same signedness and realness and assert + // that both values are still equal. // // checks will be compiled out if NDEBUG is defined. - template + template < + typename NarrowT, + typename WideT, + typename = std::enable_if_t< + std::is_arithmetic_v && + std::is_arithmetic_v && + std::is_signed_v == std::is_signed_v && + std::is_floating_point_v == std::is_floating_point_v && + sizeof (NarrowT) <= sizeof (WideT), + void + > + > constexpr NarrowT narrow (const WideT &val) { static_assert (sizeof (NarrowT) <= sizeof (WideT)); - static_assert (std::is_signed_v == std::is_signed_v); #ifndef NDEBUG auto narrow = static_cast (val);