diff options
Diffstat (limited to 'utils.h')
-rw-r--r-- | utils.h | 19 |
1 files changed, 19 insertions, 0 deletions
@@ -0,0 +1,19 @@ +#include <limits> +#include <stdexcept> + +/* + * Credits to: + * http://stackoverflow.com/questions/13150449/efficient-unsigned-to-signed-cast-avoiding-implementation-defined-behavior + */ +template<typename Uint, typename Int> +Int uintToInt(Uint x) +{ + if (x <= std::numeric_limits<Int>::max()) + return static_cast<Int>(x); + + if (x >= std::numeric_limits<Int>::min()) + return static_cast<Int>(x - std::numeric_limits<Int>::min()) + + std::numeric_limits<Int>::min(); + + throw std::overflow_error("given value does not fit integer type"); +} |