From bb98651307fc778a60ba9dd65e1edd780b0ba108 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Mon, 10 Feb 2014 01:18:44 +0100 Subject: [PATCH] New checked_numcast. --- src/casts.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/casts.hpp diff --git a/src/casts.hpp b/src/casts.hpp new file mode 100644 index 0000000..1117567 --- /dev/null +++ b/src/casts.hpp @@ -0,0 +1,25 @@ +#ifndef id8714F4436A1F462193A253C8C5AF55CD +#define id8714F4436A1F462193A253C8C5AF55CD + +#if !defined(NDEBUG) +#include +#endif + +namespace cloonel { + template + To checked_numcast ( From parFrom ) __attribute__((pure)); + + template + inline + To checked_numcast (From parFrom) { +#if defined(NDEBUG) + return static_cast(parFrom); +#else + const To retVal = static_cast(parFrom); + assert(static_cast(retVal) == parFrom); + return retVal; +#endif + } +} //namespace cloonel + +#endif