diff --git a/base/checked_cast.hpp b/base/checked_cast.hpp index 363840c785..bc37f13418 100644 --- a/base/checked_cast.hpp +++ b/base/checked_cast.hpp @@ -2,19 +2,31 @@ #include "base/assert.hpp" +#include + namespace base { template ReturnType checked_cast(ParameterType v) { - CHECK_EQUAL(static_cast(static_cast(v)), v, ()); - return static_cast(v); + static_assert(std::is_integral::value, "ParameterType should be integral"); + static_assert(std::is_integral::value, "ReturnType should be integral"); + + ReturnType const result = static_cast(v); + CHECK_EQUAL(static_cast(result), v, ()); + CHECK((result > 0) == (v > 0), ("checked_cast failed, value =", v, ", result =", result)); + return result; } template ReturnType asserted_cast(ParameterType v) { - ASSERT_EQUAL(static_cast(static_cast(v)), v, ()); - return static_cast(v); + static_assert(std::is_integral::value, "ParameterType should be integral"); + static_assert(std::is_integral::value, "ReturnType should be integral"); + + ReturnType const result = static_cast(v); + ASSERT_EQUAL(static_cast(result), v, ()); + ASSERT((result > 0) == (v > 0), ("asserted_cast failed, value =", v, ", result =", result)); + return result; } } // namespace base