From 93275a43a7070511a9fe033ac0106dfe5f8989db Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 8 Jun 2012 16:46:03 +1000 Subject: [PATCH] Add size_cast for size reductions --- types/casts.hpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/types/casts.hpp b/types/casts.hpp index 7c443ea4..fe408bd3 100644 --- a/types/casts.hpp +++ b/types/casts.hpp @@ -26,6 +26,7 @@ #include +//----------------------------------------------------------------------------- namespace detail { template T @@ -58,6 +59,7 @@ sign_cast (const V v) { return detail::_sign_cast(v); } +//----------------------------------------------------------------------------- namespace detail { // Same sign, no possibility of truncation with larger target type template @@ -84,4 +86,15 @@ T trunc_cast (V v) { return detail::_trunc_cast (v); } + +//----------------------------------------------------------------------------- +template +T +size_cast (const V v) { + CHECK_HARD (std::numeric_limits::min () <= v); + CHECK_HARD (std::numeric_limits::max () >= v); + + return static_cast (v); +} + #endif