New checked_numcast.
This commit is contained in:
parent
07225e3aea
commit
bb98651307
1 changed files with 25 additions and 0 deletions
25
src/casts.hpp
Normal file
25
src/casts.hpp
Normal 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
|
Loading…
Reference in a new issue