From e05e049c6942198c5be49101d7caeef9a1b00119 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 17 Nov 2015 16:38:34 +1100 Subject: [PATCH] cast: ensure real<->int or size reduction in trunc_cast --- cast.hpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/cast.hpp b/cast.hpp index c0d57aab..90e8129d 100644 --- a/cast.hpp +++ b/cast.hpp @@ -71,9 +71,17 @@ sign_cast (const U u) /// assert if the value cannot be cast loslessly from U to T, else return the /// converted value.Note: this is only a debug-time check and is compiled out /// in optimised builds. - -template -T +template < + typename T, + typename U +> +std::enable_if_t< + // either we're casting from real to integer + !std::is_floating_point::value && std::is_floating_point::value + // or we're reducing the size of the type + || sizeof (T) < sizeof (U), + T +> trunc_cast (U u) { auto t = static_cast (u);