New checked_numcast.

This commit is contained in:
King_DuckZ 2014-02-10 01:18:44 +01:00
parent 07225e3aea
commit bb98651307

25
src/casts.hpp Normal file
View file

@ -0,0 +1,25 @@
#ifndef id8714F4436A1F462193A253C8C5AF55CD
#define id8714F4436A1F462193A253C8C5AF55CD
#if !defined(NDEBUG)
#include <cassert>
#endif
namespace cloonel {
template <typename To, typename From>
To checked_numcast ( From parFrom ) __attribute__((pure));
template <typename To, typename From>
inline
To checked_numcast (From parFrom) {
#if defined(NDEBUG)
return static_cast<To>(parFrom);
#else
const To retVal = static_cast<To>(parFrom);
assert(static_cast<From>(retVal) == parFrom);
return retVal;
#endif
}
} //namespace cloonel
#endif