From c9cc38eac7534be8cc550a4136478b211bb8ee04 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 30 Jan 2018 16:25:28 +1100 Subject: [PATCH] cast: tighten type constraints for narrow cast --- cast.hpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) 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);